@vmz/plugin-markdown-it 0.3.12 → 0.3.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/runtime.d.ts +23 -1
- package/dist/runtime.js +46 -2
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -11,7 +11,7 @@ link, and fence analysis for build-time document work.
|
|
|
11
11
|
The package depends on `markdown-it` directly, so no separate peer install is required.
|
|
12
12
|
|
|
13
13
|
```ts
|
|
14
|
-
import { defineConfig } from '@vmz/
|
|
14
|
+
import { defineConfig } from '@vmz/vmz';
|
|
15
15
|
import markdownIt from '@vmz/plugin-markdown-it';
|
|
16
16
|
|
|
17
17
|
export default defineConfig({
|
package/dist/runtime.d.ts
CHANGED
|
@@ -8,6 +8,28 @@ export type MarkdownItRuntimeOptions = {
|
|
|
8
8
|
/** Convert single newlines to `<br>` (default `false`). */
|
|
9
9
|
breaks?: boolean;
|
|
10
10
|
};
|
|
11
|
+
export type MarkdownAnalyzeHooks = {
|
|
12
|
+
/**
|
|
13
|
+
* Build-time fence highlighter (sync). Return value is injected as the
|
|
14
|
+
* fence's inner HTML by markdown-it; return '' to fall back to escaped code.
|
|
15
|
+
*/
|
|
16
|
+
highlight?: (code: string, lang: string) => string;
|
|
17
|
+
/**
|
|
18
|
+
* Rewrite an author link href (e.g. `.md` relatives → site routes).
|
|
19
|
+
* Return `null`/`undefined` to keep the original href.
|
|
20
|
+
*/
|
|
21
|
+
resolveHref?: (href: string) => string | null | undefined;
|
|
22
|
+
/**
|
|
23
|
+
* Build-time heading anchor. Return the anchor href (usually `#<id>`) to
|
|
24
|
+
* append a self-link inside the heading; `null`/`undefined` skips it.
|
|
25
|
+
* The anchor is real markup — usable without JavaScript.
|
|
26
|
+
*/
|
|
27
|
+
headingAnchor?: (heading: {
|
|
28
|
+
id: string;
|
|
29
|
+
text: string;
|
|
30
|
+
level: number;
|
|
31
|
+
}) => string | null | undefined;
|
|
32
|
+
};
|
|
11
33
|
/**
|
|
12
34
|
* Configure the renderer before use (also called by the `markdownIt()` plugin factory).
|
|
13
35
|
* Later calls rebuild the cached instance.
|
|
@@ -29,7 +51,7 @@ export declare function renderMarkdown(source: any): any;
|
|
|
29
51
|
* fences: Array<{ lang: string, info: string, content: string, lineStart: number, lineEnd: number }>
|
|
30
52
|
* }}
|
|
31
53
|
*/
|
|
32
|
-
export declare function analyzeMarkdown(source: any): {
|
|
54
|
+
export declare function analyzeMarkdown(source: any, hooks?: MarkdownAnalyzeHooks): {
|
|
33
55
|
html: any;
|
|
34
56
|
headings: any[];
|
|
35
57
|
links: any[];
|
package/dist/runtime.js
CHANGED
|
@@ -34,6 +34,18 @@ function createMd() {
|
|
|
34
34
|
cached = new MarkdownIt(options);
|
|
35
35
|
return cached;
|
|
36
36
|
}
|
|
37
|
+
/**
|
|
38
|
+
* Hooked instance for build-time document work: a dedicated MarkdownIt with the
|
|
39
|
+
* `highlight` option wired, never the shared cached one (hooks are per-call).
|
|
40
|
+
*/
|
|
41
|
+
function createHookedMd(hooks) {
|
|
42
|
+
const md = new MarkdownIt(options);
|
|
43
|
+
if (hooks.highlight) {
|
|
44
|
+
const highlight = hooks.highlight;
|
|
45
|
+
md.options.highlight = (str, lang) => highlight(str, lang);
|
|
46
|
+
}
|
|
47
|
+
return md;
|
|
48
|
+
}
|
|
37
49
|
/**
|
|
38
50
|
* @param {string} source
|
|
39
51
|
* @returns {string} HTML fragment (no wrapping document)
|
|
@@ -51,9 +63,24 @@ export function renderMarkdown(source) {
|
|
|
51
63
|
* fences: Array<{ lang: string, info: string, content: string, lineStart: number, lineEnd: number }>
|
|
52
64
|
* }}
|
|
53
65
|
*/
|
|
54
|
-
export function analyzeMarkdown(source) {
|
|
55
|
-
const md = createMd();
|
|
66
|
+
export function analyzeMarkdown(source, hooks) {
|
|
67
|
+
const md = hooks ? createHookedMd(hooks) : createMd();
|
|
56
68
|
const tokens = md.parse(String(source ?? ''), {});
|
|
69
|
+
if (hooks?.resolveHref) {
|
|
70
|
+
const resolveHref = hooks.resolveHref;
|
|
71
|
+
for (const t of tokens) {
|
|
72
|
+
if (t.type !== 'inline' || !Array.isArray(t.children))
|
|
73
|
+
continue;
|
|
74
|
+
for (const c of t.children) {
|
|
75
|
+
if (c.type !== 'link_open')
|
|
76
|
+
continue;
|
|
77
|
+
const href = c.attrGet('href') || '';
|
|
78
|
+
const rewritten = resolveHref(href);
|
|
79
|
+
if (typeof rewritten === 'string')
|
|
80
|
+
c.attrSet('href', rewritten);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
57
84
|
/** @type {Array<{ level: number, id: string, text: string }>} */
|
|
58
85
|
const headings = [];
|
|
59
86
|
/** @type {Array<{ href: string, text: string }>} */
|
|
@@ -78,6 +105,23 @@ export function analyzeMarkdown(source) {
|
|
|
78
105
|
headings.push({ level, id, text });
|
|
79
106
|
// Inject id into open token attrs for render.
|
|
80
107
|
t.attrSet('id', id);
|
|
108
|
+
if (hooks?.headingAnchor && inline && inline.type === 'inline' && Array.isArray(inline.children)) {
|
|
109
|
+
const href = hooks.headingAnchor({ id, text, level });
|
|
110
|
+
if (typeof href === 'string' && href) {
|
|
111
|
+
const Token = t.constructor;
|
|
112
|
+
const open = new Token('link_open', 'a', 1);
|
|
113
|
+
open.attrs = [
|
|
114
|
+
['href', href],
|
|
115
|
+
['class', 'h-anchor'],
|
|
116
|
+
['aria-hidden', 'true'],
|
|
117
|
+
['tabindex', '-1'],
|
|
118
|
+
];
|
|
119
|
+
const label = new Token('text', '', 0);
|
|
120
|
+
label.content = '#';
|
|
121
|
+
const close = new Token('link_close', 'a', -1);
|
|
122
|
+
inline.children.push(open, label, close);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
81
125
|
}
|
|
82
126
|
if (t.type === 'fence') {
|
|
83
127
|
const info = String(t.info || '').trim();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"dependencies": {
|
|
3
|
-
"@vmz/plugin": "0.3.
|
|
3
|
+
"@vmz/plugin": "0.3.13",
|
|
4
4
|
"markdown-it": "^14.1.0"
|
|
5
5
|
},
|
|
6
6
|
"description": "VMZ markdown-it adapter — MarkdownIt + engines.markdown registration",
|
|
@@ -49,5 +49,5 @@
|
|
|
49
49
|
},
|
|
50
50
|
"type": "module",
|
|
51
51
|
"types": "./dist/vmz.plugin.d.ts",
|
|
52
|
-
"version": "0.3.
|
|
52
|
+
"version": "0.3.13"
|
|
53
53
|
}
|