Skip to content

Commit 1e96c03

Browse files
authored
Absorb @github-docs/frontmatter (#17783)
1 parent b177927 commit 1e96c03

14 files changed

Lines changed: 314 additions & 27 deletions

lib/all-products.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const assert = require('assert')
55
const { difference } = require('lodash')
66
const yaml = require('js-yaml')
77
const contentDir = path.join(process.cwd(), 'content')
8-
const frontmatter = require('@github-docs/frontmatter')
8+
const frontmatter = require('./read-frontmatter')
99
const getApplicableVersions = require('./get-applicable-versions')
1010
const removeFPTFromPath = require('./remove-fpt-from-path')
1111

lib/frontmatter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const parse = require('@github-docs/frontmatter')
1+
const parse = require('./read-frontmatter')
22
const layoutNames = Object.keys(require('./layouts')).concat([false])
33
const semverRange = {
44
type: 'string',

lib/read-frontmatter.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
const matter = require('gray-matter')
2+
const revalidator = require('revalidator')
3+
const { difference, intersection } = require('lodash')
4+
5+
function readFrontmatter (markdown, opts = { validateKeyNames: false, validateKeyOrder: false }) {
6+
const schema = opts.schema || { properties: {} }
7+
const filepath = opts.filepath || null
8+
9+
let content, data
10+
let errors = []
11+
12+
try {
13+
({ content, data } = matter(markdown))
14+
} catch (e) {
15+
const defaultReason = 'invalid frontmatter entry'
16+
17+
const reason = e.reason
18+
// make this common error message a little easier to understand
19+
? e.reason.startsWith('can not read a block mapping entry;') ? defaultReason : e.reason
20+
: defaultReason
21+
22+
const error = {
23+
reason,
24+
message: 'YML parsing error!'
25+
}
26+
27+
if (filepath) error.filepath = filepath
28+
errors.push(error)
29+
return { errors }
30+
}
31+
32+
const allowedKeys = Object.keys(schema.properties)
33+
const existingKeys = Object.keys(data)
34+
const expectedKeys = intersection(allowedKeys, existingKeys)
35+
36+
;({ errors } = revalidator.validate(data, schema))
37+
38+
// add filepath property to each error object
39+
if (errors.length && filepath) {
40+
errors = errors.map(error => Object.assign(error, { filepath }))
41+
}
42+
43+
// validate key names
44+
if (opts.validateKeyNames) {
45+
const invalidKeys = difference(existingKeys, allowedKeys)
46+
invalidKeys.forEach(key => {
47+
const error = {
48+
property: key,
49+
message: `not allowed. Allowed properties are: ${allowedKeys.join(', ')}`
50+
}
51+
if (filepath) error.filepath = filepath
52+
errors.push(error)
53+
})
54+
}
55+
56+
// validate key order
57+
if (opts.validateKeyOrder && existingKeys.join('') !== expectedKeys.join('')) {
58+
const error = {
59+
property: 'keys',
60+
message: `keys must be in order. Current: ${existingKeys.join(',')}; Expected: ${expectedKeys.join(',')}`
61+
}
62+
if (filepath) error.filepath = filepath
63+
errors.push(error)
64+
}
65+
66+
return { content, data, errors }
67+
}
68+
69+
// Expose gray-matter's underlying stringify method for joining a parsed
70+
// frontmatter object and a markdown string back into a unified string
71+
//
72+
// stringify('some string', {some: 'frontmatter'})
73+
readFrontmatter.stringify = matter.stringify
74+
75+
module.exports = readFrontmatter

package-lock.json

Lines changed: 5 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
"@babel/preset-env": "^7.12.7",
2020
"@babel/preset-react": "^7.12.7",
2121
"@babel/runtime": "^7.11.2",
22-
"@github-docs/frontmatter": "^1.3.1",
2322
"@graphql-inspector/core": "^2.3.0",
2423
"@graphql-tools/load": "^6.2.5",
2524
"@octokit/rest": "^16.38.1",
@@ -47,7 +46,7 @@
4746
"flat": "^5.0.0",
4847
"github-slugger": "^1.2.1",
4948
"got": "^9.6.0",
50-
"gray-matter": "^4.0.1",
49+
"gray-matter": "^4.0.2",
5150
"hast-util-from-parse5": "^6.0.1",
5251
"hast-util-parse-selector": "^2.2.5",
5352
"hast-util-select": "^4.0.2",
@@ -65,7 +64,7 @@
6564
"js-yaml": "^3.14.0",
6665
"linkinator": "^2.13.1",
6766
"liquidjs": "^9.22.1",
68-
"lodash": "^4.17.19",
67+
"lodash": "^4.17.20",
6968
"lunr": "^2.3.9",
7069
"lunr-languages": "^1.4.0",
7170
"mdast-util-from-markdown": "^0.8.4",

script/content-migrations/update-developer-site-links.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const fs = require('fs')
44
const path = require('path')
55
const walk = require('walk-sync')
6-
const frontmatter = require('@github-docs/frontmatter')
6+
const frontmatter = require('../../lib/read-frontmatter')
77
const { loadPages, loadPageMap } = require('../../lib/pages')
88
const patterns = require('../../lib/patterns')
99
const loadRedirects = require('../../lib/redirects/precompile')

script/enterprise-server-releases/ghes-to-ghae-versioning.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const fs = require('fs')
44
const path = require('path')
55
const walk = require('walk-sync')
66
const program = require('commander')
7-
const frontmatter = require('@github-docs/frontmatter')
7+
const frontmatter = require('../../lib/read-frontmatter')
88
const contentPath = path.join(process.cwd(), 'content')
99
const dataPath = path.join(process.cwd(), 'data')
1010
const translationsPath = path.join(process.cwd(), 'translations')

script/reconcile-category-dirs-with-ids.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const fs = require('fs')
44
const path = require('path')
5-
const frontmatter = require('@github-docs/frontmatter')
5+
const frontmatter = require('../lib/read-frontmatter')
66
const walk = require('walk-sync')
77
const slash = require('slash')
88
const GithubSlugger = require('github-slugger')

script/reconcile-filenames-with-ids.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const path = require('path')
55
const walk = require('walk-sync')
66
const slugger = new (require('github-slugger'))()
77
const entities = new (require('html-entities').XmlEntities)()
8-
const frontmatter = require('@github-docs/frontmatter')
8+
const frontmatter = require('../lib/read-frontmatter')
99
const { execSync } = require('child_process')
1010
const addRedirectToFrontmatter = require('../lib/redirects/add-redirect-to-frontmatter')
1111

script/update-versioning-in-files.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const fs = require('fs')
44
const path = require('path')
55
const walk = require('walk-sync')
6-
const frontmatter = require('@github-docs/frontmatter')
6+
const frontmatter = require('../lib/read-frontmatter')
77
const contentPath = path.join(process.cwd(), 'content')
88
const dataPath = path.join(process.cwd(), 'data')
99

0 commit comments

Comments
 (0)