const sanitizeHtml = require('sanitize-html');
function transform(content) {
const result = sanitizeHtml(content, {
allowedTags: [
'h4', 'h5', 'h6', 'blockquote', 'p', 'a', 'ul', 'ol',
'li', 'b', 'i', 'strong', 'em', 'strike', 's', 'del', 'abbr', 'code', 'hr', 'br', 'div',
'table', 'thead', 'caption', 'tbody', 'tr', 'th', 'td', 'pre', 'section', 'img',
'figure', 'figcaption', 'span', 'label', 'input',
],
nonTextTags: [ 'style', 'script', 'textarea', 'option', 'h1', 'h2', 'h3', 'nav' ],
allowedAttributes: {
'a': [ 'href', 'class', 'data-note-path' ],
'img': [ 'src' ],
'section': [ 'class', 'data-note-id' ],
'figure': [ 'class' ],
'span': [ 'class', 'style' ],
'label': [ 'class' ],
'input': [ 'class', 'type', 'disabled' ],
'code': [ 'class' ],
'ul': [ 'class' ],
'table': [ 'class' ],
'en-media': [ 'hash' ]
},
allowedSchemes: ['http', 'https', 'ftp', 'mailto', 'data', 'evernote'],
transformTags: {
// 'h5': sanitizeHtml.simpleTransform('strong', {}, false),
'table': sanitizeHtml.simpleTransform('table', {}, false)
},
});
return result.replace(/
/gi, '')
.replace(/<\/table>/gi, '
')
.replace(/<\/div>/gi, '')
.replace(/
/gi, '
')
.replace(/<\/h5>/gi, '
')
.replace(/
/gi, '')
.replace(/<\/h4>/gi, '
')
.replace(/opt<\/span>/gi, '')
.replace(/.*new (BackendScriptApi|FrontendScriptApi).*<\/h2>/gi, '')
;
}
const fs = require("fs");
const path = require("path");
const html = require("html");
let sourceFiles = [];
const getFilesRecursively = (directory) => {
const filesInDirectory = fs.readdirSync(directory);
for (const file of filesInDirectory) {
const absolute = path.join(directory, file);
if (fs.statSync(absolute).isDirectory()) {
getFilesRecursively(absolute);
} else if (file.endsWith('.html')) {
sourceFiles.push(absolute);
}
}
};
getFilesRecursively('./tmp/api_docs');
for (const sourcePath of sourceFiles) {
const content = fs.readFileSync(sourcePath).toString();
const transformedContent = transform(content);
const prettifiedContent = html.prettyPrint(transformedContent, {indent_size: 2});
const filteredContent = prettifiedContent
.replace(/
Documentation generated by [^<]+<\/a>/gi, '')
.replace(/JSDoc: (Class|Module): [a-z]+/gi, '');
const destPath = sourcePath.replaceAll("tmp", "docs");
fs.mkdirSync(path.dirname(destPath), {recursive: true});
fs.writeFileSync(destPath, filteredContent.trim());
console.log(destPath);
}