@mintlify/common 1.0.905 → 1.0.907
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.
|
@@ -3,7 +3,7 @@ import { MdxJsxFlowElement } from 'mdast-util-mdx-jsx';
|
|
|
3
3
|
export interface PreprocessCustomHeadingIdsOptions {
|
|
4
4
|
/**
|
|
5
5
|
* When true, preserves the original ID as-is (only removing characters unsafe
|
|
6
|
-
* in HTML attributes). When false (default),
|
|
6
|
+
* in HTML attributes). When false (default), also collapses whitespace to hyphens.
|
|
7
7
|
*/
|
|
8
8
|
preserveOriginal?: boolean;
|
|
9
9
|
}
|
|
@@ -1,9 +1,19 @@
|
|
|
1
|
-
import slugify from '@sindresorhus/slugify';
|
|
2
1
|
const HEADING_ID_RE = /^(#{1,6})\s+(.+?)\s*\{\s*#([^}]+?)\s*\}\s*$/;
|
|
3
2
|
const UNSAFE_HTML_ATTR_CHARS = /[<>"]/g;
|
|
3
|
+
/**
|
|
4
|
+
* Characters to strip from custom heading IDs in the default (non-editor) path.
|
|
5
|
+
* Combines HTML-unsafe characters, `&` (non-conformant in attributes), and the
|
|
6
|
+
* punctuation that `cleanHeadingId` strips at render time so the preprocessed ID
|
|
7
|
+
* matches the final DOM `id`.
|
|
8
|
+
*/
|
|
9
|
+
const CUSTOM_ID_STRIP_CHARS = /[<>"&?,;:!'()\[\]{}]/g;
|
|
10
|
+
const EMOJI_RE = /\p{Emoji_Modifier}|\p{Emoji_Modifier_Base}|\p{Emoji_Presentation}|\p{Extended_Pictographic}|[\u200D\uFE0E\uFE0F]/gu;
|
|
4
11
|
function sanitizeHtmlAttr(id) {
|
|
5
12
|
return id.trim().replace(UNSAFE_HTML_ATTR_CHARS, '');
|
|
6
13
|
}
|
|
14
|
+
function normalizeCustomId(raw) {
|
|
15
|
+
return raw.trim().replace(CUSTOM_ID_STRIP_CHARS, '').replace(EMOJI_RE, '').replace(/\s+/g, '-');
|
|
16
|
+
}
|
|
7
17
|
/**
|
|
8
18
|
* Converts markdown headings with custom ID syntax into JSX heading elements.
|
|
9
19
|
*
|
|
@@ -40,7 +50,7 @@ export function preprocessCustomHeadingIds(content, options) {
|
|
|
40
50
|
const level = heading[1].length;
|
|
41
51
|
const sanitizedId = (options === null || options === void 0 ? void 0 : options.preserveOriginal)
|
|
42
52
|
? sanitizeHtmlAttr(heading[3])
|
|
43
|
-
:
|
|
53
|
+
: normalizeCustomId(heading[3]);
|
|
44
54
|
result.push(`<h${level} id="${sanitizedId}">`, heading[2], `</h${level}>`);
|
|
45
55
|
continue;
|
|
46
56
|
}
|
|
@@ -62,6 +62,8 @@ export const resolveComponentWithContent = (tree, componentName, snippet, export
|
|
|
62
62
|
const replaceVariablesWithProps = (node, treeToInject, exportMap, snippetExportMap) => {
|
|
63
63
|
const variableNameMap = {};
|
|
64
64
|
const variablesMap = {};
|
|
65
|
+
const propValues = getSnippetPropValues(node);
|
|
66
|
+
replaceCodePlaceholdersWithProps(treeToInject, propValues);
|
|
65
67
|
visit(treeToInject, (node) => {
|
|
66
68
|
var _a;
|
|
67
69
|
if ((node.type === 'mdxTextExpression' || node.type === 'mdxFlowExpression') &&
|
|
@@ -107,6 +109,30 @@ const replaceVariablesWithProps = (node, treeToInject, exportMap, snippetExportM
|
|
|
107
109
|
snippetNodesToInject: treeToInject.children,
|
|
108
110
|
};
|
|
109
111
|
};
|
|
112
|
+
const getSnippetPropValues = (node) => {
|
|
113
|
+
const propValues = {};
|
|
114
|
+
node.attributes.forEach((attribute) => {
|
|
115
|
+
if (attribute.type !== 'mdxJsxAttribute' ||
|
|
116
|
+
!attribute.value ||
|
|
117
|
+
typeof attribute.value !== 'string')
|
|
118
|
+
return;
|
|
119
|
+
propValues[attribute.name] = attribute.value;
|
|
120
|
+
});
|
|
121
|
+
return propValues;
|
|
122
|
+
};
|
|
123
|
+
const replaceCodePlaceholdersWithProps = (tree, propValues) => {
|
|
124
|
+
visit(tree, (node) => {
|
|
125
|
+
if (node.type !== 'code' || typeof node.value !== 'string') {
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
node.value = node.value.replace(/\{\s*([A-Za-z_$][\w$]*)\s*\}/g, (match, prop) => {
|
|
129
|
+
var _a;
|
|
130
|
+
if (!(prop in propValues))
|
|
131
|
+
return match;
|
|
132
|
+
return (_a = propValues[prop]) !== null && _a !== void 0 ? _a : match;
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
};
|
|
110
136
|
const reinsertExports = (content, exportMap, snippetExportMap) => {
|
|
111
137
|
const nodesToInject = [];
|
|
112
138
|
for (const [key, value] of Object.entries(snippetExportMap)) {
|