@awesome-fe/translate
Version:
Translation utils
71 lines • 2.68 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.htmlToMd = exports.mdToHtml = exports.markdownFromHtml = exports.markdownToHtml = exports.markdownStringify = exports.markdownParse = void 0;
var remarkParse = require("remark-parse");
var remarkStringify = require("remark-stringify");
var rehypeParse = require("rehype-parse");
var remarkHtml = require("remark-html");
var rehypeRemark = require("rehype-remark");
var frontmatter = require("remark-frontmatter");
var unified = require("unified");
var stringWidth = require("string-width");
var custom_parser_plugin_1 = require("./remark-plugins/custom-parser-plugin");
var custom_compiler_plugin_1 = require("./remark-plugins/custom-compiler-plugin");
var mast_to_hast_handlers_1 = require("./remark-plugins/mast-to-hast-handlers");
var hast_to_mast_handlers_1 = require("./remark-plugins/hast-to-mast-handlers");
var stringifyOptions = {
emphasis: '*',
listItemIndent: 1,
incrementListMarker: false,
stringLength: stringWidth,
paddedTable: false,
fences: true,
entities: false,
};
function markdownParse(markdown) {
return unified().use(remarkParse)
.use(frontmatter)
.use(custom_parser_plugin_1.customParser)
.parse(markdown);
}
exports.markdownParse = markdownParse;
function markdownStringify(tree) {
return unified().use(remarkStringify, stringifyOptions)
.use(frontmatter)
.use(custom_compiler_plugin_1.customCompiler)
.stringify(tree);
}
exports.markdownStringify = markdownStringify;
function markdownToHtml(ast) {
return unified().use(remarkParse)
.use(frontmatter)
.use(custom_parser_plugin_1.customParser)
.use(remarkHtml, { handlers: mast_to_hast_handlers_1.mastToHastHandlers })
.processSync(markdownStringify(ast)).contents.toString();
}
exports.markdownToHtml = markdownToHtml;
function markdownFromHtml(html) {
var markdown = unified().use(rehypeParse)
.use(rehypeRemark, { handlers: hast_to_mast_handlers_1.hastToMastHandlers })
.use(remarkStringify, stringifyOptions)
.use(custom_compiler_plugin_1.customCompiler)
.processSync(html);
return markdownParse(markdown);
}
exports.markdownFromHtml = markdownFromHtml;
function mdToHtml(md) {
if (!md) {
return md;
}
var html = markdownToHtml(markdownParse(md));
return html.trim().replace(/^<p>([\s\S]*?)<\/p>$/gi, '$1');
}
exports.mdToHtml = mdToHtml;
function htmlToMd(html) {
if (!html) {
return html;
}
return markdownStringify(markdownFromHtml(html));
}
exports.htmlToMd = htmlToMd;
//# sourceMappingURL=markdown.js.map