UNPKG

@patternplate/client

Version:

Universal javascript client application for patternplate

119 lines (97 loc) 3.19 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.flatten = flatten; exports.sanitize = sanitize; exports.enrich = enrich; var _path = _interopRequireDefault(require("path")); var _querystring = _interopRequireDefault(require("querystring")); var _url = _interopRequireDefault(require("url")); var _lodash = require("lodash"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } const WEIGHTS = { folder: 0, doc: 1, pattern: 2 }; function flatten(tree) { return tree.children; } function sanitize(tree, context) { const id = context.id, _context$config = context.config, config = _context$config === void 0 ? {} : _context$config, prefix = context.prefix, base = context.base, location = context.location, search = context.search; tree.children = tree.children.map(child => { const enriched = enrich(child, { base, location, id, config, prefix, search }); return enriched.children ? sanitize(enriched, context) : enriched; }).sort((a, b) => { const order = (((a.manifest || {}).options || {}).order || 0) - (((b.manifest || {}).options || {}).order || 0); const weight = (WEIGHTS[a.type] || 0) - (WEIGHTS[b.type] || 0); const comp = a.manifest.displayName.localeCompare(b.manifest.displayName); if (order !== 0) { return order; } if (weight !== 0) { return weight; } return comp; }); return enrich(tree, { base, location, id, config, prefix, search }); } function enrich(child, context) { const id = context.id, prefix = context.prefix, search = context.search; const childid = [child.contentType, child.id].join('/'); child.active = childid === id || `doc/${context.parent}/${childid}` === id; const parsed = _url.default.parse(child.href || _path.default.join(prefix, child.id || child.path)); const q = typeof parsed.query === "string" ? _querystring.default.parse(parsed.query) : parsed.query; child.href = _url.default.format({ pathname: pre(context.base || '', parsed.pathname || ''), query: Object.assign({}, context.location.query, q) }); child.warnings = child.warnings || []; child.type = child.contentType === "doc" && typeof (child.manifest.options || {}).query === "string" ? "folder" : "item"; if (child.type === "folder") { child.children = search(child.manifest.options.query).filter(item => item.id !== child.id); } if (child.manifest && child.type === "pattern" && (child.manifest.flag === "alpha" || child.manifest.flag === "deprecated")) { child.warnings.push({ type: "flag", value: child.manifest.flag, message: `${child.manifest.displayName} is flagged as ${child.manifest.flag}.` }); } return child; } function pre(base, pathname) { const b = norm(base); const p = norm(pathname); if (p.startsWith(b)) { return `/${p}`; } return `/${[norm(base), norm(pathname)].join("/")}`; } function norm(p) { return p.split("/").filter(Boolean).join("/"); } //# sourceMappingURL=tree.js.map