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