22
33import { describe , expect , it } from 'vitest'
44
5- import { convertMarkdownToHtml , subscriptify } from './gen-html'
5+ import type { Config } from './config'
6+ import { Context } from './context'
7+ import { convertMarkdownToHtml , generateHtml , subscriptify } from './gen-html'
8+ import { parseMarkdownPageContent } from './parse'
9+
10+ const config : Config = {
11+ mode : 'development' ,
12+ baseProjDir : 'xxx' ,
13+ sourceDir : 'xxx' ,
14+ outDir : 'xxx' ,
15+ version : '25.1.0' ,
16+ langs : [ { code : 'de' , version : '25.1.0' } ] ,
17+ formats : [ ] ,
18+ template : 'default' ,
19+ author : 'Climate Interactive' ,
20+ logoPath : 'xxx' ,
21+ defs : [ ] ,
22+ pages : [ 'page_1.md' ] ,
23+ untranslated : [ ] ,
24+ options : { }
25+ }
26+
27+ describe ( 'generateHtml' , ( ) => {
28+ it ( 'should convert valid Markdown' , ( ) => {
29+ const md = `\
30+ This is a valid normal link: [page](https://climateinteractive.org)
31+
32+ This is a valid reference-style link: [page][ref]
33+
34+ This is a valid normal link: [page](https://climateinteractive.org) (with parentheses after) and more text
35+
36+ This is a valid reference-style link: [page][ref] (with parentheses after) and more text
37+
38+ [ref]: https://climateinteractive.org
39+ `
40+
41+ const html = generateHtml ( new Context ( config , 'en' ) , 'page_1.md' , { raw : md } )
42+ expect ( html . baseName ) . toBe ( 'page_1' )
43+ expect ( html . relPath ) . toBe ( 'page_1.html' )
44+ expect ( html . body ) . toBe ( `\
45+ <p>This is a valid normal link: <a href="https://climateinteractive.org">page</a></p>
46+ <p>This is a valid reference-style link: <a href="https://climateinteractive.org">page</a></p>
47+ <p>This is a valid normal link: <a href="https://climateinteractive.org">page</a> (with parentheses after) and more text</p>
48+ <p>This is a valid reference-style link: <a href="https://climateinteractive.org">page</a> (with parentheses after) and more text</p>
49+ ` )
50+ } )
51+
52+ it ( 'should throw an error if invalid link syntax is detected' , ( ) => {
53+ const links = `\
54+ This is a valid normal link: [page](https://climateinteractive.org)
55+
56+ This is a valid reference-style link: [page][ref]
57+
58+ This is a valid normal link: [page](https://climateinteractive.org) (with parentheses after) and more text
59+
60+ This is a valid reference-style link: [page][ref] (with parentheses after) and more text
61+
62+ This is an invalid normal link: [page] (https://climateinteractive.org) (with parentheses after) and more text
63+
64+ This is an invalid reference-style link: [page] [ref] (with parentheses after) and more text
65+ `
66+
67+ const md = `\
68+ # <!-- section:section_1 -->Section 1
69+
70+ <!-- begin-def:block_1 -->
71+
72+ ${ links }
73+
74+ <!-- end-def -->
75+
76+ [ref]: https://climateinteractive.org
77+ `
78+
79+ // Verify that an error is thrown if the English content contains invalid link syntax.
80+ // Note that in the English case, the invalid ref link will be converted to an HTML link.
81+ const enContext = new Context ( config , 'en' )
82+ const enMd = parseMarkdownPageContent ( enContext , 'page_1.md' , md )
83+ expect ( ( ) => generateHtml ( enContext , 'page_1.md' , { raw : enMd . raw } ) ) . toThrow ( `\
84+ Detected invalid Markdown link syntax in the generated HTML:
85+ [page] (<a href
86+ [page] <a href
87+ To fix, ensure there are no spaces between link text and link url/reference, for example: [text](url) or [text][ref] (page=page_1.md)` )
88+
89+ // Verify that an error is thrown if the translated content contains invalid link syntax.
90+ // Note that in the non-English case, the invalid ref link target will not be converted
91+ // to an HTML link (unlike the English case above), so the error message will be different.
92+ const deContext = enContext . derive (
93+ 'de' ,
94+ new Map ( [
95+ [ 'section_1__title' , 'Section 1' ] ,
96+ [ 'section_1__block_1' , links ]
97+ ] )
98+ )
99+ const deMd = parseMarkdownPageContent ( deContext , 'page_1.md' , md )
100+ expect ( ( ) => generateHtml ( deContext , 'page_1.md' , { raw : deMd . raw } ) ) . toThrow ( `\
101+ Detected invalid Markdown link syntax in the generated HTML:
102+ [page] (<a href
103+ [page] [ref]
104+ To fix, ensure there are no spaces between link text and link url/reference, for example: [text](url) or [text][ref] (lang=de page=page_1.md)` )
105+ } )
106+ } )
6107
7108describe ( 'subscriptify' , ( ) => {
8109 it ( 'should convert chemical formulas' , ( ) => {
@@ -29,7 +130,7 @@ describe('convertMarkdownToHtml', () => {
29130 '<p>This is -CO<sub>2</sub>-</p>\n'
30131 )
31132 expect ( convertMarkdownToHtml ( undefined , '# This is CO2' ) ) . toBe (
32- '<h1 id="this-is-co2" >This is CO<sub>2</sub></h1>\n'
133+ '<h1>This is CO<sub>2</sub></h1>\n'
33134 )
34135 expect ( convertMarkdownToHtml ( undefined , '> This is _CO2_' ) ) . toBe (
35136 '<blockquote>\n<p>This is <em>CO<sub>2</sub></em></p>\n</blockquote>\n'
0 commit comments