0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2026-03-26 12:38:11 +00:00

subscript and superscript

This commit is contained in:
Víctor Losada Hernández
2026-03-23 23:55:35 +01:00
parent c76b174fd6
commit 02ae7176f7
2 changed files with 49 additions and 11 deletions

View File

@@ -64,18 +64,30 @@ const customHighlightPlugin = ViewPlugin.fromClass(
} }
} }
buildDecorations(view) { buildDecorations(view) {
const widgets = []; const decos = [];
const tokens = tokenizeCustomMarkdown(view.state.doc.toString()); const tokens = tokenizeCustomMarkdown(view.state.doc.toString());
// sort by line number
tokens.sort((a, b) => a.line - b.line);
tokens.forEach((tok) => { tokens.forEach((tok) => {
const line = view.state.doc.line(tok.line + 1); // CM lines are 1-based const line = view.state.doc.line(tok.line + 1);
widgets.push(Decoration.line({ class: `cm-${tok.type}` }).range(line.from));
if (tok.from != null && tok.to != null) {
decos.push({
from: line.from + tok.from,
to: line.from + tok.to,
deco: Decoration.mark({ class: `cm-${tok.type}` }),
});
} else {
decos.push({
from: line.from,
to: line.from,
deco: Decoration.line({ class: `cm-${tok.type}` }),
});
}
}); });
return Decoration.set(widgets); decos.sort((a, b) => a.from - b.from || a.to - b.to);
return Decoration.set(decos.map((d) => d.deco.range(d.from, d.to)));
} }
}, },
{ {

View File

@@ -36,9 +36,37 @@ export function tokenizeCustomMarkdown(text) {
if (/:\w+?:/.test(lineText)) tokens.push({ line: lineNumber, type: customTags.emoji }); if (/:\w+?:/.test(lineText)) tokens.push({ line: lineNumber, type: customTags.emoji });
// --- Superscript / Subscript --- // --- Superscript / Subscript ---
if (/\^\^/.test(lineText)) tokens.push({ line: lineNumber, type: customTags.subscript }); if (/\^/.test(lineText)) {
if (/\^/.test(lineText)) tokens.push({ line: lineNumber, type: customTags.superscript }); let startIndex = lineText.indexOf("^");
const superRegex = /\^(?!\s)(?=([^\n\^]*[^\s\^]))\1\^/gy;
const subRegex = /\^\^(?!\s)(?=([^\n\^]*[^\s\^]))\1\^\^/gy;
while (startIndex >= 0) {
superRegex.lastIndex = subRegex.lastIndex = startIndex;
let match = subRegex.exec(lineText);
let type = customTags.subscript;
if (!match) {
match = superRegex.exec(lineText);
type = customTags.superscript;
}
if (match) {
tokens.push({
line: lineNumber,
type,
from: match.index,
to: match.index + match[0].length,
});
}
startIndex = lineText.indexOf(
"^",
Math.max(startIndex + 1, superRegex.lastIndex || 0, subRegex.lastIndex || 0),
);
}
};
// --- Definition lists --- // --- Definition lists ---
if (/::/.test(lineText)) { if (/::/.test(lineText)) {
tokens.push({ line: lineNumber, type: customTags.definitionDesc }); tokens.push({ line: lineNumber, type: customTags.definitionDesc });
@@ -78,8 +106,6 @@ export function tokenizeCustomMarkdown(text) {
tokens.push({ line: lineNumber, type: customTags.block }); tokens.push({ line: lineNumber, type: customTags.block });
inBlock = false; inBlock = false;
} }
}); });
return tokens; return tokens;