Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | 1x 1x 8x 8x 1x | /**
* critic markup HTML LaTeX
* {--[text]--} <del>[text]</del> \st{[text]}
* {++[text]++} <ins>[text]</ins> \underline{[text]}
* {~~[text1]~>[text2]~~} <del>[text1]</del><ins>[text2]</ins> \st{[text1]}\underline{[text2]}
* {==[text]==} <mark>[text]</mark> \hl{[text]}
* {>>[text]<<} <aside>[text]</aside> \marginpar{[text]}
*/
// tslint:disable-next-line no-implicit-dependencies
import { MarkdownIt } from "markdown-it";
import { MarkdownEngineConfig } from "../markdown-engine-config";
export default (md: MarkdownIt, config: MarkdownEngineConfig) => {
// @ts-ignore
md.inline.ruler.before("strikethrough", "critic-markup", (state, silent) => {
Eif (!config.enableCriticMarkupSyntax) {
return false;
}
const { src, pos } = state;
if (
src[pos] === "{" &&
((src[pos + 1] === "-" && src[pos + 2] === "-") ||
(src[pos + 1] === "+" && src[pos + 2] === "+") ||
(src[pos + 1] === "~" && src[pos + 2] === "~") ||
(src[pos + 1] === "=" && src[pos + 2] === "=") ||
(src[pos + 1] === ">" && src[pos + 2] === ">"))
) {
const tag = src.slice(pos + 1, pos + 3);
const closeTag = tag[0] === ">" ? "<<}" : `${tag}}`;
let i = pos + 3;
let end = -1;
let content = null;
while (i < src.length) {
if (src.startsWith(closeTag, i)) {
end = i;
break;
}
i += 1;
}
if (end >= 0) {
content = src.slice(pos + 3, end);
} else {
return false;
}
if (content && !silent) {
const token = state.push("critic-markup");
token.content = content;
token.tag = tag;
state.pos = end + closeTag.length;
return true;
} else {
return false;
}
} else {
return false;
}
});
/**
* CriticMarkup renderer
*/
md.renderer.rules["critic-markup"] = (tokens, idx) => {
const token = tokens[idx];
const tag = token.tag;
const content = token.content;
if (tag === "--") {
return `<del>${content}</del>`;
} else if (tag === "++") {
return `<ins>${content}</ins>`;
} else if (tag === "==") {
return `<mark>${content}</mark>`;
} else if (tag === ">>") {
return `<span style="display:none">${content}</span>`;
} else {
// {~~[text1]~>[text2]~~}
const arr = content.split("~>");
if (arr.length === 2) {
return `<del>${arr[0]}</del><ins>${arr[1]}</ins>`;
} else {
return `<code>Error: ~> not found.</code>`;
}
}
};
};
|