Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow use of Markdown in description and notes #21495

Merged
merged 30 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
91a53cc
Allow use of Markdown in description and notes
queengooborg Dec 6, 2023
e7b428a
Update description linter
queengooborg Dec 6, 2023
c07b311
Fix note test
queengooborg Dec 6, 2023
f3aeb6f
Merge branch 'main' into markdown-notes
queengooborg Dec 10, 2023
a76ad86
Merge branch 'main' into markdown-notes
queengooborg Dec 11, 2023
d7f9ad2
Merge branch 'main' into markdown-notes
queengooborg Dec 14, 2023
c57648c
Merge branch 'main' into markdown-notes
queengooborg Dec 15, 2023
fb8dc3d
Merge branch 'main' into markdown-notes
queengooborg Dec 18, 2023
a09809d
Merge branch 'main' into markdown-notes
queengooborg Dec 19, 2023
f8fa99a
Merge branch 'main' into markdown-notes
queengooborg Mar 28, 2024
7360b37
Merge branch 'main' into markdown-notes
queengooborg Jun 25, 2024
9727e58
Merge branch 'main' into markdown-notes
queengooborg Jul 8, 2024
5bbaaa1
Revert changes to description linter for now
queengooborg Jul 16, 2024
2a944ee
Merge branch 'main' into markdown-notes
queengooborg Jul 22, 2024
f535e46
Merge branch 'main' into markdown-notes
queengooborg Aug 27, 2024
f3bb756
Fix package-lock.json
queengooborg Aug 27, 2024
426dc57
Merge branch 'main' into markdown-notes
queengooborg Aug 28, 2024
b043b9a
Merge branch 'main' into markdown-notes
caugner Oct 8, 2024
e06bfaf
chore(deps-dev): bump marked from 10.0.0 to 10.2.0
caugner Oct 8, 2024
01ea2cc
Merge branch 'main' into markdown-notes
caugner Oct 8, 2024
3121d79
Fix description
queengooborg Oct 12, 2024
39825a3
Merge branch 'main' into markdown-notes
queengooborg Nov 2, 2024
3307fd9
Ensure <, >, etc. is preserved
queengooborg Nov 2, 2024
452e84d
Convert `<code>` tags to backticks before performing Markdown parsing
queengooborg Nov 2, 2024
58de17a
Fix problematic notes
queengooborg Nov 2, 2024
17ac0c9
Add missing code tags
queengooborg Nov 2, 2024
cc1187e
Fix description for RegExp.leftContext
queengooborg Nov 2, 2024
e8a393c
Merge branch 'main' into markdown-notes
queengooborg Nov 15, 2024
cfe1b04
Fix Chromium bug numbers
queengooborg Nov 15, 2024
8c01cfd
fix(markdown): use double backtick to use backtick in code
caugner Nov 15, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lint/linter/test-descriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,23 +66,23 @@ const processApiData = (
'constructor',
path,
data,
`<code>${apiName}()</code> constructor`,
`\`${apiName}()\` constructor`,
errors,
);
} else if (featureName.endsWith('_event')) {
checkDescription(
'event',
path,
data,
`<code>${featureName.replace('_event', '')}</code> event`,
`\`${featureName.replace('_event', '')}\` event`,
errors,
);
} else if (featureName.endsWith('_permission')) {
checkDescription(
'permission',
path,
data,
`<code>${featureName.replace('_permission', '')}</code> permission`,
`\`${featureName.replace('_permission', '')}\` permission`,
errors,
);
} else if (featureName == 'secure_context_required') {
Expand Down Expand Up @@ -150,7 +150,7 @@ export default {

for (const error of errors) {
if (typeof error === 'string') {
logger.error(chalk`Description → ${error}`);
logger.error(chalk`{red ${error}}`);
} else {
logger.error(
chalk`{red Incorrect ${error.ruleName} description for {bold ${error.path}}
Expand Down
6 changes: 4 additions & 2 deletions lint/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 @@ -68,11 +69,12 @@ const testNode = (node): string[] => {
*/
export const validateHTML = (string: string): string[] => {
const errors: string[] = [];
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
errors.push(...testNode(parser.parse(string)));
errors.push(...testNode(parser.parse(html)));
} else {
errors.push(
chalk`Invalid HTML: ${htmlErrors.map((x) => x._message).join(', ')}`,
Expand Down
13 changes: 13 additions & 0 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 @@ -82,6 +82,7 @@
"husky": "^9.0.2",
"json-schema-to-typescript": "~14.1.0",
"lint-staged": "^15.0.1",
"marked": "^11.0.0",
"mocha": "~10.6.0",
"open-cli": "~8.0.0",
"ora": "~8.0.1",
Expand Down
52 changes: 52 additions & 0 deletions scripts/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import fs from 'node:fs/promises';
import esMain from 'es-main';
import stringify from 'fast-json-stable-stringify';
import { compareVersions } from 'compare-versions';
import { marked } from 'marked';
queengooborg marked this conversation as resolved.
Show resolved Hide resolved

import { InternalSupportStatement } from '../../types/index.js';
import { BrowserName, CompatData, VersionValue } from '../../types/types.js';
Expand Down Expand Up @@ -36,6 +37,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(/&#39;/g, "'")
.replace(/&quot;/g, '"');
};

/**
* Apply mirroring to a feature
* @param feature The BCD to perform mirroring on
Expand Down Expand Up @@ -109,6 +122,44 @@ export const addVersionLast = (feature: WalkOutput): void => {
}
};

/**
* Convert descriptions and notes from Markdown to HTML
* @param {WalkOutput} feature The BCD to perform mirroring on
queengooborg marked this conversation as resolved.
Show resolved Hide resolved
* @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);
}
}
}
};

/**
* Applies transforms to the given data.
* @param data - The data to apply transforms to.
Expand All @@ -119,6 +170,7 @@ export const applyTransforms = (data): void => {
for (const feature of walker) {
applyMirroring(feature);
addVersionLast(feature);
transformMD(feature);
}
};

Expand Down
Loading