@@ -21,16 +21,18 @@ const frontmatter = require('./frontmatter')
2121const products = require ( './all-products' )
2222const slash = require ( 'slash' )
2323const statsd = require ( './statsd' )
24+ const fmfromf = require ( './read-frontmatter' )
2425const getLinkData = require ( './get-link-data' )
2526
2627class Page {
27- static init ( opts ) {
28- opts = Page . read ( opts )
28+ static async init ( opts ) {
29+ opts = await Page . read ( opts )
2930 if ( ! opts ) return
3031 return new Page ( opts )
3132 }
3233
33- static read ( opts ) {
34+ static async read ( opts ) {
35+ assert ( opts . languageCode , 'languageCode is required' )
3436 assert ( opts . relativePath , 'relativePath is required' )
3537 assert ( opts . basePath , 'basePath is required' )
3638
@@ -40,40 +42,29 @@ class Page {
4042 // Per https://nodejs.org/api/fs.html#fs_fs_exists_path_callback
4143 // its better to read and handle errors than to check access/stats first
4244 try {
43- const raw = fs . readFileSync ( fullPath , 'utf8' )
44- return { ...opts , relativePath, fullPath, raw }
45+ const { data, content, errors : frontmatterErrors } = await fmfromf ( fullPath , opts . languageCode )
46+
47+ return {
48+ ...opts ,
49+ relativePath,
50+ fullPath,
51+ ...data ,
52+ markdown : content ,
53+ frontmatterErrors
54+ }
4555 } catch ( err ) {
4656 if ( err . code === 'ENOENT' ) return false
4757 console . error ( err )
4858 }
4959 }
5060
5161 constructor ( opts ) {
52- assert ( opts . languageCode , 'languageCode is required' )
53-
5462 Object . assign ( this , { ...opts } )
5563
56- // TODO remove this when crowdin-support issue 66 has been resolved
57- if ( this . languageCode !== 'en' && this . raw . includes ( ': verdadero' ) ) {
58- this . raw = this . raw . replace ( ': verdadero' , ': true' )
59- }
60-
61- // parse frontmatter and save any errors for validation in the test suite
62- const { content, data, errors : frontmatterErrors } = frontmatter ( this . raw , { filepath : this . fullPath } )
63- this . frontmatterErrors = frontmatterErrors
64-
6564 if ( this . frontmatterErrors . length ) {
6665 throw new Error ( JSON . stringify ( this . frontmatterErrors , null , 2 ) )
6766 }
6867
69- // preserve the frontmatter-free markdown content,
70- this . markdown = content
71-
72- // prevent `[foo] (bar)` strings with a space between from being interpreted as markdown links
73- this . markdown = encodeBracketedParentheses ( this . markdown )
74-
75- Object . assign ( this , data )
76-
7768 // Store raw data so we can cache parsed versions
7869 this . rawIntro = this . intro
7970 this . rawTitle = this . title
@@ -94,8 +85,10 @@ class Page {
9485 // derive array of Permalink objects
9586 this . permalinks = Permalink . derive ( this . languageCode , this . relativePath , this . title , this . versions )
9687
97- // get an array of linked items in product and category TOCs
98- this . tocItems = getTocItems ( this )
88+ if ( this . relativePath . endsWith ( 'index.md' ) ) {
89+ // get an array of linked items in product and category TOCs
90+ this . tocItems = getTocItems ( this )
91+ }
9992
10093 // if this is an article and it doesn't have showMiniToc = false, set mini TOC to true
10194 if ( ! this . relativePath . endsWith ( 'index.md' ) && ! this . mapTopic ) {
@@ -146,7 +139,20 @@ class Page {
146139 : this . renderProp ( 'title' , context , opts )
147140 }
148141
142+ async getMarkdown ( ) {
143+ const raw = fs . readFileSync ( this . fullPath , 'utf8' )
144+ const { content } = frontmatter ( raw , { filepath : this . fullPath } )
145+ // prevent `[foo] (bar)` strings with a space between from being interpreted as markdown links
146+ const encodedMarkdown = encodeBracketedParentheses ( content )
147+ return encodedMarkdown
148+ }
149+
149150 async _render ( context ) {
151+ // Get the raw markdown if we need to
152+ if ( ! this . markdown ) {
153+ this . markdown = await this . getMarkdown ( )
154+ }
155+
150156 this . intro = await renderContent ( this . rawIntro , context )
151157
152158 // rewrite local links in the intro to include current language code and GHE version if needed
0 commit comments