@mintlify/common 1.0.877 → 1.0.878
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.
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const encodeContentAsHtml: (content: string) => string;
|
|
1
|
+
export declare const encodeContentAsHtml: (content: string, siteUrl?: string) => string;
|
|
@@ -6,6 +6,20 @@ import remarkRehype from 'remark-rehype';
|
|
|
6
6
|
import { unified } from 'unified';
|
|
7
7
|
import { visit } from 'unist-util-visit';
|
|
8
8
|
const DANGEROUS_TAGS = ['script', 'iframe', 'object', 'embed', 'style'];
|
|
9
|
+
const rehypeResolveRelativeUrls = (siteUrl) => {
|
|
10
|
+
return (tree) => {
|
|
11
|
+
visit(tree, 'element', (node) => {
|
|
12
|
+
var _a;
|
|
13
|
+
for (const attr of ['href', 'src']) {
|
|
14
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
15
|
+
const value = (_a = node.properties) === null || _a === void 0 ? void 0 : _a[attr];
|
|
16
|
+
if (typeof value === 'string' && value.startsWith('/') && !value.startsWith('//')) {
|
|
17
|
+
node.properties[attr] = `${siteUrl}${value}`;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
};
|
|
9
23
|
const rehypeRaw = () => {
|
|
10
24
|
return (tree) => {
|
|
11
25
|
visit(tree, 'raw', (raw, index, parent) => {
|
|
@@ -26,13 +40,17 @@ const rehypeSanitizeDangerous = () => {
|
|
|
26
40
|
});
|
|
27
41
|
};
|
|
28
42
|
};
|
|
29
|
-
export const encodeContentAsHtml = (content) => {
|
|
30
|
-
|
|
43
|
+
export const encodeContentAsHtml = (content, siteUrl) => {
|
|
44
|
+
let processor = unified()
|
|
31
45
|
.use(remarkParse)
|
|
32
46
|
.use(remarkGfm)
|
|
33
47
|
.use(remarkRehype, { allowDangerousHtml: true })
|
|
34
48
|
.use(rehypeRaw)
|
|
35
|
-
.use(rehypeSanitizeDangerous)
|
|
49
|
+
.use(rehypeSanitizeDangerous);
|
|
50
|
+
if (siteUrl) {
|
|
51
|
+
processor = processor.use(rehypeResolveRelativeUrls, siteUrl);
|
|
52
|
+
}
|
|
53
|
+
const html = processor
|
|
36
54
|
.use(rehypeStringify, { allowDangerousHtml: true })
|
|
37
55
|
.processSync(content)
|
|
38
56
|
.toString();
|