UNPKG

@awesome-fe/translate

Version:
104 lines 4.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.renderContent = exports.BlockNodeRenderer = void 0; const base_node_renderer_1 = require("./base-node-renderer"); const add_quotes_1 = require("./utils/add-quotes"); const split_attribute_value_1 = require("./utils/split-attribute-value"); const adoc_1 = require("../../utils/adoc"); class BlockNodeRenderer extends base_node_renderer_1.BaseNodeRenderer { render(node) { const header = this.renderHeader(node); const body = this.renderBody(node); return [header, body].filter(it => !!it).join('\n'); } renderHeader(node) { const attributes = this.renderAttributes(this.getBlockAttributes(node)); return [ attributes ? `[${attributes}]` : '', this.buildBlockTitle(node), ].filter(it => !!it).join('\n'); } getBlockTitle(node) { return node.getAttribute('title'); } renderBody(node) { return `${this.renderChildren(node)}\n`; } buildBlockTitle(node) { const title = this.getBlockTitle(node)?.toString(); const blockTitle = title?.trim(); if (!blockTitle) { return ''; } if ((adoc_1.adoc.isSection(node) || adoc_1.adoc.isFloatingTitle(node)) && blockTitle === node.getTitle()) { return ''; } return `.${blockTitle}`; } renderChildren(node) { const content = node.getContent?.(); return renderContent(content); } renderAttributes(attributes) { const shortenAttributes = this.shortenAttributes(attributes); const content = shortenAttributes .map(it => this.renderAttribute(it, shortenAttributes)) .filter(it => !!it).join(','); return content ?? ''; } renderAttribute(attr, attributes = []) { const value = (0, add_quotes_1.addQuotes)(attr.value); if (attr.name === 'id') { return attributes.length > 1 ? `#${value}` : `[${value}]`; } else if (optionNamePattern.test(attr.name)) { const optionName = attr.name.replace(optionNamePattern, '$1'); return `%${(0, add_quotes_1.addQuotes)(optionName)}`; } else if (attr.name === 'role') { return (0, split_attribute_value_1.splitAttributeValue)(attr.value).map(it => `.${(0, add_quotes_1.addQuotes)(it)}`).join(''); } else if (attr.position) { return value.toString(); } else { return `${attr.name}=${value}`; } } // 简写 id 和 options 属性,把它们添加特定的前缀,然后追加到第一个位置参数后面 shortenAttributes(attributes) { const id = attributes.find(it => it.name === 'id'); const options = attributes.find(it => optionNamePattern.test(it.name)); const role = attributes.find(it => it.name === 'role'); const firstPositionalAttribute = attributes.find(it => it.position === 1); const idText = id && this.renderAttribute(id, attributes); const optionsText = options && this.renderAttribute(options, attributes); const roleText = role && this.renderAttribute(role, attributes); return attributes .filter(it => !!it) .filter(it => !firstPositionalAttribute?.value || ![id, options, role].includes(it)) .map((it) => it.name === firstPositionalAttribute?.name ? { ...it, value: [it.value, idText, optionsText, roleText].filter(it => !!it).join(''), } : it); } } exports.BlockNodeRenderer = BlockNodeRenderer; function renderContent(content) { if (!content || content.constructor.name === '$NilClass') { return ''; } if (typeof content === 'string') { return content; } if ('convert' in content) { return content.convert(); } else if (content instanceof Array) { return content.map(it => renderContent(it)) .filter(it => !!it).join('\n'); } } exports.renderContent = renderContent; const optionNamePattern = /^(.*?)-option$/; //# sourceMappingURL=block-node-renderer.js.map