author: Chenzhu-Xie name: Library/xczphysics/CONFIG/Nearest_Pattern/Unwrap tags: meta/library
-- ่พ
ๅฉๅฝๆฐ๏ผๆๅไธๅๆจกๅผไธ็โๆ ธโ (Content)
local function extractCore(name, text)
if name == "Wiki Link" then
-- ๅน้
[wiki](wiki) ๆ [wiki|alias](wiki|alias)๏ผๅ wiki
local core = text:match("%[%[([^%]]+)%]%]")
if core and core:find("|") then core = core:match("^([^|]+)|") end
return core
elseif name == "Fields" then
return text:match("%[([^:]+:[^%]]+)%]") -- ๅ [key:value] ไธญ็ key:value
elseif name == "Image" then
return text:match("!%[^%](^%)-%]%(([^)]+)%)") -- ๅ src ๆๅญ
elseif name == "Markdown Link" then
return text:match("%[^%](^%)+%]%(([^)]+)%)") -- ๅ [text](url) ไธญ็ url
elseif name == "Color Func" then
return text:match("%([\"\']([^\"\']+)[\"\']%)") -- ๅ ${Color("value")} ไธญ็ value
elseif name == "Bold" then
return text:match("%*%*([^\n%*]+)%*%*")
elseif name == "Italic" then
return text:match("_([^\n_]+)_")
elseif name == "Sup" then
return text:match("%^([^ \n%^]+)%^")
elseif name == "Strikethrough" then
return text:match("~~([^\n]+)~~")
elseif name == "Sub" then
return text:match("~([^ \n~]+)~")
elseif name == "Marker" then
return text:match("==([^\n]+)==")
elseif name == "Inline Code" then
return text:match("`([^\n`]+)`")
elseif name == "Lua Block" or name == "Script Block" then
return text:match("```%a+\n?(.*)```")
elseif name == "Header" then
return text:match("#+ (.*)")
elseif name == "Tag" then
return text:sub(2) -- ๅปๆๅ้ข็ #
end
return text -- ้ป่ฎค่ฟๅๅๆ ท
end
command.define{
name = "Cursor: Unwrap Nearest Pattern",
description = "Remove the format of the nearest pattern",
key = "Alt-w",
run = function()
local match = findNearestInlinePattern()
if not match then
editor.flashNotification("No pattern matched.")
return
end
local core = extractCore(match.name, match.text)
if not core then
editor.flashNotification("Failed to extract core from " .. match.name)
return
end
editor.copyToClipboard(core)
editor.replaceRange(match.start - 1, match.stop, core)
editor.flashNotification(match.name .. ": Unwrapped โ
")
if not match.name:find"Block" then
editor.flashNotification(core)
end
end
}