MCPcopy
hub / github.com/markmap/markmap / transform

Function transform

packages/markmap-lib/src/plugins/source-lines/index.ts:9–49  ·  view source on GitHub ↗
(transformHooks: ITransformHooks)

Source from the content-addressed store, hash-verified

7const plugin = definePlugin({
8 name,
9 transform(transformHooks: ITransformHooks) {
10 let frontmatterLines = 0;
11 transformHooks.beforeParse.tap((_md, context) => {
12 frontmatterLines = context.frontmatterInfo?.lines || 0;
13 });
14 transformHooks.parser.tap((md) => {
15 md.renderer.renderAttrs = wrapFunction(
16 md.renderer.renderAttrs,
17 (renderAttrs, token) => {
18 if (token.block && token.map) {
19 const lineRange = token.map.map((line) => line + frontmatterLines);
20 token.attrSet('data-lines', lineRange.join(','));
21 }
22 return renderAttrs(token);
23 },
24 );
25
26 if (md.renderer.rules.fence) {
27 // In markdown-it, fences with language info are hard-coded as
28 // `<pre><code ...>`, so we have to modify the result string directly.
29 md.renderer.rules.fence = wrapFunction(
30 md.renderer.rules.fence,
31 (fence, tokens, idx, ...rest) => {
32 let result = fence(tokens, idx, ...rest);
33 const token = tokens[idx];
34 if (result.startsWith('<pre>') && token.map) {
35 const lineRange = token.map.map(
36 (line) => line + frontmatterLines,
37 );
38 result =
39 result.slice(0, 4) +
40 ` data-lines="${lineRange.join(',')}"` +
41 result.slice(4);
42 }
43 return result;
44 },
45 );
46 }
47 });
48 return {};
49 },
50});
51
52export default plugin;

Callers

nothing calls this directly

Calls 2

wrapFunctionFunction · 0.90
tapMethod · 0.80

Tested by

no test coverage detected