Skip to content

Commit

Permalink
fix(markdown-table): Fixed code block in markdown api table
Browse files Browse the repository at this point in the history
  • Loading branch information
rajkeshwar committed Aug 22, 2024
1 parent 95b7460 commit 8be0776
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion scripts/create-api-tables.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,31 @@ function clearAndUpper(text) {
return text.replace(/-/, ' ').toUpperCase();
}

/**
* Function to convert code into HTML markup
* @param {string} code
*/
function codeToHtml(code) {
return code
// Escape HTML characters
.replace(/&/g, '&')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&#039;')
// Preserve line breaks and spaces (for indentation)
.replace(/\n/g, '<br>')
.replace(/ {2}/g, '&nbsp;');
}

/**
* @param {string} input
*/
function stripBackticks(input) {
// This regex matches a backtick followed by <pre> or </pre> followed by a backtick
return input.replace(/(`<pre>|<\/pre>`)/gm, (_, t) => t.replace(/`/, ''));
}

const systemsTable = ['core', 'form-core', 'localize', 'overlays'];
const mappingTable = [{ componentDir: 'validate-messages', portalDir: 'form' }];

Expand Down Expand Up @@ -54,6 +79,14 @@ for (let dir of componentDirs) {
for (const mod of customElementsJson.modules) {
for (const declaration of mod.declarations) {
if (declaration.name === c && declaration.kind === 'class') {

declaration.members = declaration.members.map(member => {
if (typeof member.default === 'string' && /\r?\n/.test(member.default)) {
member.default = `<pre><code>${codeToHtml(member.default)}</code></pre>`;
}
return member;
})

const md = customElementsManifestToMarkdown(
{
modules: [
Expand All @@ -74,7 +107,7 @@ for (let dir of componentDirs) {
],
},
);
dirApiTableMd = `${dirApiTableMd} \n\n${md}`;
dirApiTableMd = `${dirApiTableMd} \n\n${stripBackticks(md)}`;
}
}
}
Expand Down

0 comments on commit 8be0776

Please sign in to comment.