UNPKG

@podlite/schema

Version:

AST tools for Podlite markup language

123 lines 4.25 kB
import Ajv from 'ajv'; import * as pointer from 'json-pointer'; import * as jsonShemes from '../schema'; import { makeInterator } from './ast-inerator'; export * from './types'; export * from './blocks-helpers'; export * from './query-helpers'; export * from './ast-helpers'; export * from './helpers/handlers'; export { makeInterator } from './ast-inerator'; export { makeTransformer, isNamedBlock, isSemanticBlock } from './helpers/makeTransformer'; export { toAny } from './exportAny'; export { makeAttrs } from './helpers/config'; export { pluginCleanLocation } from './plugin-clean-location'; export { toAnyRules } from './helpers/plugins'; export { podlitePluggable, cleanIds, frozenIds } from './pluggableParser'; import * as parser from './grammar'; import vmargin_plug from './plugin-vmargin'; import formattingCodes_plug from './plugin-formatting-codes'; import itemsNumbering_plug from './plugin-items'; import heading_plug from './plugin-heading'; import defnGroup_plug from './plugin-group-defn'; import itemsGroup_plug from './plugin-group-items'; import defnTerms_plug from './plugin-defn-fill-term'; import table_plug from './plugin-tables'; export function toAst() { return {}; } const ajv = new Ajv({ strict: true, allowUnionTypes: true }); export function getTextContentFromNode(node) { let text = ''; const rules = { ':text': node => { text += node.value; }, ':verbatim': node => { text += node.value; }, }; const transformer = makeInterator(rules); const res = transformer(node, {}); return text; } export function validatePodliteAst(data) { return validateAst(data, 'PodliteDocument'); } export function validateAstTree(data) { return validateAst(data, 'AstTree'); } export function validateAst(data, Name = 'AstTree') { const AstTreeSchema = jsonShemes[Name]; if (!AstTreeSchema) { console.warn(`[validateAst] Can't exists ${Name} scheme.`); } const validate = ajv.compile(AstTreeSchema); if (validate(data)) { return []; } return validate.errors || []; } export function isValidateError(result, src) { if (result.length > 0) { // get most erorred dataPath let errors_by_path = {}; const reducer = (acc, value) => { acc[value.dataPath] = (acc[value.dataPath] || 0) + 1; return acc; }; const pathMap = result.reduce(reducer, errors_by_path); const mostlyError = (Object.keys(pathMap).sort((a, b) => pathMap[b] - pathMap[a]) || [])[0]; return pointer.get(src, mostlyError); } return undefined; } function makeTree() { var plugins = []; chain.use = use; chain.parse = parse; chain.use(vmargin_plug); chain.use(itemsNumbering_plug); chain.use(heading_plug); chain.use(defnTerms_plug); chain.use(table_plug); chain.use(formattingCodes_plug); // save order for the next two plugins chain.use(itemsGroup_plug); chain.use(defnGroup_plug); return chain; function chain() { return; } function use(plugin) { if (!['function'].includes(typeof plugin)) { throw plugin; } plugins.push(plugin); return chain; } function parse(src, opt = { skipChain: 0, podMode: 1 }) { let tree = parser.parse(src, { podMode: opt.podMode }); if (!opt.skipChain) { for (let i = 0; i < plugins.length; i++) { const plugin = plugins[i]; // init const visitor = plugin(opt); // process tree tree = visitor(tree); } } return tree; } } export { makeTree as toTree }; const parse = makeTree().parse; export { parse as parse }; export { default as toHtml } from './exportHtml'; export { default as Writer } from './writer'; // Cannot be `import` as it's not under TS root dir // https://stackoverflow.com/questions/51070138/how-to-import-package-json-into-typescript-file-without-including-it-in-the-comp const { version: VERSION } = require('../package.json'); // const {version: VERSION} = {version:'0.1.0'}; export { VERSION as version }; //# sourceMappingURL=index.js.map