@podlite/schema 0.0.4 → 0.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,20 @@
1
1
  # @podlite/schema
2
2
 
3
+ ## Upcoming
4
+ ## 0.0.8
5
+ - fix null elements in AST
6
+ ## 0.0.7
7
+ - add ast interator module (ast-inerator)
8
+ - add AST nodes for image and caption objects, add mkImage and mkCaption helpers
9
+ - add mkBlockItem, getTextContentFromNode and query blocks from AST helpers
10
+ - add id prop to Node type
11
+ - toc elements support
12
+ ## 0.0.6
13
+ - list of supported schemas is explicit now
14
+ - add support for create "blankline" blocks (mkBlankline)
15
+ - enhance debug
16
+ ## 0.0.5
17
+ - update deps
3
18
  ## 0.0.4
4
19
  - fix npm package
5
20
  - add sometypes of blocks
@@ -0,0 +1 @@
1
+ export declare const getNodeId: (node: any, ctx: any) => any;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.getNodeId = void 0;
7
+ const config_1 = __importDefault(require("pod6/built/helpers/config"));
8
+ const getNodeId = (node, ctx) => {
9
+ const conf = (0, config_1.default)(node, ctx);
10
+ if (conf.exists('id')) {
11
+ return conf.getFirstValue('id');
12
+ }
13
+ return node.id;
14
+ };
15
+ exports.getNodeId = getNodeId;
@@ -0,0 +1,7 @@
1
+ export declare function isNamedBlock(name: any): boolean;
2
+ export declare function isSemanticBlock(node: any): boolean;
3
+ export declare const makeInterator: (rule: any) => {
4
+ (node: any, context?: any): any;
5
+ rules: any[];
6
+ };
7
+ export default makeInterator;
@@ -0,0 +1,70 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.makeInterator = exports.isSemanticBlock = exports.isNamedBlock = void 0;
4
+ const { makeRule, makePlug } = require('pod6/built/helpers/makeQuery');
5
+ // the following names: MyBlock, myBlock are use for extending pod6
6
+ function isNamedBlock(name) {
7
+ return (name
8
+ &&
9
+ name !== name.toLowerCase()
10
+ &&
11
+ name !== name.toUpperCase());
12
+ }
13
+ exports.isNamedBlock = isNamedBlock;
14
+ // skip warnings for semantic blocks
15
+ function isSemanticBlock(node) {
16
+ const name = node.name || '';
17
+ const isTypeBlock = (node.type || '') === 'block';
18
+ return isTypeBlock && name === name.toUpperCase();
19
+ }
20
+ exports.isSemanticBlock = isSemanticBlock;
21
+ function flattenDeep(arr) {
22
+ return arr.reduce((acc, val) => Array.isArray(val) ? acc.concat(flattenDeep(val)) : acc.concat(val), []);
23
+ }
24
+ const makeInterator = (rule) => {
25
+ let rules = [];
26
+ function use(key, fn) {
27
+ if (key instanceof Array) {
28
+ console.warn(`[podlite-interator] Unsupported param for ${key}`);
29
+ }
30
+ if (key instanceof Object) {
31
+ for (var prop in key) {
32
+ if (key.hasOwnProperty(prop)) {
33
+ use(prop, key[prop]);
34
+ }
35
+ }
36
+ return [];
37
+ }
38
+ rules.push(makeRule(makePlug(key), fn));
39
+ }
40
+ function visiter(node, context = {}) {
41
+ if (node instanceof Array) {
42
+ return flattenDeep(node.map(item => visiter(item, context))).filter(Boolean);
43
+ }
44
+ if ('string' === typeof node) {
45
+ // convert string to lex node with type
46
+ return visiter({ type: 'text', value: node }, context);
47
+ return node;
48
+ }
49
+ // get first rule for this node
50
+ const reversed = rules.slice();
51
+ reversed.reverse();
52
+ const ruleIndex = reversed.findIndex(rule => rule.isFor(node));
53
+ if (ruleIndex !== -1) {
54
+ return reversed[ruleIndex].fn(node, context, visiter);
55
+ }
56
+ else {
57
+ // not found rule
58
+ const newNode = { ...node };
59
+ if (newNode.hasOwnProperty('content')) {
60
+ newNode.content = visiter(newNode.content, context);
61
+ }
62
+ return newNode;
63
+ }
64
+ }
65
+ use(rule);
66
+ visiter.rules = rules;
67
+ return visiter;
68
+ };
69
+ exports.makeInterator = makeInterator;
70
+ exports.default = exports.makeInterator;
@@ -1,17 +1,27 @@
1
- import { BlockImage, Location, RootBlock } from "./types";
2
- export declare const mkNode: (attr: any) => any;
1
+ import { AstTree, BlockCaption, BlockItem, Location, PodNode, RootBlock, Toc, TocItem, TocList, Node, Image } from "./types";
2
+ export declare const mkNode: <P extends Record<string, any>>(attr: P) => P;
3
3
  export declare const filterNulls: (content: any) => any[];
4
4
  export declare const mkBlock: (attrs: any, content: any) => any;
5
+ export declare const mkBlankline: () => {
6
+ type: string;
7
+ };
5
8
  export declare const mkFomattingCode: (attrs: any, content: any) => any;
6
9
  export declare const mkFomattingCodeL: (attrs: any, content: any) => any;
7
- export declare const mkVerbatim: (text: any) => any;
8
- export interface mkBlockImageParams {
9
- src: string;
10
- alt?: string;
11
- location: Location;
12
- margin: string;
13
- }
14
- export declare const mkBlockImage: ({ src, alt, location, margin }: mkBlockImageParams) => BlockImage;
10
+ export declare const mkVerbatim: (text: any) => {
11
+ type: string;
12
+ value: any;
13
+ };
14
+ export declare const mkToc: (content: TocList, title?: string) => Toc;
15
+ export declare const mkTocList: (content: Array<TocItem | TocList>, level: number) => TocList;
16
+ export declare const mkTocItem: (content: PodNode) => TocItem;
17
+ export declare const mkCaption: (content: Array<Node>) => BlockCaption;
18
+ export declare const mkImage: (src: string, alt?: string) => Image;
15
19
  export declare const mkRootBlock: ({ margin }: {
16
20
  margin: any;
17
21
  }, content: any) => RootBlock;
22
+ export interface mkBlockItemParams {
23
+ level: number;
24
+ location: Location;
25
+ margin: string;
26
+ }
27
+ export declare const mkItemBlock: ({ level, location, margin }: mkBlockItemParams, content: AstTree) => BlockItem;
@@ -1,55 +1,75 @@
1
1
  "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
2
  Object.defineProperty(exports, "__esModule", { value: true });
14
- exports.mkRootBlock = exports.mkBlockImage = exports.mkVerbatim = exports.mkFomattingCodeL = exports.mkFomattingCode = exports.mkBlock = exports.filterNulls = exports.mkNode = void 0;
15
- var mkNode = function (attr) {
16
- return __assign({}, attr);
3
+ exports.mkItemBlock = exports.mkRootBlock = exports.mkImage = exports.mkCaption = exports.mkTocItem = exports.mkTocList = exports.mkToc = exports.mkVerbatim = exports.mkFomattingCodeL = exports.mkFomattingCode = exports.mkBlankline = exports.mkBlock = exports.filterNulls = exports.mkNode = void 0;
4
+ const nanoid_1 = require("nanoid");
5
+ const mkNode = (attr) => {
6
+ return { ...attr };
17
7
  };
18
8
  exports.mkNode = mkNode;
19
- var filterNulls = function (content) {
9
+ const filterNulls = (content) => {
20
10
  if (Array.isArray(content)) {
21
- return content.filter(function (i) { return i; });
11
+ return content.filter(i => i);
22
12
  }
13
+ console.warn('[podlite-schema] filterNulls got not array as content');
23
14
  };
24
15
  exports.filterNulls = filterNulls;
25
- var mkBlock = function (attrs, content) {
26
- var type = "block";
27
- var name = attrs.name;
28
- var attributres = __assign({}, attrs);
29
- var result = exports.mkNode(__assign(__assign({ type: type }, attributres), { content: exports.filterNulls(content) }));
16
+ const mkBlock = (attrs, content) => {
17
+ const type = "block";
18
+ const name = attrs.name;
19
+ const attributes = { id: (0, nanoid_1.nanoid)(), ...attrs };
20
+ var result = (0, exports.mkNode)({ type, ...attributes, content: (0, exports.filterNulls)(content) });
30
21
  return result;
31
22
  };
32
23
  exports.mkBlock = mkBlock;
33
- var mkFomattingCode = function (attrs, content) {
34
- return exports.mkNode(__assign(__assign({ type: 'fcode' }, attrs), { content: content }));
24
+ const mkBlankline = () => { return (0, exports.mkNode)({ type: 'blankline' }); };
25
+ exports.mkBlankline = mkBlankline;
26
+ const mkFomattingCode = (attrs, content) => {
27
+ return (0, exports.mkNode)({ type: 'fcode', ...attrs, content });
35
28
  };
36
29
  exports.mkFomattingCode = mkFomattingCode;
37
- var mkFomattingCodeL = function (attrs, content) {
38
- var res = exports.mkNode(__assign(__assign({}, attrs), { type: 'fcode', name: "L", content: exports.filterNulls(content) }));
30
+ const mkFomattingCodeL = (attrs, content) => {
31
+ let res = (0, exports.mkNode)({ ...attrs, type: 'fcode', name: "L", content: (0, exports.filterNulls)(content) });
39
32
  return res;
40
33
  };
41
34
  exports.mkFomattingCodeL = mkFomattingCodeL;
42
- var mkVerbatim = function (text) {
43
- return exports.mkNode({ "type": "verbatim", "value": text });
35
+ const mkVerbatim = (text) => {
36
+ return (0, exports.mkNode)({ "type": "verbatim", "value": text });
44
37
  };
45
38
  exports.mkVerbatim = mkVerbatim;
46
- var mkBlockImage = function (_a) {
47
- var src = _a.src, alt = _a.alt, location = _a.location, margin = _a.margin;
48
- return exports.mkBlock({ name: 'image', location: location, margin: margin }, [{ type: 'image', src: src, alt: alt }]);
39
+ // Table of contents helpers
40
+ const mkToc = (content, title) => {
41
+ return (0, exports.mkNode)({ type: 'toc', title, content });
42
+ };
43
+ exports.mkToc = mkToc;
44
+ const mkTocList = (content, level) => {
45
+ return (0, exports.mkNode)({ type: 'toc-list', level, content });
49
46
  };
50
- exports.mkBlockImage = mkBlockImage;
51
- var mkRootBlock = function (_a, content) {
52
- var margin = _a.margin;
53
- return exports.mkBlock({ name: 'root', margin: margin }, content);
47
+ exports.mkTocList = mkTocList;
48
+ const mkTocItem = (content) => {
49
+ return (0, exports.mkNode)({ type: 'toc-item', node: content, content: [content] });
50
+ };
51
+ exports.mkTocItem = mkTocItem;
52
+ const mkCaption = (content) => {
53
+ return {
54
+ type: 'block',
55
+ name: 'caption',
56
+ content
57
+ };
58
+ };
59
+ exports.mkCaption = mkCaption;
60
+ const mkImage = (src, alt) => {
61
+ return {
62
+ type: 'image',
63
+ src,
64
+ alt
65
+ };
66
+ };
67
+ exports.mkImage = mkImage;
68
+ const mkRootBlock = ({ margin }, content) => {
69
+ return (0, exports.mkBlock)({ name: 'root', margin }, content);
54
70
  };
55
71
  exports.mkRootBlock = mkRootBlock;
72
+ const mkItemBlock = ({ level, location, margin }, content) => {
73
+ return (0, exports.mkBlock)({ name: 'item', level, margin, location }, content);
74
+ };
75
+ exports.mkItemBlock = mkItemBlock;
package/lib/index.d.ts CHANGED
@@ -1,18 +1,16 @@
1
1
  import { ErrorObject } from 'ajv';
2
- import { PodliteDocument } from './types';
2
+ import { PodNode } from './types';
3
3
  export { AstTree } from './types';
4
4
  export * from './types';
5
5
  export * from './blocks-helpers';
6
+ export * from './query-helpers';
7
+ export * from './ast-helpers';
8
+ export { makeInterator } from './ast-inerator';
6
9
  export declare function toAst(): {};
7
10
  export declare type SchemaValidationError = ErrorObject<string, Record<string, any>>;
8
- /**
9
- * Get nodes by queries
10
- * @param tree
11
- * @param queries
12
- * @returns array of matched nodes
13
- */
14
- export declare const getFromTree: (tree: PodliteDocument, ...queries: string[]) => any[];
11
+ export declare function getTextContentFromNode(node: PodNode): string;
15
12
  export declare function validatePodliteAst(data: unknown): SchemaValidationError[];
13
+ export declare function validateAstTree(data: unknown): SchemaValidationError[];
16
14
  export declare function validateAst(data: unknown, Name?: string): SchemaValidationError[];
17
15
  export declare function isValidateError(result: SchemaValidationError[], src: any): any;
18
16
  export interface Test {
package/lib/index.js CHANGED
@@ -1,15 +1,4 @@
1
1
  "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
2
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
14
3
  if (k2 === undefined) k2 = k;
15
4
  Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
@@ -36,50 +25,47 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
36
25
  return (mod && mod.__esModule) ? mod : { "default": mod };
37
26
  };
38
27
  Object.defineProperty(exports, "__esModule", { value: true });
39
- exports.isValidateError = exports.validateAst = exports.validatePodliteAst = exports.getFromTree = exports.toAst = void 0;
40
- var ajv_1 = __importDefault(require("ajv"));
41
- var pointer = __importStar(require("json-pointer"));
42
- var makeTransformer_1 = __importDefault(require("pod6/built/helpers/makeTransformer"));
28
+ exports.isValidateError = exports.validateAst = exports.validateAstTree = exports.validatePodliteAst = exports.getTextContentFromNode = exports.toAst = exports.makeInterator = void 0;
29
+ const ajv_1 = __importDefault(require("ajv"));
30
+ const pointer = __importStar(require("json-pointer"));
31
+ const jsonShemes = __importStar(require("../schema"));
32
+ const ast_inerator_1 = require("./ast-inerator");
43
33
  __exportStar(require("./types"), exports);
44
34
  __exportStar(require("./blocks-helpers"), exports);
35
+ __exportStar(require("./query-helpers"), exports);
36
+ __exportStar(require("./ast-helpers"), exports);
37
+ var ast_inerator_2 = require("./ast-inerator");
38
+ Object.defineProperty(exports, "makeInterator", { enumerable: true, get: function () { return ast_inerator_2.makeInterator; } });
45
39
  function toAst() {
46
40
  return {};
47
41
  }
48
42
  exports.toAst = toAst;
49
- var ajv = new ajv_1.default({ strict: true, allowUnionTypes: true });
50
- /**
51
- * Get nodes by queries
52
- * @param tree
53
- * @param queries
54
- * @returns array of matched nodes
55
- */
56
- var getFromTree = function (tree) {
57
- var queries = [];
58
- for (var _i = 1; _i < arguments.length; _i++) {
59
- queries[_i - 1] = arguments[_i];
60
- }
61
- var results = [];
62
- var rules = {};
63
- for (var _a = 0, queries_1 = queries; _a < queries_1.length; _a++) {
64
- var rule = queries_1[_a];
65
- rules[rule] = function (n, ctx, visiter) {
66
- results.push(n);
67
- if ('content' in n) {
68
- return { n: n, content: transformer(n.content, __assign({}, ctx)) };
69
- }
70
- };
71
- }
72
- var transformer = makeTransformer_1.default(rules);
73
- transformer(tree, {});
74
- return results;
75
- };
76
- exports.getFromTree = getFromTree;
77
- function validatePodliteAst(data) { return validateAst(data, 'PodliteDocument.json'); }
43
+ const ajv = new ajv_1.default({ strict: true, allowUnionTypes: true });
44
+ function getTextContentFromNode(node) {
45
+ let text = '';
46
+ const rules = {
47
+ ":text": (node) => {
48
+ text += node.value;
49
+ },
50
+ ":verbatim": (node) => {
51
+ text += node.value;
52
+ },
53
+ };
54
+ const transformer = (0, ast_inerator_1.makeInterator)(rules);
55
+ const res = transformer(node, {});
56
+ return text;
57
+ }
58
+ exports.getTextContentFromNode = getTextContentFromNode;
59
+ function validatePodliteAst(data) { return validateAst(data, 'PodliteDocument'); }
78
60
  exports.validatePodliteAst = validatePodliteAst;
79
- function validateAst(data, Name) {
80
- if (Name === void 0) { Name = 'AstTree.json'; }
81
- var AstTreeSchema = require("../schema/" + Name);
82
- var validate = ajv.compile(AstTreeSchema);
61
+ function validateAstTree(data) { return validateAst(data, 'AstTree'); }
62
+ exports.validateAstTree = validateAstTree;
63
+ function validateAst(data, Name = 'AstTree') {
64
+ const AstTreeSchema = jsonShemes[Name];
65
+ if (!AstTreeSchema) {
66
+ console.warn(`[validateAst] Can't exists ${Name} scheme.`);
67
+ }
68
+ const validate = ajv.compile(AstTreeSchema);
83
69
  if (validate(data)) {
84
70
  return [];
85
71
  }
@@ -89,13 +75,13 @@ exports.validateAst = validateAst;
89
75
  function isValidateError(result, src) {
90
76
  if (result.length > 0) {
91
77
  // get most erorred dataPath
92
- var errors_by_path = {};
93
- var reducer = function (acc, value) {
78
+ let errors_by_path = {};
79
+ const reducer = (acc, value) => {
94
80
  acc[value.dataPath] = (acc[value.dataPath] || 0) + 1;
95
81
  return acc;
96
82
  };
97
- var pathMap_1 = result.reduce(reducer, errors_by_path);
98
- var mostlyError = (Object.keys(pathMap_1).sort(function (a, b) { return pathMap_1[b] - pathMap_1[a]; }) || [])[0];
83
+ const pathMap = result.reduce(reducer, errors_by_path);
84
+ const mostlyError = (Object.keys(pathMap).sort((a, b) => pathMap[b] - pathMap[a]) || [])[0];
99
85
  return pointer.get(src, mostlyError);
100
86
  }
101
87
  return undefined;
@@ -0,0 +1,27 @@
1
+ declare type Query = ((par: Object) => Boolean) | any;
2
+ /**
3
+ * compileQuery - compile a query to a function
4
+
5
+ Object as a parameter:
6
+ {'name':'head', level:[1,2,3,4]}
7
+
8
+ const checkObject = compileQuery({name:'head',level:2}, (item)=>item.name === 1);
9
+
10
+ Function as a parameter:
11
+ (item)=>item.name === 1
12
+ const checkObject2 = compileQuery((i)=>i.name == '1')
13
+
14
+ const r = checkObject([{name:'head', level:1},{name:'head', level:2},{name:'head', level:2}, {name:1, level:2}])
15
+ *
16
+ * @param obj
17
+ * @returns function that can be used to filter the objects
18
+ */
19
+ export declare const compileQuery: (...obj: Query[]) => (any: any) => any;
20
+ /**
21
+ * Get nodes by queries
22
+ * @param tree
23
+ * @param queries
24
+ * @returns array of matched nodes
25
+ */
26
+ export declare const getFromTree: (tree: any, ...queries: string[] | any) => any;
27
+ export {};
@@ -0,0 +1,133 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.getFromTree = exports.compileQuery = void 0;
7
+ const makeTransformer_1 = __importDefault(require("pod6/built/helpers/makeTransformer"));
8
+ /**
9
+ * compileQuery - compile a query to a function
10
+
11
+ Object as a parameter:
12
+ {'name':'head', level:[1,2,3,4]}
13
+
14
+ const checkObject = compileQuery({name:'head',level:2}, (item)=>item.name === 1);
15
+
16
+ Function as a parameter:
17
+ (item)=>item.name === 1
18
+ const checkObject2 = compileQuery((i)=>i.name == '1')
19
+
20
+ const r = checkObject([{name:'head', level:1},{name:'head', level:2},{name:'head', level:2}, {name:1, level:2}])
21
+ *
22
+ * @param obj
23
+ * @returns function that can be used to filter the objects
24
+ */
25
+ const compileQuery = (...obj) => {
26
+ const queries = [];
27
+ const compiledQueries = obj.map(compileAtomQuery);
28
+ return function OR(par) {
29
+ const source = par instanceof Array ? par : [par];
30
+ const result = [];
31
+ source.forEach(item => {
32
+ if (item instanceof Array) {
33
+ result.push(...OR(item));
34
+ }
35
+ else {
36
+ // obj should accomplished if some queries is true
37
+ if (compiledQueries.some((fn) => fn(item).length > 0)) {
38
+ result.push(item);
39
+ }
40
+ }
41
+ });
42
+ return result;
43
+ };
44
+ };
45
+ exports.compileQuery = compileQuery;
46
+ const compileAtomQuery = (obj) => {
47
+ const queries = [];
48
+ if (obj instanceof Array) {
49
+ throw new Error("Wrong query parameter [Array], should be [Function] or [Object]");
50
+ }
51
+ else if (typeof obj === 'function') {
52
+ queries.push(obj);
53
+ }
54
+ else {
55
+ for (const entry of Object.entries(obj)) {
56
+ const [key, value] = entry;
57
+ const checkValue = (val) => {
58
+ if (value instanceof Array) {
59
+ return value.includes(val);
60
+ }
61
+ else {
62
+ if (typeof value === 'number') {
63
+ return parseInt(val, 10) === value;
64
+ }
65
+ else {
66
+ return val === value;
67
+ }
68
+ }
69
+ };
70
+ const checkKeyfn = (obj) => {
71
+ if (obj.hasOwnProperty(key)) {
72
+ return checkValue(obj[key]);
73
+ }
74
+ return false;
75
+ };
76
+ queries.push(checkKeyfn);
77
+ }
78
+ }
79
+ return function AND(obj) {
80
+ const source = obj instanceof Array ? obj : [obj];
81
+ const result = [];
82
+ source.forEach(item => {
83
+ if (item instanceof Array) {
84
+ result.push(...AND(item));
85
+ }
86
+ // obj should accomplished if all queries to be true
87
+ if (queries.every((fn) => fn(item))) {
88
+ result.push(item);
89
+ }
90
+ });
91
+ return result;
92
+ };
93
+ };
94
+ /**
95
+ * Get nodes by queries
96
+ * @param tree
97
+ * @param queries
98
+ * @returns array of matched nodes
99
+ */
100
+ const getFromTree = (tree, ...queries) => {
101
+ if (!tree) {
102
+ return [];
103
+ }
104
+ let results = [];
105
+ let rules = {};
106
+ const transformer = (0, makeTransformer_1.default)({ '*:*': (node, ctx) => {
107
+ results.push(node);
108
+ if ('content' in node) {
109
+ return { node, content: transformer(node.content, { ...ctx }) };
110
+ }
111
+ } });
112
+ transformer(tree, {});
113
+ //convert queries
114
+ const queryAtoms = [];
115
+ queries.forEach(q => {
116
+ if (typeof q === 'string') {
117
+ const getNameLevel = q.match(/^(?<name>(item|head|.*))(?<level>(\d+)?)$/);
118
+ if (getNameLevel) {
119
+ let { name, level } = getNameLevel.groups;
120
+ if (name === 'item' && !level) {
121
+ level = '1';
122
+ }
123
+ const levelNum = level ? parseInt(level, 10) : undefined;
124
+ queryAtoms.push({ name, ...(levelNum ? { level: levelNum } : {}) });
125
+ }
126
+ }
127
+ else {
128
+ queryAtoms.push(q);
129
+ }
130
+ });
131
+ return (0, exports.compileQuery)(...queryAtoms)(results);
132
+ };
133
+ exports.getFromTree = getFromTree;
package/lib/types.d.ts CHANGED
@@ -1,8 +1,9 @@
1
1
  export interface RuleHandler<T = any> {
2
- (writer?: any, processor?: any): (node: T, ctx?: any, interator?: any) => void | AstTree | PodNode;
2
+ (writer: any, processor: any, tree: PodliteDocument): (node: T, ctx: any, interator: any) => void | AstTree | PodNode;
3
3
  }
4
4
  export interface Plugin {
5
5
  toAst?: RuleHandler;
6
+ toAstAfter?: RuleHandler;
6
7
  toHtml?: RuleHandler;
7
8
  }
8
9
  export interface Plugins {
@@ -58,6 +59,9 @@ export interface RulesStrict {
58
59
  'table_row': RuleHandler<TableRow>;
59
60
  'table_cell': RuleHandler<TableCell>;
60
61
  'table_head': RuleHandler<TableHead>;
62
+ ':toc': RuleHandler<Toc>;
63
+ ':toc-list': RuleHandler<TocList>;
64
+ ':toc-item': RuleHandler<TocItem>;
61
65
  'Diagram': RuleHandler<BlockDiagram>;
62
66
  'Image': RuleHandler<BlockNamed>;
63
67
  }
@@ -78,9 +82,30 @@ export interface Image {
78
82
  src: string;
79
83
  alt?: string;
80
84
  }
85
+ export interface Toc {
86
+ type: 'toc';
87
+ title?: string;
88
+ content: TocList;
89
+ }
90
+ export interface TocList {
91
+ type: 'toc-list';
92
+ level: number;
93
+ content: Array<TocItem | TocList>;
94
+ }
95
+ export interface TocItem {
96
+ type: 'toc-item';
97
+ node: PodNode;
98
+ content: Array<Node>;
99
+ }
81
100
  export interface BlockImage extends Omit<Block, 'content'> {
82
101
  name: 'image';
83
- content: [Image];
102
+ caption?: string;
103
+ link?: string;
104
+ content: [Image, BlockCaption];
105
+ }
106
+ export interface BlockCaption extends Omit<Block, 'content' | 'location' | 'margin' | 'config' | 'id'> {
107
+ name: 'caption';
108
+ content: Array<Node>;
84
109
  }
85
110
  export interface RootBlock extends Omit<Block, 'location'> {
86
111
  name: 'root';
@@ -226,6 +251,7 @@ export interface Block {
226
251
  content: Array<Node>;
227
252
  margin: string;
228
253
  config?: Array<ConfigItem | BrokenConfigItem>;
254
+ id?: string;
229
255
  }
230
256
  export interface BlockPod extends Block {
231
257
  name: 'pod';
@@ -290,7 +316,7 @@ export interface TableSeparator {
290
316
  export declare type BlockAny = BlockNamed;
291
317
  export interface BlockNamed extends Omit<Block, 'content'> {
292
318
  name: Capitalize<string>;
293
- content: [(Verbatim | Para | Code)?];
319
+ content: [(Verbatim | Para | Code)?] | Array<Image | BlockCaption>;
294
320
  }
295
321
  export interface BlockDiagram extends Omit<BlockNamed, 'content'> {
296
322
  name: 'Diagram';
@@ -300,7 +326,7 @@ export interface BlockDiagram extends Omit<BlockNamed, 'content'> {
300
326
  };
301
327
  }
302
328
  export declare type FormattingCodes = FormattingCodeA | FormattingCodeC | FormattingCodeB | FormattingCodeD | FormattingCodeE | FormattingCodeI | FormattingCodeL | FormattingCodeN | FormattingCodeX | FormattingCodeZ | FormattingCodeV | FormattingCodeS | FormattingCodeAny;
303
- export declare type PodNode = Ambient | BlockPod | BlockData | BlockCode | BlankLine | BlockNested | BlockOutput | BlockInput | BlockPara | BlockHead | BlockComment | BlockDefn | string | Para | Text | Code | BlockNamed | BlockAny | Verbatim | BlockTable | List | BlockConfig | BlockItem | Alias | BlockImage | Image | RootBlock | Separator | FormattingCodes;
329
+ export declare type PodNode = Ambient | BlockPod | BlockData | BlockCode | BlankLine | BlockNested | BlockOutput | BlockInput | BlockPara | BlockHead | BlockComment | BlockDefn | string | Para | Text | Code | BlockNamed | BlockAny | Verbatim | BlockTable | List | BlockConfig | BlockItem | Alias | BlockImage | Image | RootBlock | Separator | BlockCaption | FormattingCodes | Toc;
304
330
  export declare type Node = PodNode;
305
331
  export declare type AstTree = Array<PodNode>;
306
332
  export declare type PodliteDocument = RootBlock;