From 8be07760a8982bbb1f5a1f21dd81b0ffc3403f21 Mon Sep 17 00:00:00 2001 From: Rajkeshwar Prasad Date: Thu, 22 Aug 2024 22:31:57 +0200 Subject: [PATCH] fix(markdown-table): Fixed code block in markdown api table --- scripts/create-api-tables.mjs | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/scripts/create-api-tables.mjs b/scripts/create-api-tables.mjs index 7d7576ae2..983f6e4af 100644 --- a/scripts/create-api-tables.mjs +++ b/scripts/create-api-tables.mjs @@ -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, '>') + .replace(/"/g, '"') + .replace(/'/g, ''') + // Preserve line breaks and spaces (for indentation) + .replace(/\n/g, '
') + .replace(/ {2}/g, ' '); +} + +/** + * @param {string} input + */ +function stripBackticks(input) { + // This regex matches a backtick followed by
 or 
followed by a backtick + return input.replace(/(`
|<\/pre>`)/gm, (_, t) => t.replace(/`/, ''));
+}
+
 const systemsTable = ['core', 'form-core', 'localize', 'overlays'];
 const mappingTable = [{ componentDir: 'validate-messages', portalDir: 'form' }];
 
@@ -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 = `
${codeToHtml(member.default)}
`; + } + return member; + }) + const md = customElementsManifestToMarkdown( { modules: [ @@ -74,7 +107,7 @@ for (let dir of componentDirs) { ], }, ); - dirApiTableMd = `${dirApiTableMd} \n\n${md}`; + dirApiTableMd = `${dirApiTableMd} \n\n${stripBackticks(md)}`; } } }