Notes/src/transform_api_docs.js

227 lines
7.6 KiB
JavaScript
Raw Normal View History

2023-01-05 16:52:39 +01:00
const sanitizeHtml = require('sanitize-html');
function transform(content) {
2023-01-05 23:38:41 +01:00
const result = sanitizeHtml(content, {
2023-01-05 16:52:39 +01:00
allowedTags: [
2023-01-05 23:38:41 +01:00
'h4', 'h5', 'h6', 'blockquote', 'p', 'a', 'ul', 'ol',
2023-01-05 16:52:39 +01:00
'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',
],
2023-01-05 23:38:41 +01:00
nonTextTags: [ 'style', 'script', 'textarea', 'option', 'h1', 'h2', 'h3', 'nav' ],
2023-01-05 16:52:39 +01:00
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'],
2023-01-05 23:38:41 +01:00
transformTags: {
// 'h5': sanitizeHtml.simpleTransform('strong', {}, false),
'table': sanitizeHtml.simpleTransform('table', {}, false)
},
2023-01-05 16:52:39 +01:00
});
2023-01-05 23:38:41 +01:00
return result.replace(/<table>/gi, '<figure class="table"><table>')
.replace(/<\/table>/gi, '</table></figure>')
.replace(/<div><\/div>/gi, '')
.replace(/<h5>/gi, '<p><strong>')
.replace(/<\/h5>/gi, '</strong></p>')
.replace(/<h4>/gi, '<h2>')
.replace(/<\/h4>/gi, '</h2>')
.replace(/<span class="signature-attributes">opt<\/span>/gi, '')
.replace(/<h2>.*new (BackendScriptApi|FrontendScriptApi).*<\/h2>/gi, '')
;
2023-01-05 16:52:39 +01:00
}
const fs = require("fs");
const path = require("path");
2023-01-05 23:38:41 +01:00
const html = require("html");
2023-01-05 16:52:39 +01:00
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);
2023-01-05 23:38:41 +01:00
} else if (file.endsWith('.html')) {
2023-01-05 16:52:39 +01:00
sourceFiles.push(absolute);
}
}
};
2023-01-10 22:48:56 +01:00
const TMP_API_DOCS = './tmp/api_docs';
const TMP_FE_DOCS = TMP_API_DOCS + '/frontend_api';
const TMP_BE_DOCS = TMP_API_DOCS + '/backend_api';
getFilesRecursively(TMP_API_DOCS);
2023-01-05 16:52:39 +01:00
for (const sourcePath of sourceFiles) {
2023-01-05 23:38:41 +01:00
const content = fs.readFileSync(sourcePath).toString();
2023-01-05 16:52:39 +01:00
const transformedContent = transform(content);
2023-01-05 23:38:41 +01:00
const prettifiedContent = html.prettyPrint(transformedContent, {indent_size: 2});
const filteredContent = prettifiedContent
.replace(/<br \/>Documentation generated by <a href="https:\/\/github.com\/jsdoc\/jsdoc">[^<]+<\/a>/gi, '')
.replace(/JSDoc: (Class|Module): [a-z]+/gi, '');
2023-01-05 16:52:39 +01:00
2023-01-05 23:38:41 +01:00
const destPath = sourcePath.replaceAll("tmp", "docs");
2023-01-05 16:52:39 +01:00
fs.mkdirSync(path.dirname(destPath), {recursive: true});
2023-01-05 23:38:41 +01:00
fs.writeFileSync(destPath, filteredContent.trim());
2023-01-05 16:52:39 +01:00
console.log(destPath);
2023-01-05 23:38:41 +01:00
}
2023-01-10 16:51:50 +01:00
const META_PATH = './docs/user_guide/!!!meta.json';
const meta = JSON.parse(fs.readFileSync(META_PATH).toString());
2023-01-10 22:48:56 +01:00
function findNoteMeta(noteMeta, name, notePath) {
if (noteMeta.title === name) {
return {
noteMeta,
filePath: '/' + noteMeta.dataFileName,
notePath
};
}
for (const childMeta of noteMeta.children || []) {
const ret = findNoteMeta(childMeta, name, [...notePath, childMeta.noteId]);
if (ret) {
return {
noteMeta: ret.noteMeta,
filePath: '/' + noteMeta.dirFileName + ret.path,
notePath: ret.notePath
};
}
}
return null;
}
const {noteMeta: scriptApiDocsRoot, filePath: scriptApiDocsRootFilePath, notePath: scriptApiDocsRootNotePath} =
findNoteMeta(meta.files[0], 'Script API', ['_scriptApi']);
const BE_FILES = ['AbstractBeccaEntity', 'BAttribute', 'BBranch', 'BEtapiToken', 'BNote', 'BNoteRevision', 'BOption', 'BRecentNote', 'module-sql'];
const FE_FILES = ['FNote', 'FAttribute', 'FBranch', 'FNoteComplement'];
scriptApiDocsRoot.children = getScriptApiMeta();
2023-01-10 16:51:50 +01:00
fs.writeFileSync(META_PATH, JSON.stringify(meta, null, 2));
2023-01-10 22:48:56 +01:00
const scriptApiDocsRootDir = './docs/user_guide' + scriptApiDocsRootFilePath.substr(0, scriptApiDocsRootFilePath.length - 5);
fs.mkdirSync(scriptApiDocsRootDir, {recursive: true});
fs.mkdirSync(scriptApiDocsRootDir + '/BackendScriptApi', {recursive: true});
fs.mkdirSync(scriptApiDocsRootDir + '/FrontendScriptApi', {recursive: true});
const BE_ROOT = scriptApiDocsRootDir + '/BackendScriptApi.html';
const FE_ROOT = scriptApiDocsRootDir + '/FrontendScriptApi.html';
fs.copyFileSync(TMP_BE_DOCS + '/BackendScriptApi.html', BE_ROOT);
fs.copyFileSync(TMP_FE_DOCS + '/FrontendScriptApi.html', FE_ROOT);
function rewriteLinks(rootFilePath, files, dir) {
let content = fs.readFileSync(rootFilePath).toString();
for (const file of files) {
content = content.replaceAll(`href="${file}.html"`, `href="${dir}/${file}.html"`);
}
fs.writeFileSync(rootFilePath, content);
}
for (const file of BE_FILES) {
fs.copyFileSync(TMP_BE_DOCS + '/' + file + '.html', scriptApiDocsRootDir + '/BackendScriptApi/' + file + '.html');
}
rewriteLinks(BE_ROOT, BE_FILES, 'BackendScriptApi');
for (const file of FE_FILES) {
fs.copyFileSync(TMP_FE_DOCS + '/' + file + '.html', scriptApiDocsRootDir + '/FrontendScriptApi/' + file + '.html');
}
rewriteLinks(FE_ROOT, FE_FILES, 'FrontendScriptApi');
function createChildren(files, notePath) {
let positionCounter = 0;
const camelCase = name => name.charAt(0).toLowerCase() + name.substr(1);
return files.map(file => {
positionCounter += 10;
return {
"isClone": false,
"noteId": "_file",
"notePath": [
...notePath,
'_' + camelCase(file)
],
"title": file,
"notePosition": positionCounter,
"prefix": null,
"isExpanded": false,
"type": "text",
"mime": "text/html",
"attributes": [],
"format": "html",
"dataFileName": file + ".html"
}
});
}
function getScriptApiMeta() {
return [
{
"isClone": false,
"noteId": "_frontendApi",
"notePath": [
...scriptApiDocsRootNotePath,
"_frontendApi"
],
"title": "API docs",
"notePosition": 10,
"prefix": null,
"isExpanded": false,
"type": "text",
"mime": "text/html",
"attributes": [],
"format": "html",
"dataFileName": "FrontendScriptApi.html",
"children": createChildren(FE_FILES, [
...scriptApiDocsRootNotePath,
"_frontendApi"
])
},
{
"isClone": false,
"noteId": "_backendApi",
"notePath": [
...scriptApiDocsRootNotePath,
"_backendApi"
],
"title": "API docs",
"notePosition": 20,
"prefix": null,
"isExpanded": false,
"type": "text",
"mime": "text/html",
"attributes": [],
"format": "html",
"dataFileName": "BackendScriptApi.html",
"children": createChildren(BE_FILES, [
...scriptApiDocsRootNotePath,
"_backendApi"
])
}
];
}