@o2ter/frosty-markdown 1.0.0
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/LICENSE +21 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +169 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +165 -0
- package/dist/index.mjs.map +1 -0
- package/dist/index.server.js +169 -0
- package/dist/index.server.js.map +1 -0
- package/dist/index.server.mjs +165 -0
- package/dist/index.server.mjs.map +1 -0
- package/package.json +47 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 - 2026 O2ter Limited. All rights reserved.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { ElementNode, ComponentNode } from 'frosty';
|
|
2
|
+
import { PluggableList } from 'unified';
|
|
3
|
+
import { Options as Options$1 } from 'remark-rehype';
|
|
4
|
+
|
|
5
|
+
type UrlTransform = (value: string, key: string, node: any) => string;
|
|
6
|
+
interface Options {
|
|
7
|
+
children?: string;
|
|
8
|
+
rehypePlugins?: PluggableList;
|
|
9
|
+
remarkPlugins?: PluggableList;
|
|
10
|
+
remarkRehypeOptions?: Options$1;
|
|
11
|
+
allowedElements?: string[];
|
|
12
|
+
allowElement?: (node: any, index: number, parent: any) => boolean;
|
|
13
|
+
components?: Record<string, any>;
|
|
14
|
+
disallowedElements?: string[];
|
|
15
|
+
skipHtml?: boolean;
|
|
16
|
+
unwrapDisallowed?: boolean;
|
|
17
|
+
urlTransform?: UrlTransform;
|
|
18
|
+
fallback?: ElementNode;
|
|
19
|
+
}
|
|
20
|
+
interface HooksOptions extends Options {
|
|
21
|
+
fallback?: ElementNode;
|
|
22
|
+
}
|
|
23
|
+
declare function Markdown(options: Readonly<Options>): ComponentNode;
|
|
24
|
+
declare function MarkdownAsync(options: Readonly<HooksOptions>): ElementNode;
|
|
25
|
+
declare function defaultUrlTransform(value: string): string;
|
|
26
|
+
|
|
27
|
+
export { Markdown, MarkdownAsync, defaultUrlTransform };
|
|
28
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sources":["../src/index.ts"],"mappings":";;;;AAGA,KAAK,YAAY;AACjB,UAAU,OAAO;AACjB;AACA,oBAAoB,aAAa;AACjC,oBAAoB,aAAa;AACjC,0BAA0BA,SAAmB;AAC7C;AACA;AACA,iBAAiB,MAAM;AACvB;AACA;AACA;AACA,mBAAmB,YAAY;AAC/B,eAAe,WAAW;AAC1B;AACA,UAAU,YAAY,SAAS,OAAO;AACtC,eAAe,WAAW;AAC1B;AACA,iBAAiB,QAAQ,UAAU,QAAQ,CAAC,OAAO,IAAI,aAAa;AACpE,iBAAiB,aAAa,UAAU,QAAQ,CAAC,YAAY,IAAI,WAAW;AAC5E,iBAAiB,mBAAmB;;;;","names":["RemarkRehypeOptions"]}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var devlop = require('devlop');
|
|
4
|
+
var hastUtilToJsxRuntime = require('hast-util-to-jsx-runtime');
|
|
5
|
+
var htmlUrlAttributes = require('html-url-attributes');
|
|
6
|
+
var jsxRuntime = require('frosty/jsx-runtime');
|
|
7
|
+
var frosty = require('frosty');
|
|
8
|
+
var remarkParse = require('remark-parse');
|
|
9
|
+
var remarkRehype = require('remark-rehype');
|
|
10
|
+
var unified = require('unified');
|
|
11
|
+
var unistUtilVisit = require('unist-util-visit');
|
|
12
|
+
var vfile = require('vfile');
|
|
13
|
+
|
|
14
|
+
//
|
|
15
|
+
// index.ts
|
|
16
|
+
//
|
|
17
|
+
// The MIT License
|
|
18
|
+
// Copyright (c) 2021 - 2026 O2ter Limited. All rights reserved.
|
|
19
|
+
//
|
|
20
|
+
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
21
|
+
// of this software and associated documentation files (the "Software"), to deal
|
|
22
|
+
// in the Software without restriction, including without limitation the rights
|
|
23
|
+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
24
|
+
// copies of the Software, and to permit persons to whom the Software is
|
|
25
|
+
// furnished to do so, subject to the following conditions:
|
|
26
|
+
//
|
|
27
|
+
// The above copyright notice and this permission notice shall be included in
|
|
28
|
+
// all copies or substantial portions of the Software.
|
|
29
|
+
//
|
|
30
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
31
|
+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
32
|
+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
33
|
+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
34
|
+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
35
|
+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
36
|
+
// THE SOFTWARE.
|
|
37
|
+
//
|
|
38
|
+
const emptyPlugins = [];
|
|
39
|
+
const emptyRemarkRehypeOptions = { allowDangerousHtml: true };
|
|
40
|
+
const safeProtocol = /^(https?|ircs?|mailto|xmpp)$/i;
|
|
41
|
+
function Markdown(options) {
|
|
42
|
+
const processor = createProcessor(options);
|
|
43
|
+
const file = createFile(options);
|
|
44
|
+
return post(processor.runSync(processor.parse(file), file), options);
|
|
45
|
+
}
|
|
46
|
+
function MarkdownAsync(options) {
|
|
47
|
+
const processor = frosty.useMemo(() => createProcessor(options), [options.rehypePlugins, options.remarkPlugins, options.remarkRehypeOptions]);
|
|
48
|
+
const { resource: tree, error } = frosty.useResource(() => {
|
|
49
|
+
const file = createFile(options);
|
|
50
|
+
return processor.run(processor.parse(file), file);
|
|
51
|
+
}, [options.children, processor]);
|
|
52
|
+
if (error)
|
|
53
|
+
throw error;
|
|
54
|
+
return tree ? post(tree, options) : options.fallback;
|
|
55
|
+
}
|
|
56
|
+
function createProcessor(options) {
|
|
57
|
+
const rehypePlugins = options.rehypePlugins || emptyPlugins;
|
|
58
|
+
const remarkPlugins = options.remarkPlugins || emptyPlugins;
|
|
59
|
+
const remarkRehypeOptions = options.remarkRehypeOptions
|
|
60
|
+
? { ...options.remarkRehypeOptions, ...emptyRemarkRehypeOptions }
|
|
61
|
+
: emptyRemarkRehypeOptions;
|
|
62
|
+
const processor = unified.unified()
|
|
63
|
+
.use(remarkParse)
|
|
64
|
+
.use(remarkPlugins)
|
|
65
|
+
.use(remarkRehype, remarkRehypeOptions)
|
|
66
|
+
.use(rehypePlugins);
|
|
67
|
+
return processor;
|
|
68
|
+
}
|
|
69
|
+
function createFile(options) {
|
|
70
|
+
const children = options.children || '';
|
|
71
|
+
const file = new vfile.VFile();
|
|
72
|
+
if (typeof children === 'string') {
|
|
73
|
+
file.value = children;
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
devlop.unreachable('Unexpected value `' +
|
|
77
|
+
children +
|
|
78
|
+
'` for `children` prop, expected `string`');
|
|
79
|
+
}
|
|
80
|
+
return file;
|
|
81
|
+
}
|
|
82
|
+
function post(tree, options) {
|
|
83
|
+
const allowedElements = options.allowedElements;
|
|
84
|
+
const allowElement = options.allowElement;
|
|
85
|
+
const components = options.components;
|
|
86
|
+
const disallowedElements = options.disallowedElements;
|
|
87
|
+
const skipHtml = options.skipHtml;
|
|
88
|
+
const unwrapDisallowed = options.unwrapDisallowed;
|
|
89
|
+
const urlTransform = options.urlTransform || defaultUrlTransform;
|
|
90
|
+
if (allowedElements && disallowedElements) {
|
|
91
|
+
devlop.unreachable('Unexpected combined `allowedElements` and `disallowedElements`, expected one or the other');
|
|
92
|
+
}
|
|
93
|
+
unistUtilVisit.visit(tree, transform);
|
|
94
|
+
return hastUtilToJsxRuntime.toJsxRuntime(tree, {
|
|
95
|
+
Fragment: jsxRuntime.Fragment,
|
|
96
|
+
components,
|
|
97
|
+
ignoreInvalidStyle: true,
|
|
98
|
+
jsx: jsxRuntime.jsx,
|
|
99
|
+
jsxs: jsxRuntime.jsxs,
|
|
100
|
+
passKeys: true,
|
|
101
|
+
passNode: true
|
|
102
|
+
});
|
|
103
|
+
function transform(node, index, parent) {
|
|
104
|
+
if (node.type === 'raw' && parent && typeof index === 'number') {
|
|
105
|
+
if (skipHtml) {
|
|
106
|
+
parent.children.splice(index, 1);
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
parent.children[index] = { type: 'text', value: node.value };
|
|
110
|
+
}
|
|
111
|
+
return index;
|
|
112
|
+
}
|
|
113
|
+
if (node.type === 'element') {
|
|
114
|
+
let key;
|
|
115
|
+
for (key in htmlUrlAttributes.urlAttributes) {
|
|
116
|
+
if (Object.hasOwn(htmlUrlAttributes.urlAttributes, key) &&
|
|
117
|
+
Object.hasOwn(node.properties, key)) {
|
|
118
|
+
const value = node.properties[key];
|
|
119
|
+
const test = htmlUrlAttributes.urlAttributes[key];
|
|
120
|
+
if (test === null || test.includes(node.tagName)) {
|
|
121
|
+
node.properties[key] = urlTransform(String(value || ''), key, node);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
if (node.type === 'element') {
|
|
127
|
+
let remove = allowedElements
|
|
128
|
+
? !allowedElements.includes(node.tagName)
|
|
129
|
+
: disallowedElements
|
|
130
|
+
? disallowedElements.includes(node.tagName)
|
|
131
|
+
: false;
|
|
132
|
+
if (!remove && allowElement && typeof index === 'number') {
|
|
133
|
+
remove = !allowElement(node, index, parent);
|
|
134
|
+
}
|
|
135
|
+
if (remove && parent && typeof index === 'number') {
|
|
136
|
+
if (unwrapDisallowed && node.children) {
|
|
137
|
+
parent.children.splice(index, 1, ...node.children);
|
|
138
|
+
}
|
|
139
|
+
else {
|
|
140
|
+
parent.children.splice(index, 1);
|
|
141
|
+
}
|
|
142
|
+
return index;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
function defaultUrlTransform(value) {
|
|
148
|
+
const colon = value.indexOf(':');
|
|
149
|
+
const questionMark = value.indexOf('?');
|
|
150
|
+
const numberSign = value.indexOf('#');
|
|
151
|
+
const slash = value.indexOf('/');
|
|
152
|
+
if (
|
|
153
|
+
// If there is no protocol, it’s relative.
|
|
154
|
+
colon === -1 ||
|
|
155
|
+
// If the first colon is after a `?`, `#`, or `/`, it’s not a protocol.
|
|
156
|
+
(slash !== -1 && colon > slash) ||
|
|
157
|
+
(questionMark !== -1 && colon > questionMark) ||
|
|
158
|
+
(numberSign !== -1 && colon > numberSign) ||
|
|
159
|
+
// It is a protocol, it should be allowed.
|
|
160
|
+
safeProtocol.test(value.slice(0, colon))) {
|
|
161
|
+
return value;
|
|
162
|
+
}
|
|
163
|
+
return '';
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
exports.Markdown = Markdown;
|
|
167
|
+
exports.MarkdownAsync = MarkdownAsync;
|
|
168
|
+
exports.defaultUrlTransform = defaultUrlTransform;
|
|
169
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/index.ts"],"sourcesContent":["//\n// index.ts\n//\n// The MIT License\n// Copyright (c) 2021 - 2026 O2ter Limited. All rights reserved.\n//\n// Permission is hereby granted, free of charge, to any person obtaining a copy\n// of this software and associated documentation files (the \"Software\"), to deal\n// in the Software without restriction, including without limitation the rights\n// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n// copies of the Software, and to permit persons to whom the Software is\n// furnished to do so, subject to the following conditions:\n//\n// The above copyright notice and this permission notice shall be included in\n// all copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n// THE SOFTWARE.\n//\n\nimport { unreachable } from 'devlop';\nimport { toJsxRuntime } from 'hast-util-to-jsx-runtime';\nimport { urlAttributes } from 'html-url-attributes';\nimport { Fragment, jsx, jsxs } from 'frosty/jsx-runtime';\nimport { ComponentNode, ElementNode, useMemo, useResource } from 'frosty';\nimport remarkParse from 'remark-parse';\nimport remarkRehype from 'remark-rehype';\nimport { unified, Processor } from 'unified';\nimport { visit } from 'unist-util-visit';\nimport { VFile } from 'vfile';\n\nimport type { PluggableList } from 'unified';\nimport type { Root as MdastRoot } from 'mdast';\nimport type { Root } from 'hast';\nimport type { Options as RemarkRehypeOptions } from 'remark-rehype';\nimport type { BuildVisitor } from 'unist-util-visit';\n\ntype UrlTransform = (value: string, key: string, node: any) => string;\n\ninterface Options {\n children?: string;\n rehypePlugins?: PluggableList;\n remarkPlugins?: PluggableList;\n remarkRehypeOptions?: RemarkRehypeOptions;\n allowedElements?: string[];\n allowElement?: (node: any, index: number, parent: any) => boolean;\n components?: Record<string, any>;\n disallowedElements?: string[];\n skipHtml?: boolean;\n unwrapDisallowed?: boolean;\n urlTransform?: UrlTransform;\n fallback?: ElementNode;\n}\n\ninterface HooksOptions extends Options {\n fallback?: ElementNode;\n}\n\nconst emptyPlugins: PluggableList = [];\nconst emptyRemarkRehypeOptions: Readonly<RemarkRehypeOptions> = { allowDangerousHtml: true };\nconst safeProtocol = /^(https?|ircs?|mailto|xmpp)$/i;\n\nexport function Markdown(options: Readonly<Options>): ComponentNode {\n const processor = createProcessor(options);\n const file = createFile(options);\n return post(processor.runSync(processor.parse(file), file), options);\n}\n\nexport function MarkdownAsync(options: Readonly<HooksOptions>): ElementNode {\n const processor = useMemo(\n () => createProcessor(options),\n [options.rehypePlugins, options.remarkPlugins, options.remarkRehypeOptions]\n );\n const { resource: tree, error } = useResource(() => {\n const file = createFile(options);\n return processor.run(processor.parse(file), file);\n }, [options.children, processor]);\n if (error) throw error;\n return tree ? post(tree, options) : options.fallback;\n}\n\n\nfunction createProcessor(options: Readonly<Options>): Processor<MdastRoot, MdastRoot, Root, undefined, undefined> {\n const rehypePlugins = options.rehypePlugins || emptyPlugins;\n const remarkPlugins = options.remarkPlugins || emptyPlugins;\n const remarkRehypeOptions = options.remarkRehypeOptions\n ? { ...options.remarkRehypeOptions, ...emptyRemarkRehypeOptions }\n : emptyRemarkRehypeOptions;\n\n const processor = unified()\n .use(remarkParse)\n .use(remarkPlugins)\n .use(remarkRehype, remarkRehypeOptions)\n .use(rehypePlugins);\n\n return processor;\n}\n\n\nfunction createFile(options: Readonly<Options>): VFile {\n const children = options.children || '';\n const file = new VFile();\n\n if (typeof children === 'string') {\n file.value = children;\n } else {\n unreachable(\n 'Unexpected value `' +\n children +\n '` for `children` prop, expected `string`'\n );\n }\n\n return file;\n}\n\n\nfunction post(tree: any, options: Readonly<Options>): ComponentNode {\n const allowedElements = options.allowedElements;\n const allowElement = options.allowElement;\n const components = options.components;\n const disallowedElements = options.disallowedElements;\n const skipHtml = options.skipHtml;\n const unwrapDisallowed = options.unwrapDisallowed;\n const urlTransform = options.urlTransform || defaultUrlTransform;\n\n if (allowedElements && disallowedElements) {\n unreachable(\n 'Unexpected combined `allowedElements` and `disallowedElements`, expected one or the other'\n );\n }\n\n visit(tree, transform as BuildVisitor<Root>);\n\n return toJsxRuntime(tree, {\n Fragment,\n components,\n ignoreInvalidStyle: true,\n jsx,\n jsxs,\n passKeys: true,\n passNode: true\n });\n\n function transform(node: any, index: number, parent: any) {\n if (node.type === 'raw' && parent && typeof index === 'number') {\n if (skipHtml) {\n parent.children.splice(index, 1);\n } else {\n parent.children[index] = { type: 'text', value: node.value };\n }\n\n return index;\n }\n\n if (node.type === 'element') {\n let key: string;\n\n for (key in urlAttributes) {\n if (\n Object.hasOwn(urlAttributes, key) &&\n Object.hasOwn(node.properties, key)\n ) {\n const value = node.properties[key];\n const test = urlAttributes[key];\n if (test === null || test.includes(node.tagName)) {\n node.properties[key] = urlTransform(String(value || ''), key, node);\n }\n }\n }\n }\n\n if (node.type === 'element') {\n let remove = allowedElements\n ? !allowedElements.includes(node.tagName)\n : disallowedElements\n ? disallowedElements.includes(node.tagName)\n : false;\n\n if (!remove && allowElement && typeof index === 'number') {\n remove = !allowElement(node, index, parent);\n }\n\n if (remove && parent && typeof index === 'number') {\n if (unwrapDisallowed && node.children) {\n parent.children.splice(index, 1, ...node.children);\n } else {\n parent.children.splice(index, 1);\n }\n\n return index;\n }\n }\n }\n}\n\n\nexport function defaultUrlTransform(value: string): string {\n const colon = value.indexOf(':');\n const questionMark = value.indexOf('?');\n const numberSign = value.indexOf('#');\n const slash = value.indexOf('/');\n\n if (\n // If there is no protocol, it’s relative.\n colon === -1 ||\n // If the first colon is after a `?`, `#`, or `/`, it’s not a protocol.\n (slash !== -1 && colon > slash) ||\n (questionMark !== -1 && colon > questionMark) ||\n (numberSign !== -1 && colon > numberSign) ||\n // It is a protocol, it should be allowed.\n safeProtocol.test(value.slice(0, colon))\n ) {\n return value;\n }\n\n return '';\n}"],"names":["useMemo","useResource","unified","VFile","unreachable","visit","toJsxRuntime","Fragment","jsx","jsxs","urlAttributes"],"mappings":";;;;;;;;;;;;;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAwCA,MAAM,YAAY,GAAkB,EAAE;AACtC,MAAM,wBAAwB,GAAkC,EAAE,kBAAkB,EAAE,IAAI,EAAE;AAC5F,MAAM,YAAY,GAAG,+BAA+B;AAE9C,SAAU,QAAQ,CAAC,OAA0B,EAAA;AACjD,IAAA,MAAM,SAAS,GAAG,eAAe,CAAC,OAAO,CAAC;AAC1C,IAAA,MAAM,IAAI,GAAG,UAAU,CAAC,OAAO,CAAC;AAChC,IAAA,OAAO,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,SAAS,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,IAAI,CAAC,EAAE,OAAO,CAAC;AACtE;AAEM,SAAU,aAAa,CAAC,OAA+B,EAAA;IAC3D,MAAM,SAAS,GAAGA,cAAO,CACvB,MAAM,eAAe,CAAC,OAAO,CAAC,EAC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AAQ,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC,EAAE,GAAG,IAAI,CAAC,QAAQ,CAAC;gBACpD;qBAAO;oBACL,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC,CAAC;gBAClC;AAEA,gBAAA,OAAO,KAAK;YACd;QACF;IACF;AACF;AAGM,SAAU,mBAAmB,CAAC,KAAa,EAAA;IAC/C,MAAM,KAAK,GAAG,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC;IAChC,MAAM,YAAY,GAAG,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC;IACvC,MAAM,UAAU,GAAG,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC;IACrC,MAAM,KAAK,GAAG,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC;AAEhC,IAAA;;IAEE,KAAK,KAAK,EAAE;;SAEX,KAAK,KAAK,EAAE,IAAI,KAAK,GAAG,KAAK,CAAC;SAC9B,YAAY,KAAK,EAAE,IAAI,KAAK,GAAG,YAAY,CAAC;SAC5C,UAAU,KAAK,EAAE,IAAI,KAAK,GAAG,UAAU,CAAC;;AAEzC,QAAA,YAAY,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC,EACxC;AACA,QAAA,OAAO,KAAK;IACd;AAEA,IAAA,OAAO,EAAE;AACX;;;;;;"}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { unreachable } from 'devlop';
|
|
2
|
+
import { toJsxRuntime } from 'hast-util-to-jsx-runtime';
|
|
3
|
+
import { urlAttributes } from 'html-url-attributes';
|
|
4
|
+
import { jsxs, jsx, Fragment } from 'frosty/jsx-runtime';
|
|
5
|
+
import { useMemo, useResource } from 'frosty';
|
|
6
|
+
import remarkParse from 'remark-parse';
|
|
7
|
+
import remarkRehype from 'remark-rehype';
|
|
8
|
+
import { unified } from 'unified';
|
|
9
|
+
import { visit } from 'unist-util-visit';
|
|
10
|
+
import { VFile } from 'vfile';
|
|
11
|
+
|
|
12
|
+
//
|
|
13
|
+
// index.ts
|
|
14
|
+
//
|
|
15
|
+
// The MIT License
|
|
16
|
+
// Copyright (c) 2021 - 2026 O2ter Limited. All rights reserved.
|
|
17
|
+
//
|
|
18
|
+
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
19
|
+
// of this software and associated documentation files (the "Software"), to deal
|
|
20
|
+
// in the Software without restriction, including without limitation the rights
|
|
21
|
+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
22
|
+
// copies of the Software, and to permit persons to whom the Software is
|
|
23
|
+
// furnished to do so, subject to the following conditions:
|
|
24
|
+
//
|
|
25
|
+
// The above copyright notice and this permission notice shall be included in
|
|
26
|
+
// all copies or substantial portions of the Software.
|
|
27
|
+
//
|
|
28
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
29
|
+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
30
|
+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
31
|
+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
32
|
+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
33
|
+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
34
|
+
// THE SOFTWARE.
|
|
35
|
+
//
|
|
36
|
+
const emptyPlugins = [];
|
|
37
|
+
const emptyRemarkRehypeOptions = { allowDangerousHtml: true };
|
|
38
|
+
const safeProtocol = /^(https?|ircs?|mailto|xmpp)$/i;
|
|
39
|
+
function Markdown(options) {
|
|
40
|
+
const processor = createProcessor(options);
|
|
41
|
+
const file = createFile(options);
|
|
42
|
+
return post(processor.runSync(processor.parse(file), file), options);
|
|
43
|
+
}
|
|
44
|
+
function MarkdownAsync(options) {
|
|
45
|
+
const processor = useMemo(() => createProcessor(options), [options.rehypePlugins, options.remarkPlugins, options.remarkRehypeOptions]);
|
|
46
|
+
const { resource: tree, error } = useResource(() => {
|
|
47
|
+
const file = createFile(options);
|
|
48
|
+
return processor.run(processor.parse(file), file);
|
|
49
|
+
}, [options.children, processor]);
|
|
50
|
+
if (error)
|
|
51
|
+
throw error;
|
|
52
|
+
return tree ? post(tree, options) : options.fallback;
|
|
53
|
+
}
|
|
54
|
+
function createProcessor(options) {
|
|
55
|
+
const rehypePlugins = options.rehypePlugins || emptyPlugins;
|
|
56
|
+
const remarkPlugins = options.remarkPlugins || emptyPlugins;
|
|
57
|
+
const remarkRehypeOptions = options.remarkRehypeOptions
|
|
58
|
+
? { ...options.remarkRehypeOptions, ...emptyRemarkRehypeOptions }
|
|
59
|
+
: emptyRemarkRehypeOptions;
|
|
60
|
+
const processor = unified()
|
|
61
|
+
.use(remarkParse)
|
|
62
|
+
.use(remarkPlugins)
|
|
63
|
+
.use(remarkRehype, remarkRehypeOptions)
|
|
64
|
+
.use(rehypePlugins);
|
|
65
|
+
return processor;
|
|
66
|
+
}
|
|
67
|
+
function createFile(options) {
|
|
68
|
+
const children = options.children || '';
|
|
69
|
+
const file = new VFile();
|
|
70
|
+
if (typeof children === 'string') {
|
|
71
|
+
file.value = children;
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
unreachable('Unexpected value `' +
|
|
75
|
+
children +
|
|
76
|
+
'` for `children` prop, expected `string`');
|
|
77
|
+
}
|
|
78
|
+
return file;
|
|
79
|
+
}
|
|
80
|
+
function post(tree, options) {
|
|
81
|
+
const allowedElements = options.allowedElements;
|
|
82
|
+
const allowElement = options.allowElement;
|
|
83
|
+
const components = options.components;
|
|
84
|
+
const disallowedElements = options.disallowedElements;
|
|
85
|
+
const skipHtml = options.skipHtml;
|
|
86
|
+
const unwrapDisallowed = options.unwrapDisallowed;
|
|
87
|
+
const urlTransform = options.urlTransform || defaultUrlTransform;
|
|
88
|
+
if (allowedElements && disallowedElements) {
|
|
89
|
+
unreachable('Unexpected combined `allowedElements` and `disallowedElements`, expected one or the other');
|
|
90
|
+
}
|
|
91
|
+
visit(tree, transform);
|
|
92
|
+
return toJsxRuntime(tree, {
|
|
93
|
+
Fragment,
|
|
94
|
+
components,
|
|
95
|
+
ignoreInvalidStyle: true,
|
|
96
|
+
jsx,
|
|
97
|
+
jsxs,
|
|
98
|
+
passKeys: true,
|
|
99
|
+
passNode: true
|
|
100
|
+
});
|
|
101
|
+
function transform(node, index, parent) {
|
|
102
|
+
if (node.type === 'raw' && parent && typeof index === 'number') {
|
|
103
|
+
if (skipHtml) {
|
|
104
|
+
parent.children.splice(index, 1);
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
parent.children[index] = { type: 'text', value: node.value };
|
|
108
|
+
}
|
|
109
|
+
return index;
|
|
110
|
+
}
|
|
111
|
+
if (node.type === 'element') {
|
|
112
|
+
let key;
|
|
113
|
+
for (key in urlAttributes) {
|
|
114
|
+
if (Object.hasOwn(urlAttributes, key) &&
|
|
115
|
+
Object.hasOwn(node.properties, key)) {
|
|
116
|
+
const value = node.properties[key];
|
|
117
|
+
const test = urlAttributes[key];
|
|
118
|
+
if (test === null || test.includes(node.tagName)) {
|
|
119
|
+
node.properties[key] = urlTransform(String(value || ''), key, node);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
if (node.type === 'element') {
|
|
125
|
+
let remove = allowedElements
|
|
126
|
+
? !allowedElements.includes(node.tagName)
|
|
127
|
+
: disallowedElements
|
|
128
|
+
? disallowedElements.includes(node.tagName)
|
|
129
|
+
: false;
|
|
130
|
+
if (!remove && allowElement && typeof index === 'number') {
|
|
131
|
+
remove = !allowElement(node, index, parent);
|
|
132
|
+
}
|
|
133
|
+
if (remove && parent && typeof index === 'number') {
|
|
134
|
+
if (unwrapDisallowed && node.children) {
|
|
135
|
+
parent.children.splice(index, 1, ...node.children);
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
parent.children.splice(index, 1);
|
|
139
|
+
}
|
|
140
|
+
return index;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
function defaultUrlTransform(value) {
|
|
146
|
+
const colon = value.indexOf(':');
|
|
147
|
+
const questionMark = value.indexOf('?');
|
|
148
|
+
const numberSign = value.indexOf('#');
|
|
149
|
+
const slash = value.indexOf('/');
|
|
150
|
+
if (
|
|
151
|
+
// If there is no protocol, it’s relative.
|
|
152
|
+
colon === -1 ||
|
|
153
|
+
// If the first colon is after a `?`, `#`, or `/`, it’s not a protocol.
|
|
154
|
+
(slash !== -1 && colon > slash) ||
|
|
155
|
+
(questionMark !== -1 && colon > questionMark) ||
|
|
156
|
+
(numberSign !== -1 && colon > numberSign) ||
|
|
157
|
+
// It is a protocol, it should be allowed.
|
|
158
|
+
safeProtocol.test(value.slice(0, colon))) {
|
|
159
|
+
return value;
|
|
160
|
+
}
|
|
161
|
+
return '';
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
export { Markdown, MarkdownAsync, defaultUrlTransform };
|
|
165
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":["../src/index.ts"],"sourcesContent":["//\n// index.ts\n//\n// The MIT License\n// Copyright (c) 2021 - 2026 O2ter Limited. All rights reserved.\n//\n// Permission is hereby granted, free of charge, to any person obtaining a copy\n// of this software and associated documentation files (the \"Software\"), to deal\n// in the Software without restriction, including without limitation the rights\n// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n// copies of the Software, and to permit persons to whom the Software is\n// furnished to do so, subject to the following conditions:\n//\n// The above copyright notice and this permission notice shall be included in\n// all copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n// THE SOFTWARE.\n//\n\nimport { unreachable } from 'devlop';\nimport { toJsxRuntime } from 'hast-util-to-jsx-runtime';\nimport { urlAttributes } from 'html-url-attributes';\nimport { Fragment, jsx, jsxs } from 'frosty/jsx-runtime';\nimport { ComponentNode, ElementNode, useMemo, useResource } from 'frosty';\nimport remarkParse from 'remark-parse';\nimport remarkRehype from 'remark-rehype';\nimport { unified, Processor } from 'unified';\nimport { visit } from 'unist-util-visit';\nimport { VFile } from 'vfile';\n\nimport type { PluggableList } from 'unified';\nimport type { Root as MdastRoot } from 'mdast';\nimport type { Root } from 'hast';\nimport type { Options as RemarkRehypeOptions } from 'remark-rehype';\nimport type { BuildVisitor } from 'unist-util-visit';\n\ntype UrlTransform = (value: string, key: string, node: any) => string;\n\ninterface Options {\n children?: string;\n rehypePlugins?: PluggableList;\n remarkPlugins?: PluggableList;\n remarkRehypeOptions?: RemarkRehypeOptions;\n allowedElements?: string[];\n allowElement?: (node: any, index: number, parent: any) => boolean;\n components?: Record<string, any>;\n disallowedElements?: string[];\n skipHtml?: boolean;\n unwrapDisallowed?: boolean;\n urlTransform?: UrlTransform;\n fallback?: ElementNode;\n}\n\ninterface HooksOptions extends Options {\n fallback?: ElementNode;\n}\n\nconst emptyPlugins: PluggableList = [];\nconst emptyRemarkRehypeOptions: Readonly<RemarkRehypeOptions> = { allowDangerousHtml: true };\nconst safeProtocol = /^(https?|ircs?|mailto|xmpp)$/i;\n\nexport function Markdown(options: Readonly<Options>): ComponentNode {\n const processor = createProcessor(options);\n const file = createFile(options);\n return post(processor.runSync(processor.parse(file), file), options);\n}\n\nexport function MarkdownAsync(options: Readonly<HooksOptions>): ElementNode {\n const processor = useMemo(\n () => createProcessor(options),\n [options.rehypePlugins, options.remarkPlugins, options.remarkRehypeOptions]\n );\n const { resource: tree, error } = useResource(() => {\n const file = createFile(options);\n return processor.run(processor.parse(file), file);\n }, [options.children, processor]);\n if (error) throw error;\n return tree ? post(tree, options) : options.fallback;\n}\n\n\nfunction createProcessor(options: Readonly<Options>): Processor<MdastRoot, MdastRoot, Root, undefined, undefined> {\n const rehypePlugins = options.rehypePlugins || emptyPlugins;\n const remarkPlugins = options.remarkPlugins || emptyPlugins;\n const remarkRehypeOptions = options.remarkRehypeOptions\n ? { ...options.remarkRehypeOptions, ...emptyRemarkRehypeOptions }\n : emptyRemarkRehypeOptions;\n\n const processor = unified()\n .use(remarkParse)\n .use(remarkPlugins)\n .use(remarkRehype, remarkRehypeOptions)\n .use(rehypePlugins);\n\n return processor;\n}\n\n\nfunction createFile(options: Readonly<Options>): VFile {\n const children = options.children || '';\n const file = new VFile();\n\n if (typeof children === 'string') {\n file.value = children;\n } else {\n unreachable(\n 'Unexpected value `' +\n children +\n '` for `children` prop, expected `string`'\n );\n }\n\n return file;\n}\n\n\nfunction post(tree: any, options: Readonly<Options>): ComponentNode {\n const allowedElements = options.allowedElements;\n const allowElement = options.allowElement;\n const components = options.components;\n const disallowedElements = options.disallowedElements;\n const skipHtml = options.skipHtml;\n const unwrapDisallowed = options.unwrapDisallowed;\n const urlTransform = options.urlTransform || defaultUrlTransform;\n\n if (allowedElements && disallowedElements) {\n unreachable(\n 'Unexpected combined `allowedElements` and `disallowedElements`, expected one or the other'\n );\n }\n\n visit(tree, transform as BuildVisitor<Root>);\n\n return toJsxRuntime(tree, {\n Fragment,\n components,\n ignoreInvalidStyle: true,\n jsx,\n jsxs,\n passKeys: true,\n passNode: true\n });\n\n function transform(node: any, index: number, parent: any) {\n if (node.type === 'raw' && parent && typeof index === 'number') {\n if (skipHtml) {\n parent.children.splice(index, 1);\n } else {\n parent.children[index] = { type: 'text', value: node.value };\n }\n\n return index;\n }\n\n if (node.type === 'element') {\n let key: string;\n\n for (key in urlAttributes) {\n if (\n Object.hasOwn(urlAttributes, key) &&\n Object.hasOwn(node.properties, key)\n ) {\n const value = node.properties[key];\n const test = urlAttributes[key];\n if (test === null || test.includes(node.tagName)) {\n node.properties[key] = urlTransform(String(value || ''), key, node);\n }\n }\n }\n }\n\n if (node.type === 'element') {\n let remove = allowedElements\n ? !allowedElements.includes(node.tagName)\n : disallowedElements\n ? disallowedElements.includes(node.tagName)\n : false;\n\n if (!remove && allowElement && typeof index === 'number') {\n remove = !allowElement(node, index, parent);\n }\n\n if (remove && parent && typeof index === 'number') {\n if (unwrapDisallowed && node.children) {\n parent.children.splice(index, 1, ...node.children);\n } else {\n parent.children.splice(index, 1);\n }\n\n return index;\n }\n }\n }\n}\n\n\nexport function defaultUrlTransform(value: string): string {\n const colon = value.indexOf(':');\n const questionMark = value.indexOf('?');\n const numberSign = value.indexOf('#');\n const slash = value.indexOf('/');\n\n if (\n // If there is no protocol, it’s relative.\n colon === -1 ||\n // If the first colon is after a `?`, `#`, or `/`, it’s not a protocol.\n (slash !== -1 && colon > slash) ||\n (questionMark !== -1 && colon > questionMark) ||\n (numberSign !== -1 && colon > numberSign) ||\n // It is a protocol, it should be allowed.\n safeProtocol.test(value.slice(0, colon))\n ) {\n return value;\n }\n\n return '';\n}"],"names":[],"mappings":";;;;;;;;;;;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAwCA,MAAM,YAAY,GAAkB,EAAE;AACtC,MAAM,wBAAwB,GAAkC,EAAE,kBAAkB,EAAE,IAAI,EAAE;AAC5F,MAAM,YAAY,GAAG,+BAA+B;AAE9C,SAAU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@@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var devlop = require('devlop');
|
|
4
|
+
var hastUtilToJsxRuntime = require('hast-util-to-jsx-runtime');
|
|
5
|
+
var htmlUrlAttributes = require('html-url-attributes');
|
|
6
|
+
var jsxRuntime = require('frosty/jsx-runtime');
|
|
7
|
+
var frosty = require('frosty');
|
|
8
|
+
var remarkParse = require('remark-parse');
|
|
9
|
+
var remarkRehype = require('remark-rehype');
|
|
10
|
+
var unified = require('unified');
|
|
11
|
+
var unistUtilVisit = require('unist-util-visit');
|
|
12
|
+
var vfile = require('vfile');
|
|
13
|
+
|
|
14
|
+
//
|
|
15
|
+
// index.ts
|
|
16
|
+
//
|
|
17
|
+
// The MIT License
|
|
18
|
+
// Copyright (c) 2021 - 2026 O2ter Limited. All rights reserved.
|
|
19
|
+
//
|
|
20
|
+
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
21
|
+
// of this software and associated documentation files (the "Software"), to deal
|
|
22
|
+
// in the Software without restriction, including without limitation the rights
|
|
23
|
+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
24
|
+
// copies of the Software, and to permit persons to whom the Software is
|
|
25
|
+
// furnished to do so, subject to the following conditions:
|
|
26
|
+
//
|
|
27
|
+
// The above copyright notice and this permission notice shall be included in
|
|
28
|
+
// all copies or substantial portions of the Software.
|
|
29
|
+
//
|
|
30
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
31
|
+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
32
|
+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
33
|
+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
34
|
+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
35
|
+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
36
|
+
// THE SOFTWARE.
|
|
37
|
+
//
|
|
38
|
+
const emptyPlugins = [];
|
|
39
|
+
const emptyRemarkRehypeOptions = { allowDangerousHtml: true };
|
|
40
|
+
const safeProtocol = /^(https?|ircs?|mailto|xmpp)$/i;
|
|
41
|
+
function Markdown(options) {
|
|
42
|
+
const processor = createProcessor(options);
|
|
43
|
+
const file = createFile(options);
|
|
44
|
+
return post(processor.runSync(processor.parse(file), file), options);
|
|
45
|
+
}
|
|
46
|
+
function MarkdownAsync(options) {
|
|
47
|
+
const processor = frosty.useMemo(() => createProcessor(options), [options.rehypePlugins, options.remarkPlugins, options.remarkRehypeOptions]);
|
|
48
|
+
const { resource: tree, error } = frosty.useResource(() => {
|
|
49
|
+
const file = createFile(options);
|
|
50
|
+
return processor.run(processor.parse(file), file);
|
|
51
|
+
}, [options.children, processor]);
|
|
52
|
+
if (error)
|
|
53
|
+
throw error;
|
|
54
|
+
return tree ? post(tree, options) : options.fallback;
|
|
55
|
+
}
|
|
56
|
+
function createProcessor(options) {
|
|
57
|
+
const rehypePlugins = options.rehypePlugins || emptyPlugins;
|
|
58
|
+
const remarkPlugins = options.remarkPlugins || emptyPlugins;
|
|
59
|
+
const remarkRehypeOptions = options.remarkRehypeOptions
|
|
60
|
+
? { ...options.remarkRehypeOptions, ...emptyRemarkRehypeOptions }
|
|
61
|
+
: emptyRemarkRehypeOptions;
|
|
62
|
+
const processor = unified.unified()
|
|
63
|
+
.use(remarkParse)
|
|
64
|
+
.use(remarkPlugins)
|
|
65
|
+
.use(remarkRehype, remarkRehypeOptions)
|
|
66
|
+
.use(rehypePlugins);
|
|
67
|
+
return processor;
|
|
68
|
+
}
|
|
69
|
+
function createFile(options) {
|
|
70
|
+
const children = options.children || '';
|
|
71
|
+
const file = new vfile.VFile();
|
|
72
|
+
if (typeof children === 'string') {
|
|
73
|
+
file.value = children;
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
devlop.unreachable('Unexpected value `' +
|
|
77
|
+
children +
|
|
78
|
+
'` for `children` prop, expected `string`');
|
|
79
|
+
}
|
|
80
|
+
return file;
|
|
81
|
+
}
|
|
82
|
+
function post(tree, options) {
|
|
83
|
+
const allowedElements = options.allowedElements;
|
|
84
|
+
const allowElement = options.allowElement;
|
|
85
|
+
const components = options.components;
|
|
86
|
+
const disallowedElements = options.disallowedElements;
|
|
87
|
+
const skipHtml = options.skipHtml;
|
|
88
|
+
const unwrapDisallowed = options.unwrapDisallowed;
|
|
89
|
+
const urlTransform = options.urlTransform || defaultUrlTransform;
|
|
90
|
+
if (allowedElements && disallowedElements) {
|
|
91
|
+
devlop.unreachable('Unexpected combined `allowedElements` and `disallowedElements`, expected one or the other');
|
|
92
|
+
}
|
|
93
|
+
unistUtilVisit.visit(tree, transform);
|
|
94
|
+
return hastUtilToJsxRuntime.toJsxRuntime(tree, {
|
|
95
|
+
Fragment: jsxRuntime.Fragment,
|
|
96
|
+
components,
|
|
97
|
+
ignoreInvalidStyle: true,
|
|
98
|
+
jsx: jsxRuntime.jsx,
|
|
99
|
+
jsxs: jsxRuntime.jsxs,
|
|
100
|
+
passKeys: true,
|
|
101
|
+
passNode: true
|
|
102
|
+
});
|
|
103
|
+
function transform(node, index, parent) {
|
|
104
|
+
if (node.type === 'raw' && parent && typeof index === 'number') {
|
|
105
|
+
if (skipHtml) {
|
|
106
|
+
parent.children.splice(index, 1);
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
parent.children[index] = { type: 'text', value: node.value };
|
|
110
|
+
}
|
|
111
|
+
return index;
|
|
112
|
+
}
|
|
113
|
+
if (node.type === 'element') {
|
|
114
|
+
let key;
|
|
115
|
+
for (key in htmlUrlAttributes.urlAttributes) {
|
|
116
|
+
if (Object.hasOwn(htmlUrlAttributes.urlAttributes, key) &&
|
|
117
|
+
Object.hasOwn(node.properties, key)) {
|
|
118
|
+
const value = node.properties[key];
|
|
119
|
+
const test = htmlUrlAttributes.urlAttributes[key];
|
|
120
|
+
if (test === null || test.includes(node.tagName)) {
|
|
121
|
+
node.properties[key] = urlTransform(String(value || ''), key, node);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
if (node.type === 'element') {
|
|
127
|
+
let remove = allowedElements
|
|
128
|
+
? !allowedElements.includes(node.tagName)
|
|
129
|
+
: disallowedElements
|
|
130
|
+
? disallowedElements.includes(node.tagName)
|
|
131
|
+
: false;
|
|
132
|
+
if (!remove && allowElement && typeof index === 'number') {
|
|
133
|
+
remove = !allowElement(node, index, parent);
|
|
134
|
+
}
|
|
135
|
+
if (remove && parent && typeof index === 'number') {
|
|
136
|
+
if (unwrapDisallowed && node.children) {
|
|
137
|
+
parent.children.splice(index, 1, ...node.children);
|
|
138
|
+
}
|
|
139
|
+
else {
|
|
140
|
+
parent.children.splice(index, 1);
|
|
141
|
+
}
|
|
142
|
+
return index;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
function defaultUrlTransform(value) {
|
|
148
|
+
const colon = value.indexOf(':');
|
|
149
|
+
const questionMark = value.indexOf('?');
|
|
150
|
+
const numberSign = value.indexOf('#');
|
|
151
|
+
const slash = value.indexOf('/');
|
|
152
|
+
if (
|
|
153
|
+
// If there is no protocol, it’s relative.
|
|
154
|
+
colon === -1 ||
|
|
155
|
+
// If the first colon is after a `?`, `#`, or `/`, it’s not a protocol.
|
|
156
|
+
(slash !== -1 && colon > slash) ||
|
|
157
|
+
(questionMark !== -1 && colon > questionMark) ||
|
|
158
|
+
(numberSign !== -1 && colon > numberSign) ||
|
|
159
|
+
// It is a protocol, it should be allowed.
|
|
160
|
+
safeProtocol.test(value.slice(0, colon))) {
|
|
161
|
+
return value;
|
|
162
|
+
}
|
|
163
|
+
return '';
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
exports.Markdown = Markdown;
|
|
167
|
+
exports.MarkdownAsync = MarkdownAsync;
|
|
168
|
+
exports.defaultUrlTransform = defaultUrlTransform;
|
|
169
|
+
//# sourceMappingURL=index.server.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.server.js","sources":["../src/index.ts"],"sourcesContent":["//\n// index.ts\n//\n// The MIT License\n// Copyright (c) 2021 - 2026 O2ter Limited. All rights reserved.\n//\n// Permission is hereby granted, free of charge, to any person obtaining a copy\n// of this software and associated documentation files (the \"Software\"), to deal\n// in the Software without restriction, including without limitation the rights\n// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n// copies of the Software, and to permit persons to whom the Software is\n// furnished to do so, subject to the following conditions:\n//\n// The above copyright notice and this permission notice shall be included in\n// all copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n// THE SOFTWARE.\n//\n\nimport { unreachable } from 'devlop';\nimport { toJsxRuntime } from 'hast-util-to-jsx-runtime';\nimport { urlAttributes } from 'html-url-attributes';\nimport { Fragment, jsx, jsxs } from 'frosty/jsx-runtime';\nimport { ComponentNode, ElementNode, useMemo, useResource } from 'frosty';\nimport remarkParse from 'remark-parse';\nimport remarkRehype from 'remark-rehype';\nimport { unified, Processor } from 'unified';\nimport { visit } from 'unist-util-visit';\nimport { VFile } from 'vfile';\n\nimport type { PluggableList } from 'unified';\nimport type { Root as MdastRoot } from 'mdast';\nimport type { Root } from 'hast';\nimport type { Options as RemarkRehypeOptions } from 'remark-rehype';\nimport type { BuildVisitor } from 'unist-util-visit';\n\ntype UrlTransform = (value: string, key: string, node: any) => string;\n\ninterface Options {\n children?: string;\n rehypePlugins?: PluggableList;\n remarkPlugins?: PluggableList;\n remarkRehypeOptions?: RemarkRehypeOptions;\n allowedElements?: string[];\n allowElement?: (node: any, index: number, parent: any) => boolean;\n components?: Record<string, any>;\n disallowedElements?: string[];\n skipHtml?: boolean;\n unwrapDisallowed?: boolean;\n urlTransform?: UrlTransform;\n fallback?: ElementNode;\n}\n\ninterface HooksOptions extends Options {\n fallback?: ElementNode;\n}\n\nconst emptyPlugins: PluggableList = [];\nconst emptyRemarkRehypeOptions: Readonly<RemarkRehypeOptions> = { allowDangerousHtml: true };\nconst safeProtocol = /^(https?|ircs?|mailto|xmpp)$/i;\n\nexport function Markdown(options: Readonly<Options>): ComponentNode {\n const processor = createProcessor(options);\n const file = createFile(options);\n return post(processor.runSync(processor.parse(file), file), options);\n}\n\nexport function MarkdownAsync(options: Readonly<HooksOptions>): ElementNode {\n const processor = useMemo(\n () => createProcessor(options),\n [options.rehypePlugins, options.remarkPlugins, options.remarkRehypeOptions]\n );\n const { resource: tree, error } = useResource(() => {\n const file = createFile(options);\n return processor.run(processor.parse(file), file);\n }, [options.children, processor]);\n if (error) throw error;\n return tree ? post(tree, options) : options.fallback;\n}\n\n\nfunction createProcessor(options: Readonly<Options>): Processor<MdastRoot, MdastRoot, Root, undefined, undefined> {\n const rehypePlugins = options.rehypePlugins || emptyPlugins;\n const remarkPlugins = options.remarkPlugins || emptyPlugins;\n const remarkRehypeOptions = options.remarkRehypeOptions\n ? { ...options.remarkRehypeOptions, ...emptyRemarkRehypeOptions }\n : emptyRemarkRehypeOptions;\n\n const processor = unified()\n .use(remarkParse)\n .use(remarkPlugins)\n .use(remarkRehype, remarkRehypeOptions)\n .use(rehypePlugins);\n\n return processor;\n}\n\n\nfunction createFile(options: Readonly<Options>): VFile {\n const children = options.children || '';\n const file = new VFile();\n\n if (typeof children === 'string') {\n file.value = children;\n } else {\n unreachable(\n 'Unexpected value `' +\n children +\n '` for `children` prop, expected `string`'\n );\n }\n\n return file;\n}\n\n\nfunction post(tree: any, options: Readonly<Options>): ComponentNode {\n const allowedElements = options.allowedElements;\n const allowElement = options.allowElement;\n const components = options.components;\n const disallowedElements = options.disallowedElements;\n const skipHtml = options.skipHtml;\n const unwrapDisallowed = options.unwrapDisallowed;\n const urlTransform = options.urlTransform || defaultUrlTransform;\n\n if (allowedElements && disallowedElements) {\n unreachable(\n 'Unexpected combined `allowedElements` and `disallowedElements`, expected one or the other'\n );\n }\n\n visit(tree, transform as BuildVisitor<Root>);\n\n return toJsxRuntime(tree, {\n Fragment,\n components,\n ignoreInvalidStyle: true,\n jsx,\n jsxs,\n passKeys: true,\n passNode: true\n });\n\n function transform(node: any, index: number, parent: any) {\n if (node.type === 'raw' && parent && typeof index === 'number') {\n if (skipHtml) {\n parent.children.splice(index, 1);\n } else {\n parent.children[index] = { type: 'text', value: node.value };\n }\n\n return index;\n }\n\n if (node.type === 'element') {\n let key: string;\n\n for (key in urlAttributes) {\n if (\n Object.hasOwn(urlAttributes, key) &&\n Object.hasOwn(node.properties, key)\n ) {\n const value = node.properties[key];\n const test = urlAttributes[key];\n if (test === null || test.includes(node.tagName)) {\n node.properties[key] = urlTransform(String(value || ''), key, node);\n }\n }\n }\n }\n\n if (node.type === 'element') {\n let remove = allowedElements\n ? !allowedElements.includes(node.tagName)\n : disallowedElements\n ? disallowedElements.includes(node.tagName)\n : false;\n\n if (!remove && allowElement && typeof index === 'number') {\n remove = !allowElement(node, index, parent);\n }\n\n if (remove && parent && typeof index === 'number') {\n if (unwrapDisallowed && node.children) {\n parent.children.splice(index, 1, ...node.children);\n } else {\n parent.children.splice(index, 1);\n }\n\n return index;\n }\n }\n }\n}\n\n\nexport function defaultUrlTransform(value: string): string {\n const colon = value.indexOf(':');\n const questionMark = value.indexOf('?');\n const numberSign = value.indexOf('#');\n const slash = value.indexOf('/');\n\n if (\n // If there is no protocol, it’s relative.\n colon === -1 ||\n // If the first colon is after a `?`, `#`, or `/`, it’s not a protocol.\n (slash !== -1 && colon > slash) ||\n (questionMark !== -1 && colon > questionMark) ||\n (numberSign !== -1 && colon > numberSign) ||\n // It is a protocol, it should be allowed.\n safeProtocol.test(value.slice(0, colon))\n ) {\n return value;\n }\n\n return '';\n}"],"names":["useMemo","useResource","unified","VFile","unreachable","visit","toJsxRuntime","Fragment","jsx","jsxs","urlAttributes"],"mappings":";;;;;;;;;;;;;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAwCA,MAAM,YAAY,GAAkB,EAAE;AACtC,MAAM,wBAAwB,GAAkC,EAAE,kBAAkB,EAAE,IAAI,EAAE;AAC5F,MAAM,YAAY,GAAG,+BAA+B;AAE9C,SAAU,QAAQ,CAAC,OAA0B,EAAA;AACjD,IAAA,MAAM,SAAS,GAAG,eAAe,CAAC,OAAO,CAAC;AAC1C,IAAA,MAAM,IAAI,GAAG,UAAU,CAAC,OAAO,CAAC;AAChC,IAAA,OAAO,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,SAAS,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,IAAI,CAAC,EAAE,OAAO,CAAC;AACtE;AAEM,SAAU,aAAa,CAAC,OAA+B,EAAA;IAC3D,MAAM,SAAS,GAAGA,cAAO,CACvB,MAAM,eAAe,CAAC,OAAO,CAAC,EAC9B,CAAC,OAAO,CAAC,aAAa,EAAE,OAAO,CAAC,aAAa,EAAE,OAAO,CAAC,mBAAmB,CAAC,CAC5E;IACD,MAAM,EAAE,QAAQ,EAAE,IAAI,EAAE,KAAK,EAAE,GAAGC,kBAAW,CAAC,MAAK;AACjD,QAAA,MAAM,IAAI,GAAG,UAAU,CAAC,OAAO,CAAC;AAChC,QAA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AC,KAAa,EAAA;IAC/C,MAAM,KAAK,GAAG,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC;IAChC,MAAM,YAAY,GAAG,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC;IACvC,MAAM,UAAU,GAAG,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC;IACrC,MAAM,KAAK,GAAG,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC;AAEhC,IAAA;;IAEE,KAAK,KAAK,EAAE;;SAEX,KAAK,KAAK,EAAE,IAAI,KAAK,GAAG,KAAK,CAAC;SAC9B,YAAY,KAAK,EAAE,IAAI,KAAK,GAAG,YAAY,CAAC;SAC5C,UAAU,KAAK,EAAE,IAAI,KAAK,GAAG,UAAU,CAAC;;AAEzC,QAAA,YAAY,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC,EACxC;AACA,QAAA,OAAO,KAAK;IACd;AAEA,IAAA,OAAO,EAAE;AACX;;;;;;"}
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { unreachable } from 'devlop';
|
|
2
|
+
import { toJsxRuntime } from 'hast-util-to-jsx-runtime';
|
|
3
|
+
import { urlAttributes } from 'html-url-attributes';
|
|
4
|
+
import { jsxs, jsx, Fragment } from 'frosty/jsx-runtime';
|
|
5
|
+
import { useMemo, useResource } from 'frosty';
|
|
6
|
+
import remarkParse from 'remark-parse';
|
|
7
|
+
import remarkRehype from 'remark-rehype';
|
|
8
|
+
import { unified } from 'unified';
|
|
9
|
+
import { visit } from 'unist-util-visit';
|
|
10
|
+
import { VFile } from 'vfile';
|
|
11
|
+
|
|
12
|
+
//
|
|
13
|
+
// index.ts
|
|
14
|
+
//
|
|
15
|
+
// The MIT License
|
|
16
|
+
// Copyright (c) 2021 - 2026 O2ter Limited. All rights reserved.
|
|
17
|
+
//
|
|
18
|
+
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
19
|
+
// of this software and associated documentation files (the "Software"), to deal
|
|
20
|
+
// in the Software without restriction, including without limitation the rights
|
|
21
|
+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
22
|
+
// copies of the Software, and to permit persons to whom the Software is
|
|
23
|
+
// furnished to do so, subject to the following conditions:
|
|
24
|
+
//
|
|
25
|
+
// The above copyright notice and this permission notice shall be included in
|
|
26
|
+
// all copies or substantial portions of the Software.
|
|
27
|
+
//
|
|
28
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
29
|
+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
30
|
+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
31
|
+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
32
|
+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
33
|
+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
34
|
+
// THE SOFTWARE.
|
|
35
|
+
//
|
|
36
|
+
const emptyPlugins = [];
|
|
37
|
+
const emptyRemarkRehypeOptions = { allowDangerousHtml: true };
|
|
38
|
+
const safeProtocol = /^(https?|ircs?|mailto|xmpp)$/i;
|
|
39
|
+
function Markdown(options) {
|
|
40
|
+
const processor = createProcessor(options);
|
|
41
|
+
const file = createFile(options);
|
|
42
|
+
return post(processor.runSync(processor.parse(file), file), options);
|
|
43
|
+
}
|
|
44
|
+
function MarkdownAsync(options) {
|
|
45
|
+
const processor = useMemo(() => createProcessor(options), [options.rehypePlugins, options.remarkPlugins, options.remarkRehypeOptions]);
|
|
46
|
+
const { resource: tree, error } = useResource(() => {
|
|
47
|
+
const file = createFile(options);
|
|
48
|
+
return processor.run(processor.parse(file), file);
|
|
49
|
+
}, [options.children, processor]);
|
|
50
|
+
if (error)
|
|
51
|
+
throw error;
|
|
52
|
+
return tree ? post(tree, options) : options.fallback;
|
|
53
|
+
}
|
|
54
|
+
function createProcessor(options) {
|
|
55
|
+
const rehypePlugins = options.rehypePlugins || emptyPlugins;
|
|
56
|
+
const remarkPlugins = options.remarkPlugins || emptyPlugins;
|
|
57
|
+
const remarkRehypeOptions = options.remarkRehypeOptions
|
|
58
|
+
? { ...options.remarkRehypeOptions, ...emptyRemarkRehypeOptions }
|
|
59
|
+
: emptyRemarkRehypeOptions;
|
|
60
|
+
const processor = unified()
|
|
61
|
+
.use(remarkParse)
|
|
62
|
+
.use(remarkPlugins)
|
|
63
|
+
.use(remarkRehype, remarkRehypeOptions)
|
|
64
|
+
.use(rehypePlugins);
|
|
65
|
+
return processor;
|
|
66
|
+
}
|
|
67
|
+
function createFile(options) {
|
|
68
|
+
const children = options.children || '';
|
|
69
|
+
const file = new VFile();
|
|
70
|
+
if (typeof children === 'string') {
|
|
71
|
+
file.value = children;
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
unreachable('Unexpected value `' +
|
|
75
|
+
children +
|
|
76
|
+
'` for `children` prop, expected `string`');
|
|
77
|
+
}
|
|
78
|
+
return file;
|
|
79
|
+
}
|
|
80
|
+
function post(tree, options) {
|
|
81
|
+
const allowedElements = options.allowedElements;
|
|
82
|
+
const allowElement = options.allowElement;
|
|
83
|
+
const components = options.components;
|
|
84
|
+
const disallowedElements = options.disallowedElements;
|
|
85
|
+
const skipHtml = options.skipHtml;
|
|
86
|
+
const unwrapDisallowed = options.unwrapDisallowed;
|
|
87
|
+
const urlTransform = options.urlTransform || defaultUrlTransform;
|
|
88
|
+
if (allowedElements && disallowedElements) {
|
|
89
|
+
unreachable('Unexpected combined `allowedElements` and `disallowedElements`, expected one or the other');
|
|
90
|
+
}
|
|
91
|
+
visit(tree, transform);
|
|
92
|
+
return toJsxRuntime(tree, {
|
|
93
|
+
Fragment,
|
|
94
|
+
components,
|
|
95
|
+
ignoreInvalidStyle: true,
|
|
96
|
+
jsx,
|
|
97
|
+
jsxs,
|
|
98
|
+
passKeys: true,
|
|
99
|
+
passNode: true
|
|
100
|
+
});
|
|
101
|
+
function transform(node, index, parent) {
|
|
102
|
+
if (node.type === 'raw' && parent && typeof index === 'number') {
|
|
103
|
+
if (skipHtml) {
|
|
104
|
+
parent.children.splice(index, 1);
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
parent.children[index] = { type: 'text', value: node.value };
|
|
108
|
+
}
|
|
109
|
+
return index;
|
|
110
|
+
}
|
|
111
|
+
if (node.type === 'element') {
|
|
112
|
+
let key;
|
|
113
|
+
for (key in urlAttributes) {
|
|
114
|
+
if (Object.hasOwn(urlAttributes, key) &&
|
|
115
|
+
Object.hasOwn(node.properties, key)) {
|
|
116
|
+
const value = node.properties[key];
|
|
117
|
+
const test = urlAttributes[key];
|
|
118
|
+
if (test === null || test.includes(node.tagName)) {
|
|
119
|
+
node.properties[key] = urlTransform(String(value || ''), key, node);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
if (node.type === 'element') {
|
|
125
|
+
let remove = allowedElements
|
|
126
|
+
? !allowedElements.includes(node.tagName)
|
|
127
|
+
: disallowedElements
|
|
128
|
+
? disallowedElements.includes(node.tagName)
|
|
129
|
+
: false;
|
|
130
|
+
if (!remove && allowElement && typeof index === 'number') {
|
|
131
|
+
remove = !allowElement(node, index, parent);
|
|
132
|
+
}
|
|
133
|
+
if (remove && parent && typeof index === 'number') {
|
|
134
|
+
if (unwrapDisallowed && node.children) {
|
|
135
|
+
parent.children.splice(index, 1, ...node.children);
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
parent.children.splice(index, 1);
|
|
139
|
+
}
|
|
140
|
+
return index;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
function defaultUrlTransform(value) {
|
|
146
|
+
const colon = value.indexOf(':');
|
|
147
|
+
const questionMark = value.indexOf('?');
|
|
148
|
+
const numberSign = value.indexOf('#');
|
|
149
|
+
const slash = value.indexOf('/');
|
|
150
|
+
if (
|
|
151
|
+
// If there is no protocol, it’s relative.
|
|
152
|
+
colon === -1 ||
|
|
153
|
+
// If the first colon is after a `?`, `#`, or `/`, it’s not a protocol.
|
|
154
|
+
(slash !== -1 && colon > slash) ||
|
|
155
|
+
(questionMark !== -1 && colon > questionMark) ||
|
|
156
|
+
(numberSign !== -1 && colon > numberSign) ||
|
|
157
|
+
// It is a protocol, it should be allowed.
|
|
158
|
+
safeProtocol.test(value.slice(0, colon))) {
|
|
159
|
+
return value;
|
|
160
|
+
}
|
|
161
|
+
return '';
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
export { Markdown, MarkdownAsync, defaultUrlTransform };
|
|
165
|
+
//# sourceMappingURL=index.server.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.server.mjs","sources":["../src/index.ts"],"sourcesContent":["//\n// index.ts\n//\n// The MIT License\n// Copyright (c) 2021 - 2026 O2ter Limited. All rights reserved.\n//\n// Permission is hereby granted, free of charge, to any person obtaining a copy\n// of this software and associated documentation files (the \"Software\"), to deal\n// in the Software without restriction, including without limitation the rights\n// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n// copies of the Software, and to permit persons to whom the Software is\n// furnished to do so, subject to the following conditions:\n//\n// The above copyright notice and this permission notice shall be included in\n// all copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n// THE SOFTWARE.\n//\n\nimport { unreachable } from 'devlop';\nimport { toJsxRuntime } from 'hast-util-to-jsx-runtime';\nimport { urlAttributes } from 'html-url-attributes';\nimport { Fragment, jsx, jsxs } from 'frosty/jsx-runtime';\nimport { ComponentNode, ElementNode, useMemo, useResource } from 'frosty';\nimport remarkParse from 'remark-parse';\nimport remarkRehype from 'remark-rehype';\nimport { unified, Processor } from 'unified';\nimport { visit } from 'unist-util-visit';\nimport { VFile } from 'vfile';\n\nimport type { PluggableList } from 'unified';\nimport type { Root as MdastRoot } from 'mdast';\nimport type { Root } from 'hast';\nimport type { Options as RemarkRehypeOptions } from 'remark-rehype';\nimport type { BuildVisitor } from 'unist-util-visit';\n\ntype UrlTransform = (value: string, key: string, node: any) => string;\n\ninterface Options {\n children?: string;\n rehypePlugins?: PluggableList;\n remarkPlugins?: PluggableList;\n remarkRehypeOptions?: RemarkRehypeOptions;\n allowedElements?: string[];\n allowElement?: (node: any, index: number, parent: any) => boolean;\n components?: Record<string, any>;\n disallowedElements?: string[];\n skipHtml?: boolean;\n unwrapDisallowed?: boolean;\n urlTransform?: UrlTransform;\n fallback?: ElementNode;\n}\n\ninterface HooksOptions extends Options {\n fallback?: ElementNode;\n}\n\nconst emptyPlugins: PluggableList = [];\nconst emptyRemarkRehypeOptions: Readonly<RemarkRehypeOptions> = { allowDangerousHtml: true };\nconst safeProtocol = /^(https?|ircs?|mailto|xmpp)$/i;\n\nexport function Markdown(options: Readonly<Options>): ComponentNode {\n const processor = createProcessor(options);\n const file = createFile(options);\n return post(processor.runSync(processor.parse(file), file), options);\n}\n\nexport function MarkdownAsync(options: Readonly<HooksOptions>): ElementNode {\n const processor = useMemo(\n () => createProcessor(options),\n [options.rehypePlugins, options.remarkPlugins, options.remarkRehypeOptions]\n );\n const { resource: tree, error } = useResource(() => {\n const file = createFile(options);\n return processor.run(processor.parse(file), file);\n }, [options.children, processor]);\n if (error) throw error;\n return tree ? post(tree, options) : options.fallback;\n}\n\n\nfunction createProcessor(options: Readonly<Options>): Processor<MdastRoot, MdastRoot, Root, undefined, undefined> {\n const rehypePlugins = options.rehypePlugins || emptyPlugins;\n const remarkPlugins = options.remarkPlugins || emptyPlugins;\n const remarkRehypeOptions = options.remarkRehypeOptions\n ? { ...options.remarkRehypeOptions, ...emptyRemarkRehypeOptions }\n : emptyRemarkRehypeOptions;\n\n const processor = unified()\n .use(remarkParse)\n .use(remarkPlugins)\n .use(remarkRehype, remarkRehypeOptions)\n .use(rehypePlugins);\n\n return processor;\n}\n\n\nfunction createFile(options: Readonly<Options>): VFile {\n const children = options.children || '';\n const file = new VFile();\n\n if (typeof children === 'string') {\n file.value = children;\n } else {\n unreachable(\n 'Unexpected value `' +\n children +\n '` for `children` prop, expected `string`'\n );\n }\n\n return file;\n}\n\n\nfunction post(tree: any, options: Readonly<Options>): ComponentNode {\n const allowedElements = options.allowedElements;\n const allowElement = options.allowElement;\n const components = options.components;\n const disallowedElements = options.disallowedElements;\n const skipHtml = options.skipHtml;\n const unwrapDisallowed = options.unwrapDisallowed;\n const urlTransform = options.urlTransform || defaultUrlTransform;\n\n if (allowedElements && disallowedElements) {\n unreachable(\n 'Unexpected combined `allowedElements` and `disallowedElements`, expected one or the other'\n );\n }\n\n visit(tree, transform as BuildVisitor<Root>);\n\n return toJsxRuntime(tree, {\n Fragment,\n components,\n ignoreInvalidStyle: true,\n jsx,\n jsxs,\n passKeys: true,\n passNode: true\n });\n\n function transform(node: any, index: number, parent: any) {\n if (node.type === 'raw' && parent && typeof index === 'number') {\n if (skipHtml) {\n parent.children.splice(index, 1);\n } else {\n parent.children[index] = { type: 'text', value: node.value };\n }\n\n return index;\n }\n\n if (node.type === 'element') {\n let key: string;\n\n for (key in urlAttributes) {\n if (\n Object.hasOwn(urlAttributes, key) &&\n Object.hasOwn(node.properties, key)\n ) {\n const value = node.properties[key];\n const test = urlAttributes[key];\n if (test === null || test.includes(node.tagName)) {\n node.properties[key] = urlTransform(String(value || ''), key, node);\n }\n }\n }\n }\n\n if (node.type === 'element') {\n let remove = allowedElements\n ? !allowedElements.includes(node.tagName)\n : disallowedElements\n ? disallowedElements.includes(node.tagName)\n : false;\n\n if (!remove && allowElement && typeof index === 'number') {\n remove = !allowElement(node, index, parent);\n }\n\n if (remove && parent && typeof index === 'number') {\n if (unwrapDisallowed && node.children) {\n parent.children.splice(index, 1, ...node.children);\n } else {\n parent.children.splice(index, 1);\n }\n\n return index;\n }\n }\n }\n}\n\n\nexport function defaultUrlTransform(value: string): string {\n const colon = value.indexOf(':');\n const questionMark = value.indexOf('?');\n const numberSign = value.indexOf('#');\n const slash = value.indexOf('/');\n\n if (\n // If there is no protocol, it’s relative.\n colon === -1 ||\n // If the first colon is after a `?`, `#`, or `/`, it’s not a protocol.\n (slash !== -1 && colon > slash) ||\n (questionMark !== -1 && colon > questionMark) ||\n (numberSign !== -1 && colon > numberSign) ||\n // It is a protocol, it should be allowed.\n safeProtocol.test(value.slice(0, colon))\n ) {\n return value;\n }\n\n return '';\n}"],"names":[],"mappings":";;;;;;;;;;;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAwCA,MAAM,YAAY,GAAkB,EAAE;AACtC,MAAM,wBAAwB,GAAkC,EAAE,kBAAkB,EAAE,IAAI,EAAE;AAC5F,MAAM,YAAY,GAAG,+BAA+B;AAE9C,SAAU,QAAQ,CAAC,OAA0B,EAAA;AACjD,IAAA,MAAM,SAAS,GAAG,eAAe,CAAC,OAAO,CAAC;AAC1C,IAAA,MAAM,IAAI,GAAG,UAAU,CAAC,OAAO,CAAC;AAChC,IAAA,OAAO,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,SAAS,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,IAAI,CAAC,EAAE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K,QAAQ,EAAE;AACjD,gBAAA,IAAI,gBAAgB,IAAI,IAAI,CAAC,QAAQ,EAAE;AACrC,oBAAA,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC,EAAE,GAAG,IAAI,CAAC,QAAQ,CAAC;gBACpD;qBAAO;oBACL,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC,CAAC;gBAClC;AAEA,gBAAA,OAAO,KAAK;YACd;QACF;IACF;AACF;AAGM,SAAU,mBAAmB,CAAC,KAAa,EAAA;IAC/C,MAAM,KAAK,GAAG,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC;IAChC,MAAM,YAAY,GAAG,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC;IACvC,MAAM,UAAU,GAAG,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC;IACrC,MAAM,KAAK,GAAG,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC;AAEhC,IAAA;;IAEE,KAAK,KAAK,EAAE;;SAEX,KAAK,KAAK,EAAE,IAAI,KAAK,GAAG,KAAK,CAAC;SAC9B,YAAY,KAAK,EAAE,IAAI,KAAK,GAAG,YAAY,CAAC;SAC5C,UAAU,KAAK,EAAE,IAAI,KAAK,GAAG,UAAU,CAAC;;AAEzC,QAAA,YAAY,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC,EACxC;AACA,QAAA,OAAO,KAAK;IACd;AAEA,IAAA,OAAO,EAAE;AACX;;;;"}
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@o2ter/frosty-markdown",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"main": "dist/index",
|
|
5
|
+
"module": "dist/index",
|
|
6
|
+
"types": "dist/index",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist/*"
|
|
9
|
+
],
|
|
10
|
+
"repository": "https://github.com/o2ter/frosty-markdown.git",
|
|
11
|
+
"author": "Susan Cheng <susan.doggie@gmail.com>",
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"scripts": {
|
|
14
|
+
"clean": "rm -rf ./dist/*",
|
|
15
|
+
"rollup": "yarn clean && rollup -c",
|
|
16
|
+
"start": "npx frosty run -d -w -s ./src tests/server/app.tsx"
|
|
17
|
+
},
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"frosty": "^0.0.155",
|
|
20
|
+
"hast-util-to-jsx-runtime": "^2.3.6",
|
|
21
|
+
"html-url-attributes": "^3.0.1",
|
|
22
|
+
"lodash": "^4.17.21",
|
|
23
|
+
"remark-parse": "^11.0.0",
|
|
24
|
+
"remark-rehype": "^11.1.2",
|
|
25
|
+
"unified": "^11.0.5",
|
|
26
|
+
"unist-util-visit": "^5.1.0",
|
|
27
|
+
"vfile": "^6.0.3"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@babel/core": "^7.28.0",
|
|
31
|
+
"@babel/preset-env": "^7.28.0",
|
|
32
|
+
"@babel/preset-react": "^7.27.1",
|
|
33
|
+
"@babel/preset-typescript": "^7.27.1",
|
|
34
|
+
"@rollup/plugin-babel": "^6.0.4",
|
|
35
|
+
"@rollup/plugin-commonjs": "^25.0.7",
|
|
36
|
+
"@rollup/plugin-json": "^6.1.0",
|
|
37
|
+
"@rollup/plugin-node-resolve": "^15.2.3",
|
|
38
|
+
"@rollup/plugin-typescript": "^11.1.6",
|
|
39
|
+
"@types/lodash": "^4.17.16",
|
|
40
|
+
"rollup": "^4.22.4",
|
|
41
|
+
"rollup-plugin-dts": "^6.1.1",
|
|
42
|
+
"rollup-plugin-scss": "^4.0.1",
|
|
43
|
+
"sass": "^1.90.0",
|
|
44
|
+
"tslib": "^2.8.1",
|
|
45
|
+
"typescript": "^5.9.2"
|
|
46
|
+
}
|
|
47
|
+
}
|