@terrafirmagreg-team/mdx 1.2.6 → 1.2.7
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/package.json +3 -6
- package/src/lib/markdown/index.ts +0 -1
- package/src/lib/markdown/dom.ts +0 -104
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@terrafirmagreg-team/mdx",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.7",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "MDX/markdown pipeline",
|
|
6
6
|
"license": "LGPL-3.0-only",
|
|
@@ -22,9 +22,8 @@
|
|
|
22
22
|
"dependencies": {
|
|
23
23
|
"@fumadocs/story": "^1.5.3",
|
|
24
24
|
"@fumari/stf": "^1.1.2",
|
|
25
|
-
"@terrafirmagreg-team/translate": "^1.2.
|
|
26
|
-
"@terrafirmagreg-team/ui": "^1.2.
|
|
27
|
-
"@types/turndown": "^5.0.6",
|
|
25
|
+
"@terrafirmagreg-team/translate": "^1.2.8",
|
|
26
|
+
"@terrafirmagreg-team/ui": "^1.2.9",
|
|
28
27
|
"fumadocs-core": "16.17.10",
|
|
29
28
|
"fumadocs-docgen": "^3.2.0",
|
|
30
29
|
"fumadocs-twoslash": "^4.0.4",
|
|
@@ -38,8 +37,6 @@
|
|
|
38
37
|
"remark-gfm": "^4.0.1",
|
|
39
38
|
"remark-math": "^6.0.0",
|
|
40
39
|
"remark-parse": "^11.0.0",
|
|
41
|
-
"turndown": "^7.2.5",
|
|
42
|
-
"turndown-plugin-gfm": "^1.0.2",
|
|
43
40
|
"unified": "^11.0.5",
|
|
44
41
|
"unist-util-visit": "^5.2.0"
|
|
45
42
|
},
|
package/src/lib/markdown/dom.ts
DELETED
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
'use client';
|
|
2
|
-
import type TurndownService from 'turndown';
|
|
3
|
-
|
|
4
|
-
function decodedUrl(raw: string): string {
|
|
5
|
-
const url = raw.replace(/(?:%[0-9A-Fa-f]{2})+/g, (run) => {
|
|
6
|
-
let decoded: string;
|
|
7
|
-
try {
|
|
8
|
-
decoded = decodeURIComponent(run);
|
|
9
|
-
} catch {
|
|
10
|
-
return run;
|
|
11
|
-
}
|
|
12
|
-
return /[/?#&=%+]/.test(decoded) ? run : decoded;
|
|
13
|
-
});
|
|
14
|
-
return /[\s<>()]/.test(url) ? `<${url}>` : url;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
let servicePromise: Promise<TurndownService> | undefined;
|
|
18
|
-
|
|
19
|
-
function turndownService(): Promise<TurndownService> {
|
|
20
|
-
servicePromise ??= (async () => {
|
|
21
|
-
const [{ default: TurndownServiceImpl }, { gfm }] = await Promise.all([
|
|
22
|
-
import('turndown'),
|
|
23
|
-
// @ts-expect-error -- no published type declarations
|
|
24
|
-
import('turndown-plugin-gfm'),
|
|
25
|
-
]);
|
|
26
|
-
const service = new TurndownServiceImpl({
|
|
27
|
-
headingStyle: 'atx',
|
|
28
|
-
codeBlockStyle: 'fenced',
|
|
29
|
-
bulletListMarker: '-',
|
|
30
|
-
});
|
|
31
|
-
service.use(gfm);
|
|
32
|
-
service.remove([
|
|
33
|
-
'script',
|
|
34
|
-
'style',
|
|
35
|
-
'noscript',
|
|
36
|
-
'template',
|
|
37
|
-
'button',
|
|
38
|
-
'select',
|
|
39
|
-
'nav',
|
|
40
|
-
'aside',
|
|
41
|
-
'footer',
|
|
42
|
-
]);
|
|
43
|
-
service.remove(
|
|
44
|
-
(node) =>
|
|
45
|
-
node.nodeName === 'SVG' ||
|
|
46
|
-
(node instanceof HTMLImageElement &&
|
|
47
|
-
(node.src.startsWith('data:') ||
|
|
48
|
-
(node.width > 0 && node.width <= 48))) ||
|
|
49
|
-
(node instanceof HTMLElement && node.dataset.mdSkip !== undefined),
|
|
50
|
-
);
|
|
51
|
-
service.addRule('decodedLink', {
|
|
52
|
-
filter: 'a',
|
|
53
|
-
replacement(content, node) {
|
|
54
|
-
const anchor = node as HTMLAnchorElement;
|
|
55
|
-
const href = anchor.getAttribute('href');
|
|
56
|
-
if (!href) return content;
|
|
57
|
-
const title = anchor.getAttribute('title');
|
|
58
|
-
const titlePart = title ? ` "${title.replace(/"/g, '\\"')}"` : '';
|
|
59
|
-
return `[${content}](${decodedUrl(href)}${titlePart})`;
|
|
60
|
-
},
|
|
61
|
-
});
|
|
62
|
-
service.addRule('decodedImage', {
|
|
63
|
-
filter: 'img',
|
|
64
|
-
replacement(_content, node) {
|
|
65
|
-
const image = node as HTMLImageElement;
|
|
66
|
-
const src = image.getAttribute('src');
|
|
67
|
-
if (!src) return '';
|
|
68
|
-
const alt = image.getAttribute('alt') ?? '';
|
|
69
|
-
const title = image.getAttribute('title');
|
|
70
|
-
const titlePart = title ? ` "${title.replace(/"/g, '\\"')}"` : '';
|
|
71
|
-
return `}${titlePart})`;
|
|
72
|
-
},
|
|
73
|
-
});
|
|
74
|
-
return service;
|
|
75
|
-
})();
|
|
76
|
-
return servicePromise;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
export async function domToMarkdown(
|
|
80
|
-
root: Element | null | undefined,
|
|
81
|
-
): Promise<string> {
|
|
82
|
-
if (!root) return '';
|
|
83
|
-
return (await turndownService()).turndown(root as HTMLElement);
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
export function pageMarkdown(
|
|
87
|
-
root: Element | null | undefined,
|
|
88
|
-
title: string,
|
|
89
|
-
): Promise<string> {
|
|
90
|
-
return domToMarkdown(root).then((body) => `# ${title}\n\n${body}`);
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
export async function openPageMarkdown(
|
|
94
|
-
root: Element | null | undefined,
|
|
95
|
-
title: string,
|
|
96
|
-
): Promise<void> {
|
|
97
|
-
const markdown = await pageMarkdown(root, title);
|
|
98
|
-
const url = URL.createObjectURL(
|
|
99
|
-
new Blob([markdown], { type: 'text/markdown;charset=utf-8' }),
|
|
100
|
-
);
|
|
101
|
-
window.open(url, '_blank', 'noopener');
|
|
102
|
-
setTimeout(() => URL.revokeObjectURL(url), 60_000);
|
|
103
|
-
}
|
|
104
|
-
|