Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 26 additions & 37 deletions lua/markview/parsers/latex.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
--- HTML parser for `markview.nvim`
local latex = {};
local symbols = require("markview.symbols");

--- `string.gsub()` with support for multiple patterns.
---@param text string
Expand All @@ -15,6 +16,29 @@ local function bulk_gsub (text, gsubs)
return _o;
end

---@param symbol_map table<string, string>
---@return string[]
local function style_symbol_commands(symbol_map)
local commands = {};

for name, _ in pairs(symbol_map or {}) do
if type(name) == "string" and #name > 1 and name:match("^[A-Za-z]+$") then
table.insert(commands, "\\" .. name);
end
end

table.sort(commands, function (a, b)
return #a > #b;
end);

return commands;
end

---@return string[]
local function previewable_symbol_commands()
return style_symbol_commands(symbols.entries);
end

--- Checks if the given node is inside of `\text{}`.
---@param TSNode table
---@return boolean
Expand Down Expand Up @@ -304,23 +328,7 @@ latex.subscript = function (_, TSNode, text, range)
local node = TSNode;
local level, preview = 0, true;

local supported_symbols = {
"\\beta",
"\\gamma",
"\\rho",
"\\phi",
"\\chi",

-- See OZU2DEV/markview#379
"\\epsilon",
"\\omicron",
"\\alpha",
"\\eta",
"\\nu",
"\\rho",
"\\upsilon",
"\\sigma",
}
local supported_symbols = previewable_symbol_commands();

for _, line in ipairs(text) do
if bulk_gsub(line, supported_symbols):match("%\\") then
Expand Down Expand Up @@ -359,26 +367,7 @@ latex.superscript = function (_, TSNode, text, range)
local node = TSNode;
local level, preview = 0, true;

local supported_symbols = {
"\\alpha",
"\\beta",
"\\gamma",
"\\delta",
"\\epsilon",
"\\theta",
"\\iota",
"\\Phi",
"\\varphi",
"\\chi",

-- See OZU2DEV/markview#379
"\\omicron",
"\\eta",
"\\nu",
"\\rho",
"\\upsilon",
"\\sigma",
}
local supported_symbols = previewable_symbol_commands();

for _, line in ipairs(text) do
if bulk_gsub(line, supported_symbols):match("%\\") then
Expand Down
80 changes: 56 additions & 24 deletions lua/markview/renderers/latex.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@ local symbols = require("markview.symbols");
local spec = require("markview.spec");
local utils = require("markview.utils");

---@param text string?
---@return string?
local function normalize_style_symbol(text)
if type(text) ~= "string" or text == "" then
return nil;
end

local key = text:sub(2);

if key:match("^\\") then
key = key:sub(2);
end

return key ~= "" and key or nil;
end

--- Cached values.
latex.cache = {
font_regions = {},
Expand Down Expand Up @@ -449,20 +465,28 @@ latex.subscript = function (buffer, item)
end_col = range.col_end,
conceal = "",
});
elseif symbols.subscripts[item.text[1]:sub(2)] then
vim.api.nvim_buf_set_extmark(buffer, latex.ns, range.row_start, range.col_start, {
undo_restore = false, invalidate = true,
end_col = range.col_start + 1,
conceal = ""
});
else
local symbol_key = normalize_style_symbol(item.text[1]);
local symbol_text = symbol_key and (
symbols.subscripts[symbol_key] or
symbols.entries[symbol_key]
);

if symbol_text then
vim.api.nvim_buf_set_extmark(buffer, latex.ns, range.row_start, range.col_start, {
undo_restore = false, invalidate = true,
end_col = range.col_start + 1,
conceal = ""
});

vim.api.nvim_buf_set_extmark(buffer, latex.ns, range.row_start, range.col_start + 1, {
undo_restore = false, invalidate = true,
virt_text_pos = "overlay",
virt_text = { { symbols.subscripts[item.text[1]:sub(2)], utils.set_hl(hl) } },
vim.api.nvim_buf_set_extmark(buffer, latex.ns, range.row_start, range.col_start + 1, {
undo_restore = false, invalidate = true,
virt_text_pos = "overlay",
virt_text = { { symbol_text, utils.set_hl(hl) } },

hl_mode = "combine"
});
hl_mode = "combine"
});
end
end
end

Expand Down Expand Up @@ -562,20 +586,28 @@ latex.superscript = function (buffer, item)
end_col = range.col_end,
conceal = "",
});
elseif symbols.superscripts[item.text[1]:sub(2)] then
vim.api.nvim_buf_set_extmark(buffer, latex.ns, range.row_start, range.col_start, {
undo_restore = false, invalidate = true,
end_col = range.col_start + 1,
conceal = ""
});
else
local symbol_key = normalize_style_symbol(item.text[1]);
local symbol_text = symbol_key and (
symbols.superscripts[symbol_key] or
symbols.entries[symbol_key]
);

if symbol_text then
vim.api.nvim_buf_set_extmark(buffer, latex.ns, range.row_start, range.col_start, {
undo_restore = false, invalidate = true,
end_col = range.col_start + 1,
conceal = ""
});

vim.api.nvim_buf_set_extmark(buffer, latex.ns, range.row_start, range.col_start + 1, {
undo_restore = false, invalidate = true,
virt_text_pos = "overlay",
virt_text = { { symbols.superscripts[item.text[1]:sub(2)], utils.set_hl(hl) } },
vim.api.nvim_buf_set_extmark(buffer, latex.ns, range.row_start, range.col_start + 1, {
undo_restore = false, invalidate = true,
virt_text_pos = "overlay",
virt_text = { { symbol_text, utils.set_hl(hl) } },

hl_mode = "combine"
});
hl_mode = "combine"
});
end
end
end

Expand Down
15 changes: 14 additions & 1 deletion test/latex.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,21 @@ Some math in a line, $1+2=3$.

$$
a_s
b_\alpha
b_\delta
b_\theta
b_{\alpha}
b_{some text + \gamma}
$$

; Superscripts

$$
c^5
d^\alpha
d^\delta
d^\theta
d^{\alpha}
d^{Some other text + \alpha}
$$

Expand All @@ -59,10 +67,15 @@ $$
\Alpha \Beta \Gamma \Delta \Eta \Theta \Xi \Pi
$$

; Mixed expression

$$
M_\alpha = \int dq, q^2 g_\alpha(q) x_\alpha(q)
$$

; Text mode

$$
\text{a^2 + 2ab + b^2}
a^2 + 2ab + b^2
$$