Skip to content

Commit 793a593

Browse files
committed
Extract title from the RFCs and return with the frontmatter
1 parent bbe49bf commit 793a593

7 files changed

+737
-2
lines changed

lib/frontmatter.mjs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import githubFrontmatter from '@github-docs/frontmatter';
2+
import { title } from './title.mjs';
23

34
export function frontmatter(markdown) {
45
let { content, data, errors } = githubFrontmatter(markdown);
5-
return { content, data, errors };
6+
return { content, data: { title: title(markdown), ...data }, errors };
67
}

lib/title.mjs

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { unified } from 'unified';
2+
import remarkParse from 'remark-parse';
3+
import remarkFrontmatter from 'remark-frontmatter';
4+
import remarkGfm from 'remark-gfm';
5+
import { visit } from 'unist-util-visit';
6+
import { toString } from 'mdast-util-to-string';
7+
8+
function topLevelHeaderText(tree) {
9+
let heading;
10+
11+
visit(tree, 'heading', node => {
12+
if (node.depth !== 1) {
13+
return;
14+
}
15+
heading = node;
16+
});
17+
return heading;
18+
}
19+
20+
export function title(markdown) {
21+
const tree = unified()
22+
.use(remarkParse)
23+
.use(remarkGfm)
24+
.use(remarkFrontmatter)
25+
.parse(markdown)
26+
const headingTree = topLevelHeaderText(tree);
27+
return toString(headingTree);
28+
}

package.json

+6
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,14 @@
2424
"@github-docs/frontmatter": "^1.3.1",
2525
"chalk": "^5.2.0",
2626
"deepmerge": "^4.3.0",
27+
"remark-frontmatter": "^4.0.1",
28+
"remark-gfm": "^3.0.1",
29+
"remark-parse": "^10.0.1",
30+
"remark-stringify": "^10.0.2",
2731
"semver": "^7.3.8",
2832
"simple-git": "^3.16.0",
33+
"unified": "^10.1.2",
34+
"unist-util-visit": "^4.1.2",
2935
"yargs": "^17.6.2"
3036
},
3137
"engines": {

test/lib/title-test.mjs

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import chai from 'chai';
2+
import { title } from '../../lib/title.mjs';
3+
4+
const expect = chai.expect;
5+
6+
const RFC = `---
7+
stage: accepted
8+
start-date: 2022-08-28
9+
release-date: Unreleased
10+
release-versions:
11+
ember-source: vX.Y.Z
12+
ember-data: vX.Y.Z
13+
teams:
14+
- framework
15+
prs:
16+
accepted:
17+
---
18+
19+
# My New RFC
20+
21+
My grand plans...
22+
23+
## Another heading
24+
25+
some stuff we will do
26+
`
27+
28+
const RfcWithBlocksInHeader = `---
29+
stage: accepted
30+
start-date: 2022-08-28
31+
release-date: Unreleased
32+
release-versions:
33+
ember-source: vX.Y.Z
34+
ember-data: vX.Y.Z
35+
teams:
36+
- framework
37+
prs:
38+
accepted:
39+
---
40+
41+
# Deprecate \`foo\` and \`bar\`; immediately
42+
## Another heading
43+
`
44+
45+
46+
describe('title', function() {
47+
it('returns the contents of the first top-level header in the doc', async function() {
48+
let header = title(RFC);
49+
expect(header).to.equal('My New RFC');
50+
});
51+
52+
it('returns the text contents of the first top-level header in the doc when there are blocks', async function() {
53+
let header = title(RfcWithBlocksInHeader);
54+
expect(header).to.equal('Deprecate foo and bar; immediately');
55+
});
56+
});

test/scripts/list-frontmatter-test.mjs

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ describe('list-frontmatter', function () {
2121
prs: {
2222
accepted: null,
2323
},
24+
title: "My New RFC"
2425
},
2526
{
2627
name: 'test/fixtures/0923-already-recommended.md',
@@ -34,6 +35,7 @@ describe('list-frontmatter', function () {
3435
prs: {
3536
accepted: null,
3637
},
38+
title: "My Completely finished RFC"
3739
},
3840
]);
3941
});

test/scripts/rfc-frontmatter-test.mjs

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ describe('rfc-frontmatter', function () {
1616
prs: {
1717
accepted: null,
1818
},
19+
title: "My New RFC"
1920
});
2021
});
2122

0 commit comments

Comments
 (0)