@@ -7,57 +7,112 @@ const pageList = await loadPages(undefined, ['en'])
77const pages = await loadPageMap ( pageList )
88const redirects = await loadRedirects ( pageList )
99
10+ const liquidStartRex = / ^ { % - ? \s * i f v e r s i o n .+ ?\s * % } /
11+ const liquidEndRex = / { % - ? \s * e n d i f \s * - ? % } $ /
12+
13+ // Return
14+ //
15+ // /foo/bar
16+ //
17+ // if the text input was
18+ //
19+ // {% ifversion ghes%}/foo/bar{%endif %}
20+ //
21+ // And if no liquid, just return as is.
22+ function stripLiquid ( text ) {
23+ if ( liquidStartRex . test ( text ) && liquidEndRex . test ( text ) ) {
24+ return text . replace ( liquidStartRex , '' ) . replace ( liquidEndRex , '' ) . trim ( )
25+ } else if ( text . includes ( '{' ) ) {
26+ throw new Error ( `Unsupported Liquid in frontmatter link list (${ text } )` )
27+ }
28+ return text
29+ }
30+
1031describe ( 'front matter' , ( ) => {
32+ // Given a URI that does not start with a specific language,
33+ // return undefined if it can found as a known page.
34+ // Otherwise, return an object with information that is used to
35+ // print a useful jest error message in the assertion.
36+ function checkURL ( uri , index , redirectsContext ) {
37+ const url = `/en${ stripLiquid ( uri ) . split ( '#' ) [ 0 ] } `
38+ if ( ! ( url in pages ) ) {
39+ // Some are written without a version, but don't work with the
40+ // default version.
41+ let redirects = getRedirect ( url , redirectsContext )
42+ // If it does indeed redirect to a different version,
43+ // strip that and compare again.
44+ if ( redirects ) {
45+ const withoutVersion = getPathWithoutVersion ( redirects )
46+ if ( withoutVersion === url ) {
47+ // That means, it's actually fine
48+ return null
49+ }
50+ redirects = getPathWithoutLanguage ( withoutVersion )
51+ }
52+ return { uri, index, redirects }
53+ }
54+ return null // Falsy value will be filtered out later
55+ }
56+
57+ function makeCustomErrorMessage ( page , trouble , key ) {
58+ let customErrorMessage = `In the front matter of ${ page . relativePath } `
59+ if ( trouble . length > 0 ) {
60+ if ( trouble . length === 1 ) {
61+ customErrorMessage += `there is 1 .${ key } front matter entry that is not correct.`
62+ } else {
63+ customErrorMessage += `there are ${ trouble . length } .${ key } front matter entries that are not correct.`
64+ }
65+ const nonWarnings = trouble . filter ( ( t ) => ! t . warning )
66+ for ( const { uri, index, redirects } of nonWarnings ) {
67+ customErrorMessage += `\nindex: ${ index } URI: ${ uri } `
68+ if ( redirects ) {
69+ customErrorMessage += `\n\tredirects to ${ redirects } `
70+ } else {
71+ customErrorMessage += '\tPage not found'
72+ }
73+ }
74+ if ( trouble . find ( ( t ) => t . redirects ) ) {
75+ customErrorMessage += `\n\nNOTE! To automatically fix the redirects run this command:\n`
76+ customErrorMessage += `\n\t./scripts/update-internal-links.js content/${ page . relativePath } \n\n`
77+ }
78+ }
79+ return customErrorMessage
80+ }
81+
82+ // Test content with .includeGuides front matter
1183 const pagesWithIncludeGuides = pageList . filter ( ( page ) => page . includeGuides )
1284 test . each ( pagesWithIncludeGuides ) (
1385 '$relativePath .includeGuides have pristine links' ,
1486 async ( page ) => {
1587 const redirectsContext = { redirects, pages }
1688
1789 const trouble = page . includeGuides
18- . map ( ( uri , i ) => {
19- const url = `/en${ uri } `
20- if ( ! ( url in pages ) ) {
21- // Some are written without a verion, but don't work with the
22- // default version.
23- let redirects = getRedirect ( url , redirectsContext )
24- // If it does indeed redirect to a different version,
25- // strip that and compare again.
26- if ( redirects ) {
27- const withoutVersion = getPathWithoutVersion ( redirects )
28- if ( withoutVersion === url ) {
29- // That means, it's actually fine
30- return null
31- }
32- redirects = getPathWithoutLanguage ( withoutVersion )
33- }
34- return { uri, index : i , redirects }
35- }
36- return null // Falsy value will be filtered out later
37- } )
90+ . map ( ( uri , i ) => checkURL ( uri , i , redirectsContext ) )
3891 . filter ( Boolean )
3992
40- let customErrorMessage = `In the front matter of ${ page . relativePath } `
41- if ( trouble . length > 0 ) {
42- if ( trouble . length === 1 ) {
43- customErrorMessage += `there is 1 .includeGuides front matter entry that is not correct.`
44- } else {
45- customErrorMessage += `there are ${ trouble . length } .includeGuides front matter entries that are not correct.`
46- }
47- const nonWarnings = trouble . filter ( ( t ) => ! t . warning )
48- for ( const { uri , index , redirects } of nonWarnings ) {
49- customErrorMessage += `\nindex: ${ index } URI: ${ uri } `
50- if ( redirects ) {
51- customErrorMessage += `\n\tredirects to ${ redirects } `
52- } else {
53- customErrorMessage += '\tPage not found'
54- }
55- }
56- if ( trouble . find ( ( t ) => t . redirects ) ) {
57- customErrorMessage += `\n\nNOTE! To automatically fix the redirects run this command:\n`
58- customErrorMessage += `\n\t./scripts/update-internal-links.js ${ page . relativePath } \n\n`
59- }
93+ const customErrorMessage = makeCustomErrorMessage ( page , trouble , 'includeGuides' )
94+ expect ( trouble . length , customErrorMessage ) . toEqual ( 0 )
95+ } ,
96+ )
97+
98+ // Test content with .featuredLinks front matter
99+
100+ const pagesWithFeaturedLinks = pageList . filter ( ( page ) => page . featuredLinks )
101+ test . each ( pagesWithFeaturedLinks ) (
102+ '$relativePath .featuredLinks have pristine links' ,
103+ async ( page ) => {
104+ const redirectsContext = { redirects, pages }
105+
106+ const trouble = [ ]
107+ for ( const links of Object . values ( page . featuredLinks ) ) {
108+ // Some thing in `.featuredLinks` are not arrays.
109+ // For example `popularHeading`. So just skip them.
110+ if ( ! Array . isArray ( links ) ) continue
111+
112+ trouble . push ( ... links . map ( ( uri , i ) => checkURL ( uri , i , redirectsContext ) ) . filter ( Boolean ) )
60113 }
114+
115+ const customErrorMessage = makeCustomErrorMessage ( page , trouble , 'featuredLinks' )
61116 expect ( trouble . length , customErrorMessage ) . toEqual ( 0 )
62117 } ,
63118 )
0 commit comments