Skip to content

Commit b38eb4e

Browse files
committed
feat: Enhance Docmach wrapper tag processing to restore code blocks before markdown rendering and rename internal placeholders.
1 parent 294d677 commit b38eb4e

1 file changed

Lines changed: 11 additions & 4 deletions

File tree

src/compiler.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ function protectCodeBlocks(content: string): {
2828

2929
// Protect fenced code blocks (```...```)
3030
let protectedContent = content.replace(/```[\s\S]*?```/g, (match) => {
31-
const placeholder = `___CODE_BLOCK_${index}___`;
31+
const placeholder = `DOCMACH_CODE_BLOCK_${index}`;
3232
codeBlocks.set(placeholder, match);
3333
index++;
3434
return placeholder;
3535
});
3636

3737
// Protect inline code (`...`)
3838
protectedContent = protectedContent.replace(/`[^`\n]+`/g, (match) => {
39-
const placeholder = `___INLINE_CODE_${index}___`;
39+
const placeholder = `DOCMACH_INLINE_CODE_${index}`;
4040
codeBlocks.set(placeholder, match);
4141
index++;
4242
return placeholder;
@@ -152,7 +152,11 @@ function parseParams(
152152
return params;
153153
}
154154

155-
async function processWrapperTags(fileContent: string, filePath: string) {
155+
async function processWrapperTags(
156+
fileContent: string,
157+
filePath: string,
158+
codeBlocks: Map<string, string>
159+
) {
156160
const replacements: { original: string; replacement: string }[] = [];
157161
const wrapperRegex = /<docmach\b([^>]*?)\s*([^/])>([\s\S]*?)<\/docmach>/g;
158162

@@ -212,6 +216,7 @@ async function processWrapperTags(fileContent: string, filePath: string) {
212216
);
213217
});
214218
}
219+
innerContent = restoreCodeBlocks(innerContent, codeBlocks);
215220
innerContent = md.render(innerContent);
216221
const replaced = templateContent.replace(
217222
new RegExp(`{{\\s*${attrs["replacement"]}\\s*}}`),
@@ -405,7 +410,9 @@ export async function compileFile(filePath: string): Promise<string> {
405410
replacements.push(
406411
...(await processSelfClosingTags(processedContent, filePath))
407412
);
408-
replacements.push(...(await processWrapperTags(processedContent, filePath)));
413+
replacements.push(
414+
...(await processWrapperTags(processedContent, filePath, codeBlocks))
415+
);
409416

410417
replacements.reverse().forEach(({ original, replacement }) => {
411418
const placeholder = `<!--DOCMACH_PLACEHOLDER_${placeholderIndex}-->`;

0 commit comments

Comments
 (0)