Skip to content

Commit 1f5e477

Browse files
committed
Add meta description to docs
1 parent da9bfc7 commit 1f5e477

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

generate-docs.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const upstream = require("./_upstream")
1212
const version = require("../package.json").version
1313

1414
const r = (file) => path.resolve(__dirname, "..", file)
15+
const metaDescriptionRegExp = /<!--meta-description\n([\s\S]+?)\n-->/m
1516

1617
// Minify our docs.
1718
const htmlMinifierConfig = {
@@ -96,6 +97,26 @@ async function archiveDocsSelect() {
9697
.join("")
9798
return `<select id="archive-docs" onchange="location.href='archive/' + this.value + '/index.html'">${options}</select>`
9899
}
100+
101+
function encodeHTML (str) {
102+
const charsToEncode = /[&"'<>]/g
103+
const encodeTo = {
104+
"&": "&amp;",
105+
"\"": "&quot;",
106+
"'": "&#39;",
107+
"<": "&lt;",
108+
">": "&gt;",
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+
}
99120
class Generator {
100121
constructor(opts) {
101122
this._version = opts.version
@@ -110,7 +131,8 @@ class Generator {
110131
`([ \t]*)(- )(\\[.+?\\]\\(${escapeRegExp(file)}\\))`
111132
)
112133
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, "")
114136

115137
// fix pipes in code tags
116138
body = body.replace(/`((?:\S| -> |, )+)(\|)(\S+)`/gim,
@@ -154,6 +176,9 @@ class Generator {
154176

155177
// insert parsed HTML
156178
result = result.replace(/\[body\]/, markedHtml)
179+
180+
// insert meta description
181+
result = result.replace(/\[metaDescription\]/, metaDescription)
157182

158183
// fix anchors
159184
const anchorIds = new Map()

0 commit comments

Comments
 (0)