0 ? state.src.charCodeAt(pos - 1) : -1;
const nextChar = pos + 1 <= max ? state.src.charCodeAt(pos + 1) : -1;
if (prevChar === 32 || prevChar === 9 || /* \t */
nextChar >= 48 && nextChar <= 57)
can_close = false;
if (nextChar === 32 || nextChar === 9)
can_open = false;
return {
can_open,
can_close
};
}
function math_inline(state, silent) {
let match, token, res, pos;
if (state.src[state.pos] !== "$")
return false;
res = isValidDelim(state, state.pos);
if (!res.can_open) {
if (!silent)
state.pending += "$";
state.pos += 1;
return true;
}
const start = state.pos + 1;
match = start;
while ((match = state.src.indexOf("$", match)) !== -1) {
pos = match - 1;
while (state.src[pos] === "\\") pos -= 1;
if ((match - pos) % 2 === 1)
break;
match += 1;
}
if (match === -1) {
if (!silent)
state.pending += "$";
state.pos = start;
return true;
}
if (match - start === 0) {
if (!silent)
state.pending += "$$";
state.pos = start + 1;
return true;
}
res = isValidDelim(state, match);
if (!res.can_close) {
if (!silent)
state.pending += "$";
state.pos = start;
return true;
}
if (!silent) {
token = state.push("math_inline", "math", 0);
token.markup = "$";
token.content = state.src.slice(start, match);
}
state.pos = match + 1;
return true;
}
function math_block(state, start, end, silent) {
let firstLine;
let lastLine;
let next;
let lastPos;
let found = false;
let pos = state.bMarks[start] + state.tShift[start];
let max = state.eMarks[start];
if (pos + 2 > max)
return false;
if (state.src.slice(pos, pos + 2) !== "$$")
return false;
pos += 2;
firstLine = state.src.slice(pos, max);
if (silent)
return true;
if (firstLine.trim().slice(-2) === "$$") {
firstLine = firstLine.trim().slice(0, -2);
found = true;
}
for (next = start; !found; ) {
next++;
if (next >= end)
break;
pos = state.bMarks[next] + state.tShift[next];
max = state.eMarks[next];
if (pos < max && state.tShift[next] < state.blkIndent) {
break;
}
if (state.src.slice(pos, max).trim().slice(-2) === "$$") {
lastPos = state.src.slice(0, max).lastIndexOf("$$");
lastLine = state.src.slice(pos, lastPos);
found = true;
}
}
state.line = next + 1;
const token = state.push("math_block", "math", 0);
token.block = true;
token.content = (firstLine && firstLine.trim() ? `${firstLine}
` : "") + state.getLines(start + 1, next, state.tShift[start], true) + (lastLine && lastLine.trim() ? lastLine : "");
token.map = [start, state.line];
token.markup = "$$";
return true;
}
function MarkdownItKatex(md, options) {
const katexInline = function(latex) {
options.displayMode = false;
try {
return katex.renderToString(latex, options);
} catch (error) {
if (options.throwOnError)
console.warn(error);
return latex;
}
};
const inlineRenderer = function(tokens, idx) {
return katexInline(tokens[idx].content);
};
const katexBlock = function(latex) {
options.displayMode = true;
try {
return `${katex.renderToString(latex, options)}
`;
} catch (error) {
if (options.throwOnError)
console.warn(error);
return latex;
}
};
const blockRenderer = function(tokens, idx) {
return `${katexBlock(tokens[idx].content)}
`;
};
md.inline.ruler.after("escape", "math_inline", math_inline);
md.block.ruler.after("blockquote", "math_block", math_block, {
alt: ["paragraph", "reference", "blockquote", "list"]
});
md.renderer.rules.math_inline = inlineRenderer;
md.renderer.rules.math_block = blockRenderer;
}
// node/syntax/markdown-it/markdown-it-shiki.ts
import { isTruthy } from "@antfu/utils";
import { fromHighlighter } from "@shikijs/markdown-it/core";
// node/syntax/transform/utils.ts
function normalizeRangeStr(rangeStr = "") {
return !rangeStr.trim() ? [] : rangeStr.trim().split(/\|/g).map((i) => i.trim());
}
function getCodeBlocks(md) {
const codeblocks = Array.from(md.matchAll(/^```[\s\S]*?^```/gm)).map((m) => {
const start = m.index;
const end = m.index + m[0].length;
const startLine = md.slice(0, start).match(/\n/g)?.length || 0;
const endLine = md.slice(0, end).match(/\n/g)?.length || 0;
return [start, end, startLine, endLine];
});
return {
codeblocks,
isInsideCodeblocks(idx) {
return codeblocks.some(([s, e]) => s <= idx && idx <= e);
},
isLineInsideCodeblocks(line) {
return codeblocks.some(([, , s, e]) => s <= line && line <= e);
}
};
}
function escapeVueInCode(md) {
return md.replace(/\{\{/g, "{{");
}
// node/syntax/markdown-it/markdown-it-shiki.ts
async function MarkdownItShiki({ data: { config }, mode, utils }) {
const transformers = [
...utils.shikiOptions.transformers || [],
(config.twoslash === true || config.twoslash === mode) && (await import("@shikijs/vitepress-twoslash")).transformerTwoslash({
explicitTrigger: true,
twoslashOptions: {
handbookOptions: {
noErrorValidation: true
}
}
}),
{
pre(pre) {
this.addClassToHast(pre, "slidev-code");
delete pre.properties.tabindex;
},
postprocess(code) {
return escapeVueInCode(code);
}
}
].filter(isTruthy);
return fromHighlighter(utils.shiki, {
...utils.shikiOptions,
transformers
});
}
// node/syntax/markdown-it/markdown-it-v-drag.ts
import { SourceMapConsumer } from "source-map-js";
var dragComponentRegex = /<(v-?drag-?\w*)([\s>])/i;
var dragDirectiveRegex = /(? smc.originalPositionFor({ line: line + 1, column: 0 }).line - 1 : (line) => line;
function toMarkdownSource(map, idx) {
const start = toOriginalPos(map[0]);
const end = toOriginalPos(map[1]);
return `[${start},${Math.max(start + 1, end)},${idx}]`;
}
function replaceChildren(token, regex, replacement) {
for (const child of token.children ?? []) {
if (child.type === "html_block" || child.type === "html_inline") {
child.content = child.content.replace(regex, replacement);
}
replaceChildren(child, regex, replacement);
}
}
return _parse.call(this, src, env).map((token) => {
if (!["html_block", "html_inline", "inline"].includes(token.type) || !token.content.includes("drag") || visited.has(token))
return token;
token.content = token.content.replace(dragComponentRegex, (_, tag, space, idx) => {
const replacement = `<${tag} :markdownSource="${toMarkdownSource(token.map, idx)}"${space}`;
replaceChildren(token, dragComponentRegex, replacement);
return replacement;
}).replace(dragDirectiveRegex, (_, value, idx) => {
const replacement = `v-drag${value ?? ""} :markdownSource="${toMarkdownSource(token.map, idx)}"`;
replaceChildren(token, dragDirectiveRegex, replacement);
return replacement;
});
visited.add(token);
return token;
});
};
}
// node/syntax/markdown-it/index.ts
async function useMarkdownItPlugins(md, options, markdownTransformMap) {
const { roots, data: { features, config } } = options;
if (config.highlighter === "shiki") {
md.use(await MarkdownItShiki(options));
}
md.use(MarkdownItLink);
md.use(MarkdownItEscapeInlineCode);
md.use(MarkdownItFootnote);
md.use(taskLists, { enabled: true, lineNumber: true, label: true });
if (features.katex)
md.use(MarkdownItKatex, await setupKatex(roots));
md.use(MarkdownItVDrag, markdownTransformMap);
if (config.mdc)
md.use(MarkdownItMdc);
}
// node/setups/transformers.ts
async function setupTransformers(roots) {
const returns = await loadSetups(roots, "transformers.ts", []);
const result = {
pre: [],
preCodeblock: [],
postCodeblock: [],
post: []
};
for (const r of [...returns].reverse()) {
if (r.pre)
result.pre.push(...r.pre);
if (r.preCodeblock)
result.preCodeblock.push(...r.preCodeblock);
}
for (const r of returns) {
if (r.postCodeblock)
result.postCodeblock.push(...r.postCodeblock);
if (r.post)
result.post.push(...r.post);
}
return result;
}
// node/syntax/transform/code-wrapper.ts
var reCodeBlock = /^```([\w'-]+)?\s*(?:\{([\w*,|-]+)\}\s*?(\{[^}]*\})?([^\r\n]*))?\r?\n([ \t]*\S[\s\S]*?)^```$/gm;
function transformCodeWrapper(ctx) {
ctx.s.replace(
reCodeBlock,
(full, lang = "", rangeStr = "", options = "", attrs = "", code) => {
const ranges = normalizeRangeStr(rangeStr);
code = code.trimEnd();
options = options.trim() || "{}";
return `
\`\`\`${lang}${attrs}
${code}
\`\`\`
`;
}
);
}
// node/syntax/transform/in-page-css.ts
function transformPageCSS(ctx) {
const codeBlocks = getCodeBlocks(ctx.s.original);
ctx.s.replace(
/(\n