UNPKG

@tdb/web

Version:

Common condiguration for serving a web-site and testing web-based UI components.

143 lines 6.33 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var common_1 = require("../common"); var util_parse_1 = require("./util.parse"); var unified = require('unified'); var parse = require('remark-parse'); var html = require('remark-html'); var processor = unified() .use(parse, { commonmark: true }) .use(html); function toManifestMarkdown(path, body) { return tslib_1.__awaiter(this, void 0, void 0, function () { var res, _a, markdown, error, result; return tslib_1.__generator(this, function (_b) { switch (_b.label) { case 0: if (!body) return [3, 2]; return [4, parseMarkdown(body)]; case 1: _a = _b.sent(); return [3, 3]; case 2: _a = undefined; _b.label = 3; case 3: res = _a; markdown = res ? res.data : undefined; error = res ? res.error : undefined; result = { type: 'FILE/markdown', path: path, markdown: markdown, error: error, }; return [2, common_1.value.deleteUndefined(result)]; } }); }); } exports.toManifestMarkdown = toManifestMarkdown; function parseMarkdownFile(path) { return tslib_1.__awaiter(this, void 0, void 0, function () { var text, err_1, message; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); return [4, common_1.fs.readFile(path)]; case 1: text = (_a.sent()).toString(); return [2, parseMarkdown(text)]; case 2: err_1 = _a.sent(); message = "Failed to parse markdown while loading file."; message = message + " " + err_1.message; return [2, { data: undefined, error: { message: message } }]; case 3: return [2]; } }); }); } exports.parseMarkdownFile = parseMarkdownFile; function parseMarkdown(text) { return tslib_1.__awaiter(this, void 0, void 0, function () { var result, ast, parts, title_1, sections_1, addHtml_1, err_2, message; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: result = {}; _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); return [4, processor.parse(text)]; case 2: ast = (_a.sent()); parts = ast.children.map(function (child, index) { var type = child.type, lang = child.lang, value = child.value, depth = child.depth; var meta = child.meta ? child.meta : undefined; var html = processor.stringify(child); var data; var error; if (type === 'code' && lang === 'yaml' && value) { var res = util_parse_1.parseYaml(value); data = res.data; error = res.error; } return { type: type, depth: depth, html: html, data: data, meta: meta, error: error }; }); sections_1 = []; addHtml_1 = function (section, html) { return ((section.html || '') + "\n" + (html || '')).trim(); }; parts.forEach(function (part) { var isHeading = part.type === 'heading'; if (isHeading || sections_1.length === 0) { var depth = part.depth || 0; sections_1 = sections_1.concat([{ index: -1, depth: depth, data: [] }]); } var section = tslib_1.__assign({}, sections_1[sections_1.length - 1]); if (isHeading) { section.titleHtml = part.html; section.title = stripTags(part.html); } if (part.data) { var meta = part.meta, value = part.data; section.data = section.data.concat([{ meta: meta, value: value }]); } if (!part.data && !isHeading) { section.html = addHtml_1(section, part.html); } if (part.data && part.meta && part.meta.includes('RENDER')) { section.html = addHtml_1(section, part.html); } sections_1 = sections_1.slice(0, sections_1.length - 1).concat([ common_1.value.deleteEmpty(section), ]); if (!title_1 && isHeading && part.depth === 1) { title_1 = stripTags(part.html); } }); sections_1 = sections_1.map(function (s, index) { return (tslib_1.__assign({}, s, { index: index })); }); result.data = { title: title_1, sections: sections_1 }; return [3, 4]; case 3: err_2 = _a.sent(); message = "Failed to parse markdown."; message = message + " " + err_2.message; result.error = { message: message }; return [3, 4]; case 4: return [2, common_1.value.deleteUndefined(result)]; } }); }); } exports.parseMarkdown = parseMarkdown; function stripTags(text) { return text .trim() .replace(/^\<[a-z1-9]+\>/, '') .replace(/\<\/[a-z1-9]+\>$/, ''); } //# sourceMappingURL=util.markdown.js.map