Skip to content

Commit e68e6a5

Browse files
authored
Revert "Revert "Absorb @github-docs/frontmatter (github#17783)" (github#17793)" (github#17799)
* Revert "Revert "Absorb @github-docs/frontmatter (github#17783)" (github#17793)" This reverts commit 11baf43. * Move revalidator to depedencies
1 parent cfbcfad commit e68e6a5

14 files changed

+313
-27
lines changed

lib/all-products.js

+1-1
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

+1-1
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

+75
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

+3-13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-4
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",
@@ -89,6 +88,7 @@
8988
"remark-parse": "^7.0.2",
9089
"remark-rehype": "^5.0.0",
9190
"resolve-url-loader": "^3.1.2",
91+
"revalidator": "^0.3.1",
9292
"rimraf": "^3.0.0",
9393
"sass": "^1.26.3",
9494
"sass-loader": "^9.0.2",
@@ -153,7 +153,6 @@
153153
"prettier": "^2.1.2",
154154
"puppeteer": "^5.5.0",
155155
"replace": "^1.2.0",
156-
"revalidator": "^0.3.1",
157156
"robots-parser": "^2.1.1",
158157
"start-server-and-test": "^1.12.0",
159158
"supertest": "^4.0.2",

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

+1-1
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

+1-1
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

+1-1
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

+1-1
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

+1-1
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

tests/content/category-pages.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const path = require('path')
22
const fs = require('fs')
33
const walk = require('walk-sync')
4-
const matter = require('@github-docs/frontmatter')
4+
const matter = require('../../lib/read-frontmatter')
55
const { zip, difference } = require('lodash')
66
const GithubSlugger = require('github-slugger')
77
const { XmlEntities } = require('html-entities')

tests/content/liquid-line-breaks.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const path = require('path')
22
const fs = require('fs')
33
const walk = require('walk-sync')
4-
const matter = require('@github-docs/frontmatter')
4+
const matter = require('../../lib/read-frontmatter')
55
const { zip } = require('lodash')
66
const yaml = require('js-yaml')
77

tests/content/site-data-references.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const { isEqual, get, uniqWith } = require('lodash')
22
const loadSiteData = require('../../lib/site-data')
33
const { loadPages } = require('../../lib/pages')
44
const getDataReferences = require('../../lib/get-liquid-data-references')
5-
const frontmatter = require('@github-docs/frontmatter')
5+
const frontmatter = require('../../lib/read-frontmatter')
66
const fs = require('fs').promises
77
const path = require('path')
88

0 commit comments

Comments
 (0)