-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
68 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,53 @@ | ||
local jsdoc = require("js").global.window.document | ||
|
||
local input_pattern = jsdoc:querySelector("#input-pattern") | ||
local input_text = jsdoc:querySelector("#input-text") | ||
local text_highlighted = jsdoc:querySelector("#text-highlighted") | ||
|
||
local inspect = function(t) | ||
for key, value in pairs(t) do | ||
for key, value in ipairs(t) do | ||
print(key, " -> ", value) | ||
end | ||
end | ||
|
||
local span_wrap = function(s, classes) | ||
return string.format('<span class="%s">%s</span>', classes, s) | ||
local mark_wrap = function(s, classes) | ||
return string.format('<mark class="%s">%s</mark>', classes, s) | ||
end | ||
|
||
local highlight_lines = function(text) | ||
-- aa | ||
---@param text string | ||
---@param pattern string | ||
local highlight_lines = function(text, pattern) | ||
-- lua pattern moment need to wrap pattern in capture else it stop capturing xD | ||
pattern = "(" .. pattern .. ")" | ||
local result = text:gsub(pattern, function(...) | ||
local captures = { ... } | ||
---@type string | ||
local insertion = mark_wrap(captures[1], "highlight-primary") | ||
for index, value in ipairs(captures) do | ||
if index ~= 1 then | ||
insertion = insertion:gsub(value, mark_wrap(value, "highlight-secondary"), 1) | ||
end | ||
end | ||
return insertion | ||
end) | ||
return result | ||
end | ||
|
||
local pattern = jsdoc:querySelector("#input-pattern") | ||
local text = jsdoc:querySelector("#input-text") | ||
local text_highlighted = jsdoc:querySelector("#text-highlighted") | ||
|
||
local update = function() | ||
text_highlighted.innerText = text.value | ||
local success, hl = pcall(highlight_lines, input_text.value, input_pattern.value) | ||
if success then | ||
text_highlighted.innerHTML = hl | ||
else | ||
text_highlighted.innerHTML = input_text.value | ||
end | ||
end | ||
|
||
text:addEventListener("input", function(element, event) | ||
input_pattern:addEventListener("input", function(element, event) | ||
update() | ||
end) | ||
|
||
input_text:addEventListener("input", function(element, event) | ||
update() | ||
end) | ||
|
||
update() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters