@podlite/schema 0.0.6 → 0.0.7
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 +7 -0
- package/lib/ast-helpers.d.ts +1 -0
- package/lib/ast-helpers.js +15 -0
- package/lib/ast-inerator.d.ts +7 -0
- package/lib/ast-inerator.js +70 -0
- package/lib/blocks-helpers.d.ts +20 -11
- package/lib/blocks-helpers.js +52 -35
- package/lib/index.d.ts +6 -8
- package/lib/index.js +35 -53
- package/lib/query-helpers.d.ts +27 -0
- package/lib/query-helpers.js +133 -0
- package/lib/types.d.ts +30 -4
- package/package.json +20 -14
- package/schema/AstTree.json +329 -140
- package/schema/PodliteDocument.json +329 -140
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
# @podlite/schema
|
|
2
2
|
|
|
3
|
+
## Upcoming
|
|
4
|
+
## 0.0.7
|
|
5
|
+
- add ast interator module (ast-inerator)
|
|
6
|
+
- add AST nodes for image and caption objects, add mkImage and mkCaption helpers
|
|
7
|
+
- add mkBlockItem, getTextContentFromNode and query blocks from AST helpers
|
|
8
|
+
- add id prop to Node type
|
|
9
|
+
- toc elements support
|
|
3
10
|
## 0.0.6
|
|
4
11
|
- list of supported schemas is explicit now
|
|
5
12
|
- add support for create "blankline" blocks (mkBlankline)
|
|
@@ -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,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(`[pod] 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)));
|
|
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;
|
package/lib/blocks-helpers.d.ts
CHANGED
|
@@ -1,18 +1,27 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export declare const mkNode: (attr:
|
|
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: () =>
|
|
5
|
+
export declare const mkBlankline: () => {
|
|
6
|
+
type: string;
|
|
7
|
+
};
|
|
6
8
|
export declare const mkFomattingCode: (attrs: any, content: any) => any;
|
|
7
9
|
export declare const mkFomattingCodeL: (attrs: any, content: any) => any;
|
|
8
|
-
export declare const mkVerbatim: (text: any) =>
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
export declare const
|
|
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;
|
|
16
19
|
export declare const mkRootBlock: ({ margin }: {
|
|
17
20
|
margin: any;
|
|
18
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;
|
package/lib/blocks-helpers.js
CHANGED
|
@@ -1,58 +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.
|
|
15
|
-
|
|
16
|
-
|
|
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
|
-
|
|
9
|
+
const filterNulls = (content) => {
|
|
20
10
|
if (Array.isArray(content)) {
|
|
21
|
-
return content.filter(
|
|
11
|
+
return content.filter(i => i);
|
|
22
12
|
}
|
|
23
13
|
console.warn('[podlite-schema] filterNulls got not array as content');
|
|
24
14
|
};
|
|
25
15
|
exports.filterNulls = filterNulls;
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
var result = exports.mkNode(
|
|
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) });
|
|
31
21
|
return result;
|
|
32
22
|
};
|
|
33
23
|
exports.mkBlock = mkBlock;
|
|
34
|
-
|
|
24
|
+
const mkBlankline = () => { return (0, exports.mkNode)({ type: 'blankline' }); };
|
|
35
25
|
exports.mkBlankline = mkBlankline;
|
|
36
|
-
|
|
37
|
-
return exports.mkNode(
|
|
26
|
+
const mkFomattingCode = (attrs, content) => {
|
|
27
|
+
return (0, exports.mkNode)({ type: 'fcode', ...attrs, content });
|
|
38
28
|
};
|
|
39
29
|
exports.mkFomattingCode = mkFomattingCode;
|
|
40
|
-
|
|
41
|
-
|
|
30
|
+
const mkFomattingCodeL = (attrs, content) => {
|
|
31
|
+
let res = (0, exports.mkNode)({ ...attrs, type: 'fcode', name: "L", content: (0, exports.filterNulls)(content) });
|
|
42
32
|
return res;
|
|
43
33
|
};
|
|
44
34
|
exports.mkFomattingCodeL = mkFomattingCodeL;
|
|
45
|
-
|
|
46
|
-
return exports.mkNode({ "type": "verbatim", "value": text });
|
|
35
|
+
const mkVerbatim = (text) => {
|
|
36
|
+
return (0, exports.mkNode)({ "type": "verbatim", "value": text });
|
|
47
37
|
};
|
|
48
38
|
exports.mkVerbatim = mkVerbatim;
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
return exports.
|
|
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 });
|
|
52
46
|
};
|
|
53
|
-
exports.
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
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);
|
|
57
70
|
};
|
|
58
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 {
|
|
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,54 +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.
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
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");
|
|
44
33
|
__exportStar(require("./types"), exports);
|
|
45
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; } });
|
|
46
39
|
function toAst() {
|
|
47
40
|
return {};
|
|
48
41
|
}
|
|
49
42
|
exports.toAst = toAst;
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
rules[rule] = function (n, ctx, visiter) {
|
|
67
|
-
results.push(n);
|
|
68
|
-
if ('content' in n) {
|
|
69
|
-
return { n: n, content: transformer(n.content, __assign({}, ctx)) };
|
|
70
|
-
}
|
|
71
|
-
};
|
|
72
|
-
}
|
|
73
|
-
var transformer = makeTransformer_1.default(rules);
|
|
74
|
-
transformer(tree, {});
|
|
75
|
-
return results;
|
|
76
|
-
};
|
|
77
|
-
exports.getFromTree = getFromTree;
|
|
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;
|
|
78
59
|
function validatePodliteAst(data) { return validateAst(data, 'PodliteDocument'); }
|
|
79
60
|
exports.validatePodliteAst = validatePodliteAst;
|
|
80
|
-
function validateAst(data,
|
|
81
|
-
|
|
82
|
-
|
|
61
|
+
function validateAstTree(data) { return validateAst(data, 'AstTree'); }
|
|
62
|
+
exports.validateAstTree = validateAstTree;
|
|
63
|
+
function validateAst(data, Name = 'AstTree') {
|
|
64
|
+
const AstTreeSchema = jsonShemes[Name];
|
|
83
65
|
if (!AstTreeSchema) {
|
|
84
|
-
console.warn(
|
|
66
|
+
console.warn(`[validateAst] Can't exists ${Name} scheme.`);
|
|
85
67
|
}
|
|
86
|
-
|
|
68
|
+
const validate = ajv.compile(AstTreeSchema);
|
|
87
69
|
if (validate(data)) {
|
|
88
70
|
return [];
|
|
89
71
|
}
|
|
@@ -93,13 +75,13 @@ exports.validateAst = validateAst;
|
|
|
93
75
|
function isValidateError(result, src) {
|
|
94
76
|
if (result.length > 0) {
|
|
95
77
|
// get most erorred dataPath
|
|
96
|
-
|
|
97
|
-
|
|
78
|
+
let errors_by_path = {};
|
|
79
|
+
const reducer = (acc, value) => {
|
|
98
80
|
acc[value.dataPath] = (acc[value.dataPath] || 0) + 1;
|
|
99
81
|
return acc;
|
|
100
82
|
};
|
|
101
|
-
|
|
102
|
-
|
|
83
|
+
const pathMap = result.reduce(reducer, errors_by_path);
|
|
84
|
+
const mostlyError = (Object.keys(pathMap).sort((a, b) => pathMap[b] - pathMap[a]) || [])[0];
|
|
103
85
|
return pointer.get(src, mostlyError);
|
|
104
86
|
}
|
|
105
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
|
|
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
|
-
|
|
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;
|