Skip to content

Commit 15ec110

Browse files
foolipddbeck
andauthored
Convert descriptions to plaintext + HTML (#882)
Co-authored-by: Daniel D. Beck <[email protected]>
1 parent eb84289 commit 15ec110

File tree

3 files changed

+1311
-770
lines changed

3 files changed

+1311
-770
lines changed

index.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ import YAML from 'yaml';
66
import { FeatureData } from './types';
77
import { Temporal } from '@js-temporal/polyfill';
88

9+
import { toString as hastTreeToString } from 'hast-util-to-string';
10+
import rehypeStringify from 'rehype-stringify';
11+
import remarkParse from 'remark-parse';
12+
import remarkRehype from 'remark-rehype';
13+
import { unified } from 'unified';
14+
915
import { BASELINE_LOW_TO_HIGH_DURATION } from 'compute-baseline';
1016

1117
// The longest description allowed, to avoid them growing into documentation.
@@ -15,6 +21,7 @@ const descriptionMaxLength = 300;
1521
// They're not part of the public schema (yet).
1622
const omittables = [
1723
"description",
24+
"description_html",
1825
"snapshot",
1926
"group"
2027
]
@@ -87,8 +94,30 @@ function* identifiers(value) {
8794
}
8895
}
8996

97+
function convertMarkdown(markdown: string) {
98+
const mdTree = unified().use(remarkParse).parse(markdown);
99+
const htmlTree = unified().use(remarkRehype).runSync(mdTree);
100+
const text = hastTreeToString(htmlTree);
101+
102+
let html = unified().use(rehypeStringify).stringify(htmlTree);
103+
// Remove leading <p> and trailing </p> if there is only one of each in the
104+
// description. (If there are multiple paragraphs, let them be.)
105+
if (html.lastIndexOf('<p>') === 0 && html.indexOf('</p>') === html.length - 4) {
106+
html = html.substring(3, html.length - 4);
107+
}
108+
109+
return { text, html };
110+
}
111+
90112
const features: { [key: string]: FeatureData } = {};
91113
for (const [key, data] of yamlEntries('feature-group-definitions')) {
114+
// Convert markdown to text+HTML.
115+
if (data.description) {
116+
const { text, html } = convertMarkdown(data.description);
117+
data.description = text;
118+
data.description_html = html;
119+
}
120+
92121
// Compute Baseline high date from low date.
93122
const isDist = fs.existsSync(`feature-group-definitions/${key}.dist.yml`);
94123
if (!isDist && data.status?.baseline_high_date) {

0 commit comments

Comments
 (0)