Skip to content

Commit

Permalink
Allow use of Markdown in description and notes
Browse files Browse the repository at this point in the history
  • Loading branch information
queengooborg committed Dec 6, 2023
1 parent b2afc9d commit 91a53cc
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 120 deletions.
132 changes: 13 additions & 119 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"husky": "^8.0.1",
"json-schema-to-typescript": "~13.1.0",
"lint-staged": "^15.0.1",
"marked": "^11.0.0",
"mocha": "~10.2.0",
"open-cli": "~7.2.0",
"ora": "~7.0.1",
Expand Down
52 changes: 52 additions & 0 deletions scripts/release/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import fs from 'node:fs/promises';

import esMain from 'es-main';
import stringify from 'fast-json-stable-stringify';
import { marked } from 'marked';

import { InternalSupportStatement } from '../../types/index.js';
import { BrowserName, CompatData } from '../../types/types.js';
Expand Down Expand Up @@ -34,6 +35,18 @@ export const generateMeta = (): any => ({
timestamp: new Date(),
});

/**
* Converts Markdown to HTML and sanitizes output
* @param {string | string[]} markdown The Markdown to convert
* @returns {string | string[]} The HTML output
*/
const mdToHtml = (markdown: string): string => {
// "as string" cast because TS thinks response could be a promise
return (marked.parseInline(markdown) as string)
.replace(/'/g, "'")
.replace(/"/g, '"');
};

/**
* Apply mirroring to a feature
* @param {WalkOutput} feature The BCD to perform mirroring on
Expand All @@ -52,6 +65,44 @@ export const applyMirroring = (feature: WalkOutput): void => {
}
};

/**
* Convert descriptions and notes from Markdown to HTML
* @param {WalkOutput} feature The BCD to perform mirroring on
* @returns {void}
*/
export const transformMD = (feature: WalkOutput): void => {
if ('description' in feature.data.__compat) {
feature.data.__compat.description = mdToHtml(
feature.data.__compat.description,
);
}

for (const [browser, supportData] of Object.entries(
feature.compat.support as InternalSupportStatement,
)) {
if (!supportData) continue;

if (Array.isArray(supportData)) {
for (let i = 0; i < supportData.length; i++) {
if ('notes' in supportData[i]) {
(feature.data as any).__compat.support[browser][i].notes =
Array.isArray(supportData[i].notes)
? supportData[i].notes.map((md) => mdToHtml(md))
: mdToHtml(supportData[i].notes);
}
}
} else if (typeof supportData === 'object') {
if ('notes' in supportData) {
(feature.data as any).__compat.support[browser].notes = Array.isArray(
(supportData as any).notes,
)
? (supportData as any).notes.map((md) => mdToHtml(md))
: mdToHtml((supportData as any).notes);
}
}
}
};

/**
* Generate a BCD data bundle
* @returns {CompatData} An object containing the prepared BCD data
Expand All @@ -64,6 +115,7 @@ export const createDataBundle = async (): Promise<CompatData> => {

for (const feature of walker) {
applyMirroring(feature);
transformMD(feature);
}

return {
Expand Down
4 changes: 3 additions & 1 deletion test/linter/test-notes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import chalk from 'chalk-template';
import HTMLParser from '@desertnet/html-parser';
import { marked } from 'marked';

import { Linter, Logger, LinterData, VALID_ELEMENTS } from '../utils.js';
import {
Expand Down Expand Up @@ -74,7 +75,8 @@ const validateHTML = (
feature: string,
logger: Logger,
): void => {
const htmlErrors = HTMLParser.validate(string);
const html = marked.parseInline(string);
const htmlErrors = HTMLParser.validate(html);

if (htmlErrors.length === 0) {
// If HTML is valid, ensure we're only using valid elements
Expand Down

0 comments on commit 91a53cc

Please sign in to comment.