11import { describe , expect , it } from "vite-plus/test" ;
2+ import remarkParse from "remark-parse" ;
3+ import { unified } from "unified" ;
24
35import {
46 fileBasename ,
@@ -8,6 +10,116 @@ import {
810 splitFilePathPosition ,
911 workspaceRelativeFilePath ,
1012} from "./markdownLinks.ts" ;
13+ import { repairMarkdownFileLinks } from "./repairMarkdownFileLinks.ts" ;
14+ import {
15+ renderCodexDirectivesForCopy ,
16+ renderCodexFileCitationsAsMarkdown ,
17+ splitCodexArtifactTemplateMarkdown ,
18+ } from "./codexMarkdownDirectives.ts" ;
19+
20+ describe ( "repairMarkdownFileLinks" , ( ) => {
21+ it . each ( [
22+ "CONTRIBUTING.md" ,
23+ "local/path/file.md" ,
24+ "docs/My Folder/file.md" ,
25+ "./scripts/deploy" ,
26+ "~/notes/today.md" ,
27+ "/home/me/My Folder/file.md" ,
28+ "/tmp/reports/" ,
29+ "C:\\Users\\me\\My Folder\\file.md" ,
30+ "file:///home/me/file.md" ,
31+ "src/main.ts:12:5" ,
32+ ] ) ( "repairs a complete local destination: %s" , ( path ) => {
33+ const source = `- [file](<${ path } )` ;
34+ const rendered = repairMarkdownFileLinks ( source ) ;
35+ expect ( rendered ) . toBe ( `- [file](<${ path } >)` ) ;
36+ expect ( unified ( ) . use ( remarkParse ) . parse ( rendered ) . children [ 0 ] ) . toMatchObject ( {
37+ type : "list" ,
38+ children : [ { children : [ { children : [ { type : "link" } ] } ] } ] ,
39+ } ) ;
40+ expect ( repairMarkdownFileLinks ( rendered ) ) . toBe ( rendered ) ;
41+ } ) ;
42+
43+ it . each ( [
44+ "ordinary prose" ,
45+ "[file](<CONTRIBUTING.md>)" ,
46+ "[site](<https://example.com/docs)" ,
47+ "[route](</chat/settings)" ,
48+ "[prose](<not a file)" ,
49+ "[file](<src/main.ts" ,
50+ "[file](<src/main.ts>" ,
51+ "[file](<./file(one).md)" ,
52+ "[file](<./file.md))" ,
53+ "[file](<./file)name.md)" ,
54+ "[file](<./file\\).md)" ,
55+ "" ,
56+ "\\\\" ,
57+ "\\\\\\\\" ,
58+ "\\[file](<./file.md)" ,
59+ "\\\\\\[file](<./file.md)" ,
60+ "`[file](<./file.md)`" ,
61+ "``[file](<./file.md)``" ,
62+ "```md\n[file](<./file.md)\n```" ,
63+ "~~~md\n[file](<./file.md)\n~~~" ,
64+ " [file](<./file.md)" ,
65+ "<div>\n[file](<./file.md)\n</div>" ,
66+ "HTML <span>[file](<./file.md)</span>" ,
67+ '<span title="[file](<./file.md)">text</span>' ,
68+ "[outer [file](<./file.md)](https://example.com)" ,
69+ "[outer [file](<./file.md)][ref]\n\n[ref]: https://example.com" ,
70+ '[ref]: ./file.md "[file](<./other.md)"' ,
71+ ':codex-file-citation{path="./[file](<./other.md)" purpose="output"}' ,
72+ ':codex-file-citation{path="./[file](<./other.md)' ,
73+ '::artifact-template{skill_name="artifact-template-example" skill_directory="/tmp/skills/example" display_name="[file](<./file.md)" artifact_kind="document"}' ,
74+ '::artifact-template{display_name="[file](<./file.md)' ,
75+ ] ) ( "preserves protected or uncertain syntax: %s" , ( source ) => {
76+ expect ( repairMarkdownFileLinks ( source ) ) . toBe ( source ) ;
77+ } ) ;
78+
79+ it ( "uses CommonMark backslash parity for link openers" , ( ) => {
80+ expect ( repairMarkdownFileLinks ( "\\\\[file](<./file.md)" ) ) . toBe ( "\\\\[file](<./file.md>)" ) ;
81+ expect ( repairMarkdownFileLinks ( "\\\\\\\\[file](<./file.md)" ) ) . toBe (
82+ "\\\\\\\\[file](<./file.md>)" ,
83+ ) ;
84+ expect ( repairMarkdownFileLinks ( "\\" ) ) . toBe ( "\\" ) ;
85+ } ) ;
86+
87+ it ( "repairs ordinary segments after splitting mobile artifacts and preserves copy source" , ( ) => {
88+ const link = "[file](<./file.md)" ;
89+ const template =
90+ '::artifact-template{skill_name="artifact-template-example" skill_directory="/tmp/skills/example" display_name="[file](<./file.md)" artifact_kind="document"}' ;
91+ const citation = ':codex-file-citation{path="./report.md"}' ;
92+ const source = `${ link } \n\n${ template } \n\n${ citation } ` ;
93+ const segments = splitCodexArtifactTemplateMarkdown ( source ) ;
94+ const rendered = segments . map ( ( segment ) =>
95+ segment . kind === "markdown"
96+ ? {
97+ ...segment ,
98+ markdown : renderCodexFileCitationsAsMarkdown ( repairMarkdownFileLinks ( segment . markdown ) ) ,
99+ }
100+ : segment ,
101+ ) ;
102+ expect ( rendered [ 0 ] ) . toEqual ( {
103+ kind : "markdown" ,
104+ markdown : `${ link . slice ( 0 , - 1 ) } >)\n\n` ,
105+ sourceOffset : 0 ,
106+ } ) ;
107+ expect ( rendered [ 1 ] ) . toEqual ( segments [ 1 ] ) ;
108+ expect ( rendered [ 1 ] ) . toMatchObject ( {
109+ kind : "artifact-template" ,
110+ sourceOffset : link . length + 2 ,
111+ template : { displayName : link , skillName : "artifact-template-example" } ,
112+ } ) ;
113+ expect ( rendered [ 2 ] ) . toEqual ( {
114+ kind : "markdown" ,
115+ sourceOffset : source . indexOf ( "\n\n" + citation ) ,
116+ markdown : "\n\n[report.md](<./report.md>)" ,
117+ } ) ;
118+ expect ( renderCodexDirectivesForCopy ( source ) ) . toBe (
119+ `${ link } \n\n${ link } (Document template)\n\n[report.md](<./report.md>)` ,
120+ ) ;
121+ } ) ;
122+ } ) ;
11123
12124describe ( "inlineCodeFilePathCandidate" , ( ) => {
13125 it . each ( [
0 commit comments