11import { builders } from 'prosemirror-test-builder' ;
2+ import dd from 'ts-dedent' ;
23
4+ import type { DirectiveSyntaxValue } from '../../../' ;
35import { parseDOM } from '../../../../tests/parse-dom' ;
46import { createMarkupChecker } from '../../../../tests/sameMarkup' ;
7+ import { DirectiveContext } from '../../../../tests/utils' ;
58import { ExtensionsManager } from '../../../core' ;
69import { BaseNode , BaseSchemaSpecs } from '../../base/specs' ;
710
8- import { YfmFileSpecs , yfmFileNodeName } from './YfmFileSpecs' ;
11+ import { YfmFileAttr , YfmFileSpecs , yfmFileNodeName } from './YfmFileSpecs' ;
912
10- const {
11- schema,
12- markupParser : parser ,
13- serializer,
14- } = new ExtensionsManager ( {
15- extensions : ( builder ) => builder . use ( BaseSchemaSpecs , { } ) . use ( YfmFileSpecs ) ,
16- } ) . buildDeps ( ) ;
13+ function buildDeps ( directiveSyntax : DirectiveSyntaxValue | undefined ) {
14+ return new ExtensionsManager ( {
15+ options : { mdOpts : { preset : 'zero' } } ,
16+ extensions : ( builder ) => {
17+ builder . context . set ( 'directiveSyntax' , new DirectiveContext ( directiveSyntax ) ) ;
18+ builder . use ( BaseSchemaSpecs , { } ) . use ( YfmFileSpecs ) ;
19+ } ,
20+ } ) . buildDeps ( ) ;
21+ }
22+
23+ function buildCheckers ( directiveSyntax : DirectiveSyntaxValue ) {
24+ const { markupParser, serializer} = buildDeps ( directiveSyntax ) ;
25+ return createMarkupChecker ( { parser : markupParser , serializer} ) ;
26+ }
27+
28+ const { schema, markupParser : parser , serializer} = buildDeps ( undefined ) ;
1729
1830const { same} = createMarkupChecker ( { parser, serializer} ) ;
1931
@@ -24,13 +36,14 @@ const {doc, p, file} = builders<'doc' | 'p' | 'file'>(schema, {
2436} ) ;
2537
2638const defaultAttrs = {
27- href : 'path/to/readme' ,
28- download : 'readme.md' ,
29- hreflang : null ,
30- referrerpolicy : null ,
31- rel : null ,
32- target : null ,
33- type : null ,
39+ [ YfmFileAttr . Link ] : 'path/to/readme' ,
40+ [ YfmFileAttr . Name ] : 'readme.md' ,
41+ [ YfmFileAttr . Lang ] : null ,
42+ [ YfmFileAttr . ReferrerPolicy ] : null ,
43+ [ YfmFileAttr . Rel ] : null ,
44+ [ YfmFileAttr . Target ] : null ,
45+ [ YfmFileAttr . Type ] : null ,
46+ [ YfmFileAttr . Markup ] : '{% file ' ,
3447} ;
3548
3649describe ( 'YFM File extension' , ( ) => {
@@ -65,13 +78,14 @@ describe('YFM File extension', () => {
6578 doc (
6679 p (
6780 file ( {
68- href : 'path/to/readme' ,
69- download : 'readme.md' ,
70- hreflang : 'ru' ,
71- referrerpolicy : 'origin' ,
72- rel : 'help' ,
73- target : '_top' ,
74- type : 'text/markdown' ,
81+ [ YfmFileAttr . Link ] : 'path/to/readme' ,
82+ [ YfmFileAttr . Name ] : 'readme.md' ,
83+ [ YfmFileAttr . Lang ] : 'ru' ,
84+ [ YfmFileAttr . ReferrerPolicy ] : 'origin' ,
85+ [ YfmFileAttr . Rel ] : 'help' ,
86+ [ YfmFileAttr . Target ] : '_top' ,
87+ [ YfmFileAttr . Type ] : 'text/markdown' ,
88+ [ YfmFileAttr . Markup ] : '{% file ' ,
7589 } ) ,
7690 ) ,
7791 ) ,
@@ -93,4 +107,181 @@ describe('YFM File extension', () => {
93107 ) ,
94108 ) ;
95109 } ) ;
110+
111+ describe ( 'directiveSyntax' , ( ) => {
112+ const MARKUP = {
113+ CurlySyntax : dd `
114+ {% file src="path/to/readme.md" name="README.md" lang="ru" referrerpolicy="origin" rel="help" target="_top" type="text/markdown" %}
115+ ` ,
116+
117+ DirectiveSyntax : dd `
118+ :file[README.md](path/to/readme.md){referrerpolicy="origin" rel="help" target="_top" type="text/markdown" hreflang="ru"}
119+ ` ,
120+ } ;
121+
122+ const ATTRS = {
123+ [ YfmFileAttr . Name ] : 'README.md' ,
124+ [ YfmFileAttr . Link ] : 'path/to/readme.md' ,
125+ [ YfmFileAttr . ReferrerPolicy ] : 'origin' ,
126+ [ YfmFileAttr . Rel ] : 'help' ,
127+ [ YfmFileAttr . Target ] : '_top' ,
128+ [ YfmFileAttr . Type ] : 'text/markdown' ,
129+ [ YfmFileAttr . Lang ] : 'ru' ,
130+ } ;
131+
132+ const PM_DOC = {
133+ CurlyFile : doc (
134+ p (
135+ file ( {
136+ ...ATTRS ,
137+ [ YfmFileAttr . Markup ] : '{% file ' ,
138+ } ) ,
139+ ) ,
140+ ) ,
141+ UnknownFile : doc ( p ( file ( { ...ATTRS } ) ) ) ,
142+ DirectiveFile : doc (
143+ p (
144+ file ( {
145+ ...ATTRS ,
146+ [ YfmFileAttr . Markup ] : ':file' ,
147+ } ) ,
148+ ) ,
149+ ) ,
150+ } ;
151+
152+ describe ( 'directiveSyntax:disabled' , ( ) => {
153+ it ( 'should parse curly syntax' , ( ) => {
154+ const { parse} = buildCheckers ( 'disabled' ) ;
155+ parse ( MARKUP . CurlySyntax , PM_DOC . CurlyFile , { json : true } ) ;
156+ } ) ;
157+
158+ it ( 'should not parse directive syntax' , ( ) => {
159+ const { parse} = buildCheckers ( 'disabled' ) ;
160+ parse ( MARKUP . DirectiveSyntax , doc ( p ( MARKUP . DirectiveSyntax ) ) , { json : true } ) ;
161+ } ) ;
162+
163+ it ( 'should preserve curly syntax' , ( ) => {
164+ const { serialize} = buildCheckers ( 'disabled' ) ;
165+ serialize ( PM_DOC . CurlyFile , MARKUP . CurlySyntax ) ;
166+ } ) ;
167+
168+ it ( 'should serialize block with unknown markup to curly syntax' , ( ) => {
169+ const { serialize} = buildCheckers ( 'disabled' ) ;
170+ serialize ( PM_DOC . UnknownFile , MARKUP . CurlySyntax ) ;
171+ } ) ;
172+
173+ it ( 'should preserve directive syntax' , ( ) => {
174+ const { serialize} = buildCheckers ( 'disabled' ) ;
175+ serialize ( PM_DOC . DirectiveFile , MARKUP . DirectiveSyntax ) ;
176+ } ) ;
177+ } ) ;
178+
179+ describe ( 'directiveSyntax:enabled' , ( ) => {
180+ it ( 'should parse curly syntax' , ( ) => {
181+ const { parse} = buildCheckers ( 'enabled' ) ;
182+ parse ( MARKUP . CurlySyntax , PM_DOC . CurlyFile , { json : true } ) ;
183+ } ) ;
184+
185+ it ( 'should parse directive syntax' , ( ) => {
186+ const { parse} = buildCheckers ( 'enabled' ) ;
187+ parse ( MARKUP . DirectiveSyntax , PM_DOC . DirectiveFile , { json : true } ) ;
188+ } ) ;
189+
190+ it ( 'should preserve curly syntax' , ( ) => {
191+ const { serialize} = buildCheckers ( 'enabled' ) ;
192+ serialize ( PM_DOC . CurlyFile , MARKUP . CurlySyntax ) ;
193+ } ) ;
194+
195+ it ( 'should serialize block with unknown markup to curly syntax' , ( ) => {
196+ const { serialize} = buildCheckers ( 'enabled' ) ;
197+ serialize ( PM_DOC . UnknownFile , MARKUP . CurlySyntax ) ;
198+ } ) ;
199+
200+ it ( 'should preserve directive syntax' , ( ) => {
201+ const { serialize} = buildCheckers ( 'enabled' ) ;
202+ serialize ( PM_DOC . DirectiveFile , MARKUP . DirectiveSyntax ) ;
203+ } ) ;
204+ } ) ;
205+
206+ describe ( 'directiveSyntax:preserve' , ( ) => {
207+ it ( 'should parse curly syntax' , ( ) => {
208+ const { parse} = buildCheckers ( 'preserve' ) ;
209+ parse ( MARKUP . CurlySyntax , PM_DOC . CurlyFile , { json : true } ) ;
210+ } ) ;
211+
212+ it ( 'should parse directive syntax' , ( ) => {
213+ const { parse} = buildCheckers ( 'preserve' ) ;
214+ parse ( MARKUP . DirectiveSyntax , PM_DOC . DirectiveFile , { json : true } ) ;
215+ } ) ;
216+
217+ it ( 'should preserve curly syntax' , ( ) => {
218+ const { serialize} = buildCheckers ( 'preserve' ) ;
219+ serialize ( PM_DOC . CurlyFile , MARKUP . CurlySyntax ) ;
220+ } ) ;
221+
222+ it ( 'should serialize block with unknown markup to directive syntax' , ( ) => {
223+ const { serialize} = buildCheckers ( 'preserve' ) ;
224+ serialize ( PM_DOC . UnknownFile , MARKUP . DirectiveSyntax ) ;
225+ } ) ;
226+
227+ it ( 'should preserve directive syntax' , ( ) => {
228+ const { serialize} = buildCheckers ( 'preserve' ) ;
229+ serialize ( PM_DOC . DirectiveFile , MARKUP . DirectiveSyntax ) ;
230+ } ) ;
231+ } ) ;
232+
233+ describe ( 'directiveSyntax:overwrite' , ( ) => {
234+ it ( 'should parse curly syntax' , ( ) => {
235+ const { parse} = buildCheckers ( 'overwrite' ) ;
236+ parse ( MARKUP . CurlySyntax , PM_DOC . CurlyFile , { json : true } ) ;
237+ } ) ;
238+
239+ it ( 'should parse directive syntax' , ( ) => {
240+ const { parse} = buildCheckers ( 'overwrite' ) ;
241+ parse ( MARKUP . DirectiveSyntax , PM_DOC . DirectiveFile , { json : true } ) ;
242+ } ) ;
243+
244+ it ( 'should overwrite curly to directive syntax' , ( ) => {
245+ const { serialize} = buildCheckers ( 'overwrite' ) ;
246+ serialize ( PM_DOC . CurlyFile , MARKUP . DirectiveSyntax ) ;
247+ } ) ;
248+
249+ it ( 'should serialize block with unknown markup to directive syntax' , ( ) => {
250+ const { serialize} = buildCheckers ( 'overwrite' ) ;
251+ serialize ( PM_DOC . UnknownFile , MARKUP . DirectiveSyntax ) ;
252+ } ) ;
253+
254+ it ( 'should preserve directive syntax' , ( ) => {
255+ const { serialize} = buildCheckers ( 'overwrite' ) ;
256+ serialize ( PM_DOC . DirectiveFile , MARKUP . DirectiveSyntax ) ;
257+ } ) ;
258+ } ) ;
259+
260+ describe ( 'directiveSyntax:only' , ( ) => {
261+ it ( 'should not parse curly syntax' , ( ) => {
262+ const { parse} = buildCheckers ( 'only' ) ;
263+ parse ( MARKUP . CurlySyntax , doc ( p ( MARKUP . CurlySyntax ) ) , { json : true } ) ;
264+ } ) ;
265+
266+ it ( 'should parse directive syntax' , ( ) => {
267+ const { parse} = buildCheckers ( 'only' ) ;
268+ parse ( MARKUP . DirectiveSyntax , PM_DOC . DirectiveFile , { json : true } ) ;
269+ } ) ;
270+
271+ it ( 'should overwrite curly to directive syntax' , ( ) => {
272+ const { serialize} = buildCheckers ( 'only' ) ;
273+ serialize ( PM_DOC . CurlyFile , MARKUP . DirectiveSyntax ) ;
274+ } ) ;
275+
276+ it ( 'should serialize block with unknown markup to directive syntax' , ( ) => {
277+ const { serialize} = buildCheckers ( 'only' ) ;
278+ serialize ( PM_DOC . UnknownFile , MARKUP . DirectiveSyntax ) ;
279+ } ) ;
280+
281+ it ( 'should preserve directive syntax' , ( ) => {
282+ const { serialize} = buildCheckers ( 'only' ) ;
283+ serialize ( PM_DOC . DirectiveFile , MARKUP . DirectiveSyntax ) ;
284+ } ) ;
285+ } ) ;
286+ } ) ;
96287} ) ;
0 commit comments