Skip to content

Commit c68b34b

Browse files
vrcprlcherkanovart
andauthored
fix: upd code placeholder (#1760)
* fix: upd code placeholder * chore: add changeset --------- Co-authored-by: cherkanov_art <45258907+cherkanovart@users.noreply.github.com>
1 parent b875ca6 commit c68b34b

File tree

3 files changed

+32
-27
lines changed

3 files changed

+32
-27
lines changed

.changeset/polite-jobs-knock.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"lingo.dev": patch
3+
---
4+
5+
fix MDX code placeholder

packages/cli/src/cli/loaders/mdx2/code-placeholder.spec.ts

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import createMdxCodePlaceholderLoader from "./code-placeholder";
33
import dedent from "dedent";
44
import { md5 } from "../../utils/md5";
55

6-
const PLACEHOLDER_REGEX = /\{\/\* CODE_PLACEHOLDER_[0-9a-f]+\s*\*\/\}/g;
6+
const PLACEHOLDER_REGEX = /CODE_PLACEHOLDER_[0-9a-f]+_END/g;
77

88
const sampleContent = dedent`
99
Paragraph with some code:
@@ -19,8 +19,8 @@ describe("MDX Code Placeholder Loader", () => {
1919

2020
it("should replace fenced code with placeholder on pull", async () => {
2121
const result = await loader.pull("en", sampleContent);
22-
const hash = md5('```js\nconsole.log("foo");\n```');
23-
const expected = `Paragraph with some code:\n\n{/* CODE_PLACEHOLDER_${hash} */}`;
22+
const hash = md5('```js\nconsole.log("foo");\n```').slice(0, 16);
23+
const expected = `Paragraph with some code:\n\nCODE_PLACEHOLDER_${hash}_END`;
2424
expect(result.trim()).toBe(expected);
2525
});
2626

@@ -406,8 +406,8 @@ describe("MDX Code Placeholder Loader", () => {
406406
it("should replace inline code with placeholder on pull", async () => {
407407
const md = "This is some `inline()` code.";
408408
const pulled = await loader.pull("en", md);
409-
const hash = md5("`inline()`");
410-
const expected = `This is some {/* INLINE_CODE_PLACEHOLDER_${hash} */} code.`;
409+
const hash = md5("`inline()`").slice(0, 16);
410+
const expected = `This is some INLINE_CODE_PLACEHOLDER_${hash}_END code.`;
411411
expect(pulled).toBe(expected);
412412
});
413413

@@ -535,7 +535,7 @@ describe("MDX Code Placeholder Loader", () => {
535535
const pushed = await loader.push("en", translated);
536536

537537
// Should not contain any placeholders
538-
expect(pushed).not.toMatch(/\{\/\* CODE_PLACEHOLDER_[0-9a-f]+\s*\*\/\}/);
538+
expect(pushed).not.toMatch(/CODE_PLACEHOLDER_[0-9a-f]+_END/);
539539

540540
// Should preserve all special $ characters exactly as they were
541541
expect(pushed).toContain('const price = "$100";');
@@ -556,7 +556,7 @@ describe("MDX Code Placeholder Loader", () => {
556556
const pushed = await loader.push("en", translated);
557557

558558
// Should not contain any placeholders
559-
expect(pushed).not.toMatch(/\{\/\* INLINE_CODE_PLACEHOLDER_[0-9a-f]+\s*\*\/\}/);
559+
expect(pushed).not.toMatch(/INLINE_CODE_PLACEHOLDER_[0-9a-f]+_END/);
560560

561561
// Should preserve all special $ characters
562562
expect(pushed).toContain("`$price`");
@@ -577,8 +577,8 @@ describe("MDX Code Placeholder Loader", () => {
577577
const pushed = await loader.push("en", translated);
578578

579579
// Should not contain any placeholders
580-
expect(pushed).not.toMatch(/\{\/\* INLINE_CODE_PLACEHOLDER_[0-9a-f]+\s*\*\/\}/);
581-
expect(pushed).not.toMatch(/\{\/\* CODE_PLACEHOLDER_[0-9a-f]+\s*\*\/\}/);
580+
expect(pushed).not.toMatch(/INLINE_CODE_PLACEHOLDER_[0-9a-f]+_END/);
581+
expect(pushed).not.toMatch(/CODE_PLACEHOLDER_[0-9a-f]+_END/);
582582
expect(pushed).toContain("`getData()`");
583583
expect(pushed).toContain("Utilize");
584584
});
@@ -605,7 +605,7 @@ describe("MDX Code Placeholder Loader", () => {
605605

606606
// The fix: ALL placeholders should be replaced, including Arabic ones
607607
expect(pushedResult).not.toMatch(
608-
/\{\/\* INLINE_CODE_PLACEHOLDER_[0-9a-f]+\s*\*\/\}/,
608+
/INLINE_CODE_PLACEHOLDER_[0-9a-f]+_END/,
609609
);
610610
expect(pushedResult).not.toMatch(/\{\/\* CODE_PLACEHOLDER_[0-9a-f]+\s*\*\/\}/);
611611

@@ -639,7 +639,7 @@ describe("MDX Code Placeholder Loader", () => {
639639

640640
// All placeholders should be replaced, even when not in current pullInput
641641
expect(pushedResult).not.toMatch(
642-
/\{\/\* INLINE_CODE_PLACEHOLDER_[0-9a-f]+\s*\*\/\}/,
642+
/INLINE_CODE_PLACEHOLDER_[0-9a-f]+_END/,
643643
);
644644
expect(pushedResult).not.toMatch(/\{\/\* CODE_PLACEHOLDER_[0-9a-f]+\s*\*\/\}/);
645645
expect(pushedResult).toContain("`الحصول_على_البيانات()`");
@@ -737,7 +737,7 @@ describe("MDX Code Placeholder Loader", () => {
737737
// The code block should be present and not replaced with placeholder
738738
expect(dePushed).toContain("```bash");
739739
expect(dePushed).toContain("npm install");
740-
expect(dePushed).not.toMatch(/\{\/\* CODE_PLACEHOLDER_/);
740+
expect(dePushed).not.toMatch(/CODE_PLACEHOLDER_/);
741741
});
742742

743743
it("should preserve double newlines around placeholders for section splitting", async () => {
@@ -764,7 +764,7 @@ describe("MDX Code Placeholder Loader", () => {
764764
const pulled = await loader.pull("en", md);
765765

766766
// Verify placeholders are surrounded by double newlines for proper section splitting
767-
const placeholders = pulled.match(/\{\/\* CODE_PLACEHOLDER_[a-f0-9]+\s*\*\/\}/g);
767+
const placeholders = pulled.match(/CODE_PLACEHOLDER_[a-f0-9]+_END/g);
768768
expect(placeholders).toHaveLength(2);
769769

770770
// Check that each placeholder has double newlines around it
@@ -814,12 +814,12 @@ describe("adjacent code blocks bug", () => {
814814
console.log("___");
815815

816816
// The bug: placeholder is concatenated with "typescript" from next block
817-
const bugPattern = /\{\/\* CODE_PLACEHOLDER_[a-f0-9]+\s*\*\/\}typescript/;
817+
const bugPattern = /CODE_PLACEHOLDER_[a-f0-9]+\_ENDtypescript/;
818818
expect(pulled).not.toMatch(bugPattern);
819819

820820
// Should have proper separation
821821
expect(pulled).toMatch(
822-
/\{\/\* CODE_PLACEHOLDER_[a-f0-9]+\s*\*\/\}\n\n\{\/\* CODE_PLACEHOLDER_[a-f0-9]+\s*\*\/\}/,
822+
/CODE_PLACEHOLDER_[a-f0-9]+_END\n\nCODE_PLACEHOLDER_[a-f0-9]+_END/,
823823
);
824824
});
825825
});
@@ -885,13 +885,13 @@ describe("placeholder format edge cases (regression)", () => {
885885
const koreanMdx = `\`코드\` 디렉토리와 그 내용을 이해합니다.`;
886886
const pulled = await loader.pull("ko", koreanMdx);
887887

888-
expect(pulled).toMatch(/^\{\/\* INLINE_CODE_PLACEHOLDER/);
888+
expect(pulled).toMatch(/^INLINE_CODE_PLACEHOLDER/);
889889

890890
// Simulate frontmatter-split: matter.stringify with placeholder at start
891891
const matter = require('gray-matter');
892892
const mdxDocument = matter.stringify(pulled, { title: 'Test' });
893893

894-
expect(mdxDocument).toContain('{/*');
894+
expect(mdxDocument).toContain('INLINE_CODE_PLACEHOLDER');
895895

896896
const restored = await loader.push("ko", pulled, koreanMdx, "en", koreanMdx);
897897
expect(restored).toBe(koreanMdx);
@@ -918,8 +918,8 @@ describe("placeholder format edge cases (regression)", () => {
918918
const processor = unified().use(remarkParse).use(remarkStringify);
919919
const parsed = processor.stringify(processor.parse(pulled));
920920

921-
// JSX comments get escaped by Markdown but structure is preserved
922-
// Underscores in placeholder names get escaped: INLINE\_CODE\_PLACEHOLDER
921+
// Tag-style placeholders are preserved through markdown parsing
922+
// Underscores in placeholder names may get escaped: INLINE\_CODE\_PLACEHOLDER
923923
expect(parsed).toMatch(/INLINE[_\\]*CODE[_\\]*PLACEHOLDER/);
924924
expect(parsed).not.toContain('***'); // Not parsed as bold-italic
925925
expect(parsed).not.toMatch(/___CODE.*___/); // Not using underscore format
@@ -942,15 +942,15 @@ describe("placeholder format edge cases (regression)", () => {
942942
`;
943943

944944
const pulled = await loader.pull("en", mdContent);
945-
const placeholders = pulled.match(/\{\/\* INLINE_CODE_PLACEHOLDER_[a-f0-9]+\s*\*\/\}/g);
945+
const placeholders = pulled.match(/INLINE_CODE_PLACEHOLDER_[a-f0-9]+_END/g);
946946
expect(placeholders).toHaveLength(3);
947947

948948
const matter = require('gray-matter');
949949
const mdxDoc = matter.stringify(pulled, {
950950
title: 'Multiple Placeholders'
951951
});
952952

953-
expect(mdxDoc).toContain('{/*');
953+
expect(mdxDoc).toContain('INLINE_CODE_PLACEHOLDER');
954954

955955
const restored = await loader.push("en", pulled, mdContent, "en", mdContent);
956956
expect(restored).toBe(mdContent);
@@ -970,13 +970,13 @@ describe("placeholder format edge cases (regression)", () => {
970970
`;
971971

972972
const pulled = await loader.pull("en", mdContent);
973-
expect(pulled).toMatch(/^\{\/\* CODE_PLACEHOLDER/);
973+
expect(pulled).toMatch(/^CODE_PLACEHOLDER/);
974974

975975
const matter = require('gray-matter');
976976
const mdxDoc = matter.stringify(pulled, { title: 'Code First' });
977977

978978
expect(mdxDoc).toContain('title: Code First');
979-
expect(mdxDoc).toContain('{/*');
979+
expect(mdxDoc).toContain('CODE_PLACEHOLDER');
980980

981981
const restored = await loader.push("en", pulled, mdContent, "en", mdContent);
982982
expect(restored).toBe(mdContent);

packages/cli/src/cli/loaders/mdx2/code-placeholder.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ function extractCodePlaceholders(content: string): {
9898
const codeBlockMatches = finalContent.matchAll(fenceRegex);
9999
for (const match of codeBlockMatches) {
100100
const codeBlock = match[0];
101-
const codeBlockHash = md5(codeBlock);
102-
const placeholder = `{/* CODE_PLACEHOLDER_${codeBlockHash} */}`;
101+
const codeBlockHash = md5(codeBlock).slice(0, 16);
102+
const placeholder = `CODE_PLACEHOLDER_${codeBlockHash}_END`;
103103

104104
codePlaceholders[placeholder] = codeBlock;
105105

@@ -112,8 +112,8 @@ function extractCodePlaceholders(content: string): {
112112
const inlineCodeMatches = finalContent.matchAll(inlineCodeRegex);
113113
for (const match of inlineCodeMatches) {
114114
const inlineCode = match[0];
115-
const inlineCodeHash = md5(inlineCode);
116-
const placeholder = `{/* INLINE_CODE_PLACEHOLDER_${inlineCodeHash} */}`;
115+
const inlineCodeHash = md5(inlineCode).slice(0, 16);
116+
const placeholder = `INLINE_CODE_PLACEHOLDER_${inlineCodeHash}_END`;
117117
codePlaceholders[placeholder] = inlineCode;
118118
const replacement = placeholder;
119119
finalContent = finalContent.replace(inlineCode, () => replacement);

0 commit comments

Comments
 (0)