@@ -12,6 +12,7 @@ const upstream = require("./_upstream")
12
12
const version = require ( "../package.json" ) . version
13
13
14
14
const r = ( file ) => path . resolve ( __dirname , ".." , file )
15
+ const metaDescriptionRegExp = / < ! - - m e t a - d e s c r i p t i o n \n ( [ \s \S ] + ?) \n - - > / m
15
16
16
17
// Minify our docs.
17
18
const htmlMinifierConfig = {
@@ -96,6 +97,26 @@ async function archiveDocsSelect() {
96
97
. join ( "" )
97
98
return `<select id="archive-docs" onchange="location.href='archive/' + this.value + '/index.html'">${ options } </select>`
98
99
}
100
+
101
+ function encodeHTML ( str ) {
102
+ const charsToEncode = / [ & " ' < > ] / g
103
+ const encodeTo = {
104
+ "&" : "&" ,
105
+ "\"" : """ ,
106
+ "'" : "'" ,
107
+ "<" : "<" ,
108
+ ">" : ">" ,
109
+ }
110
+ return str . replace ( charsToEncode , function ( char ) { return encodeTo [ char ] } )
111
+ }
112
+
113
+ function extractMetaDescription ( markdown ) {
114
+ var match = markdown . match ( metaDescriptionRegExp )
115
+ if ( match ) {
116
+ return encodeHTML ( match [ 1 ] )
117
+ }
118
+ return "Mithril.js Documentation"
119
+ }
99
120
class Generator {
100
121
constructor ( opts ) {
101
122
this . _version = opts . version
@@ -110,7 +131,8 @@ class Generator {
110
131
`([ \t]*)(- )(\\[.+?\\]\\(${ escapeRegExp ( file ) } \\))`
111
132
)
112
133
const src = link . test ( this . _guides ) ? this . _guides : this . _methods
113
- let body = markdown
134
+ const metaDescription = extractMetaDescription ( markdown )
135
+ let body = markdown . replace ( metaDescriptionRegExp , "" )
114
136
115
137
// fix pipes in code tags
116
138
body = body . replace ( / ` ( (?: \S | - > | , ) + ) ( \| ) ( \S + ) ` / gim,
@@ -154,6 +176,9 @@ class Generator {
154
176
155
177
// insert parsed HTML
156
178
result = result . replace ( / \[ b o d y \] / , markedHtml )
179
+
180
+ // insert meta description
181
+ result = result . replace ( / \[ m e t a D e s c r i p t i o n \] / , metaDescription )
157
182
158
183
// fix anchors
159
184
const anchorIds = new Map ( )
0 commit comments