@awesome-fe/translate
Version:
Translation utils
50 lines • 2.14 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AngularJsonExporter = void 0;
const exporter_1 = require("./exporter");
const html_exporter_1 = require("./html-exporter");
const dom_models_1 = require("../dom/parse5/dom-models");
const markdown_1 = require("../dom/unified/markdown");
function purgeHtml(result) {
return result
.replace(/<div class="github-links">.*?<\/div>/gs, '')
.replace(/<div class="breadcrumb-container">.*?code<\/i><\/a>\s+<\/div>\s+<\/div>/gs, '')
.replace(/<!--.*?-->/gs, '')
.replace(/<(div|header)[^>]*>/gs, '')
.replace(/<\/\s*(div|header)>/gs, '')
.replace(/<a title=.*?><i class="material-icons">link<\/i><\/a>/g, '')
.trim();
}
class AngularJsonExporter extends exporter_1.Exporter {
htmlExporter;
constructor(selectors = dom_models_1.defaultSelectors) {
super();
this.htmlExporter = new html_exporter_1.HtmlExporter(selectors);
}
exportContent(content, options) {
const json = JSON.parse(content);
if (!json.contents) {
return;
}
const result = this.htmlExporter.exportContent(this.preprocessAngularJson(json.contents), { ...options, format: 'html' });
switch (options.format) {
case 'auto':
json.contents = result;
return JSON.stringify(json);
case 'html':
return purgeHtml(result);
case 'markdown':
return markdown_1.markdown.mdFromHtml(purgeHtml(result)).replace(/\n\n+/g, '\n\n');
}
}
preprocessAngularJson(contents) {
// 对 cheatsheet页做特殊处理
if (contents.includes('<h1 class="no-toc">Cheat Sheet</h1>')) {
return contents.replace(/<(td|th)>\s*<p>([\s\S]*?)<\/p>\s*<\/\1>/g, '<$1>$2</$1>');
}
// 为其它页面中的 section、header 添加翻译标记
return contents.replace(/<(section|header)\b/g, '<$1 ng-should-translate');
}
}
exports.AngularJsonExporter = AngularJsonExporter;
//# sourceMappingURL=angular-json-exporter.js.map