@clidey/dory 0.11.0 → 0.13.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/package.json +1 -1
- package/src/plugins/sanitize.ts +31 -5
package/package.json
CHANGED
package/src/plugins/sanitize.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { visit } from 'unist-util-visit';
|
|
2
2
|
|
|
3
|
+
// This plugin replaces mdxTextExpression nodes with inlineCode for safety.
|
|
3
4
|
export const remarkSafeVars = () => {
|
|
4
5
|
return (tree: any) => {
|
|
5
6
|
visit(tree, 'mdxTextExpression', (node: any, index: number | undefined, parent: any) => {
|
|
6
7
|
if (parent && typeof index === 'number') {
|
|
7
|
-
// Replace mdxTextExpression with inlineCode node for code formatting
|
|
8
8
|
parent.children[index] = {
|
|
9
9
|
type: 'inlineCode',
|
|
10
10
|
value: node.value
|
|
@@ -33,6 +33,16 @@ const KNOWN_COMPONENTS = [
|
|
|
33
33
|
'address', 'cite', 'q', 'dfn'
|
|
34
34
|
];
|
|
35
35
|
|
|
36
|
+
// Helper: returns true if the given code string is inside a code block (```)
|
|
37
|
+
function isInsideCodeBlock(code: string, idx: number): boolean {
|
|
38
|
+
// Count the number of ``` before idx
|
|
39
|
+
const before = code.slice(0, idx);
|
|
40
|
+
const codeBlockMatches = before.match(/```/g);
|
|
41
|
+
// Odd number of ``` means inside a code block
|
|
42
|
+
return codeBlockMatches ? codeBlockMatches.length % 2 === 1 : false;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Allow <service> and any <[a-z][a-z0-9]*> tag inside code blocks to be left as-is (not parsed as JSX).
|
|
36
46
|
export function preprocessMdxTags() {
|
|
37
47
|
return {
|
|
38
48
|
name: 'preprocess-mdx-tags',
|
|
@@ -40,10 +50,26 @@ export function preprocessMdxTags() {
|
|
|
40
50
|
transform(code: string, id: string) {
|
|
41
51
|
if (!id.endsWith('.mdx')) return;
|
|
42
52
|
|
|
43
|
-
//
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
53
|
+
// Remove leading whitespace from code fence blocks
|
|
54
|
+
let processed = code.replace(/^(\s+)(```\w*)/gm, '$2');
|
|
55
|
+
|
|
56
|
+
// Only replace unrecognized <someTag> with `"<someTag>"` if NOT inside a code block
|
|
57
|
+
// This regex finds all <tag> occurrences
|
|
58
|
+
processed = processed.replace(/<([a-z][a-z0-9]*)>/gi, (match, tag, offset) => {
|
|
59
|
+
// If inside a code block, do not touch
|
|
60
|
+
if (isInsideCodeBlock(processed, offset)) {
|
|
61
|
+
return match;
|
|
62
|
+
}
|
|
63
|
+
// If known component, leave as-is
|
|
64
|
+
if (KNOWN_COMPONENTS.includes(tag)) {
|
|
65
|
+
return match;
|
|
66
|
+
}
|
|
67
|
+
// If tag is 'service', always leave as-is (do not wrap in backticks)
|
|
68
|
+
if (tag === 'service') {
|
|
69
|
+
return match;
|
|
70
|
+
}
|
|
71
|
+
// Otherwise, wrap in backticks to prevent JSX parsing
|
|
72
|
+
return `\`<${tag}>\``;
|
|
47
73
|
});
|
|
48
74
|
|
|
49
75
|
return {
|