@podlite/schema 0.0.38 → 0.0.39

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.
@@ -0,0 +1,18 @@
1
+ import { PodliteDocument, PodNode } from './index';
2
+ /**
3
+ * Minimal corpus item that the selector engine needs. Concrete consumers
4
+ * (publisher's `publishRecord`, editor preview, etc.) supply richer
5
+ * objects; only `file` and `node` are read here.
6
+ */
7
+ export declare type SelectorDoc = {
8
+ file: string;
9
+ node: PodNode | PodliteDocument;
10
+ };
11
+ export declare type ParsedSelector = {
12
+ scheme?: string;
13
+ document?: string;
14
+ anchor?: string;
15
+ blockFilters: string[];
16
+ };
17
+ export declare const parseSelector: (selector: string) => ParsedSelector | undefined;
18
+ export declare const runSelector: <T extends SelectorDoc>(selector: string, docs: T[]) => PodNode[] | T[];
@@ -0,0 +1,158 @@
1
+ import { getFromTree, getNodeId, getTextContentFromNode, makeAttrs } from './index';
2
+ export const parseSelector = (selector) => {
3
+ const trimmed = selector.trim();
4
+ if (!trimmed)
5
+ return undefined;
6
+ // Split on the first '|' — left is source, right is blocks selector
7
+ const pipeIdx = trimmed.indexOf('|');
8
+ const sourcePart = (pipeIdx === -1 ? trimmed : trimmed.slice(0, pipeIdx)).trim();
9
+ const filterPart = pipeIdx === -1 ? '' : trimmed.slice(pipeIdx + 1).trim();
10
+ const blockFilters = filterPart
11
+ ? filterPart
12
+ .split(',')
13
+ .map(s => s.trim())
14
+ .filter(Boolean)
15
+ : [];
16
+ // Source: scheme:path or scheme:path#anchor
17
+ const sourceMatch = sourcePart.match(/^([^:]+):([^#]+)(?:#(.+))?$/);
18
+ if (sourceMatch) {
19
+ return {
20
+ scheme: sourceMatch[1],
21
+ document: sourceMatch[2].trim(),
22
+ anchor: sourceMatch[3],
23
+ blockFilters,
24
+ };
25
+ }
26
+ if (blockFilters.length > 0) {
27
+ return { blockFilters };
28
+ }
29
+ return undefined;
30
+ };
31
+ // Normalize a path for loose suffix comparison:
32
+ // 'src/foo.podlite' ~= 'foo.podlite'
33
+ // './includes/x.podlite' ~= 'includes/x.podlite'
34
+ const normalizePath = (p) => p.replace(/\\/g, '/').replace(/^\.\//, '');
35
+ const isGlobPattern = (s) => /[*?[]/.test(s);
36
+ // Convert a glob pattern to an anchored RegExp.
37
+ // **/ → (?:.*/)? zero or more directory segments (lets **/foo match root-level foo)
38
+ // ** → .* any characters, crosses /
39
+ // * → [^/]* any characters within a single segment
40
+ // ? → [^/] single character within a segment
41
+ // Other regex meta characters are escaped.
42
+ const globRegexCache = new Map();
43
+ const globToRegex = (glob) => {
44
+ const cached = globRegexCache.get(glob);
45
+ if (cached)
46
+ return cached;
47
+ let re = '';
48
+ let i = 0;
49
+ while (i < glob.length) {
50
+ const c = glob[i];
51
+ const next = glob[i + 1];
52
+ if (c === '*' && next === '*' && glob[i + 2] === '/') {
53
+ re += '(?:.*/)?';
54
+ i += 3;
55
+ }
56
+ else if (c === '*' && next === '*') {
57
+ re += '.*';
58
+ i += 2;
59
+ }
60
+ else if (c === '*') {
61
+ re += '[^/]*';
62
+ i += 1;
63
+ }
64
+ else if (c === '?') {
65
+ re += '[^/]';
66
+ i += 1;
67
+ }
68
+ else if (/[\\^$.()+|{}[\]]/.test(c)) {
69
+ re += '\\' + c;
70
+ i += 1;
71
+ }
72
+ else {
73
+ re += c;
74
+ i += 1;
75
+ }
76
+ }
77
+ const compiled = new RegExp(`^${re}$`);
78
+ globRegexCache.set(glob, compiled);
79
+ return compiled;
80
+ };
81
+ const filePathMatches = (docFile, target) => {
82
+ const a = normalizePath(docFile);
83
+ const b = normalizePath(target);
84
+ if (isGlobPattern(b)) {
85
+ const rx = globToRegex(b);
86
+ if (rx.test(a))
87
+ return true;
88
+ // Suffix-tolerant match: allow any parent prefix (consistent with
89
+ // non-glob suffix matching, so 'src/00-foo/x.pod' matches '00-foo/x.pod').
90
+ const body = rx.source.slice(1, -1);
91
+ const rxSuffix = new RegExp(`^(?:.*/)${body}$`);
92
+ return rxSuffix.test(a);
93
+ }
94
+ return a === b || a.endsWith('/' + b) || b.endsWith('/' + a);
95
+ };
96
+ const getDocIDs = (doc) => {
97
+ const ids = [];
98
+ getFromTree(doc.node, 'NAME', 'TITLE').forEach(block => {
99
+ const conf = makeAttrs(block, {});
100
+ const title = getTextContentFromNode(block).trim();
101
+ if (conf.exists('id')) {
102
+ const id = conf.getFirstValue('id');
103
+ if (id)
104
+ ids.push(id);
105
+ }
106
+ ids.push(title);
107
+ });
108
+ return ids;
109
+ };
110
+ function getMapIDsBlocks(srcNode) {
111
+ const idsMap = new Map();
112
+ getFromTree(srcNode, { type: 'block' }).forEach(i => {
113
+ const id = getNodeId(i, {});
114
+ if (id)
115
+ idsMap.set(id, i);
116
+ });
117
+ return idsMap;
118
+ }
119
+ export const runSelector = (selector, docs) => {
120
+ const parsed = parseSelector(selector);
121
+ if (!parsed)
122
+ return [];
123
+ const { scheme, document, anchor, blockFilters } = parsed;
124
+ let matchedDocs = docs;
125
+ if (scheme === 'doc' && document) {
126
+ matchedDocs = docs.filter(doc => getDocIDs(doc).includes(document));
127
+ }
128
+ else if (scheme === 'file' && document) {
129
+ matchedDocs = docs.filter(doc => filePathMatches(doc.file, document));
130
+ }
131
+ else if (scheme && scheme !== 'doc' && scheme !== 'file') {
132
+ return [];
133
+ }
134
+ // Anchor takes precedence — single-block-by-id lookup
135
+ if (anchor) {
136
+ const collectedBlocks = [];
137
+ for (const d of matchedDocs) {
138
+ const idsMap = getMapIDsBlocks(d.node);
139
+ const block = idsMap.get(anchor);
140
+ if (block)
141
+ collectedBlocks.push(block);
142
+ }
143
+ return collectedBlocks;
144
+ }
145
+ // Block filters — extract blocks by name across matched docs
146
+ if (blockFilters.length > 0) {
147
+ const collectedBlocks = [];
148
+ for (const d of matchedDocs) {
149
+ for (const name of blockFilters) {
150
+ collectedBlocks.push(...getFromTree(d.node, name));
151
+ }
152
+ }
153
+ return collectedBlocks;
154
+ }
155
+ // No anchor, no filter — return whole docs
156
+ return matchedDocs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Ac,EAAE,CAAA;QACrC,KAAK,MAAM,CAAC,IAAI,WAAW,EAAE;YAC3B,KAAK,MAAM,IAAI,IAAI,YAAY,EAAE;gBAC/B,eAAe,CAAC,IAAI,CAAC,GAAG,WAAW,CAAC,CAAC,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC,CAAA;aACnD;SACF;QACD,OAAO,eAAe,CAAA;KACvB;IAED,2CAA2C;IAC3C,OAAO,WAAW,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,CAAA;AACrC,CAAC,CAAA"}
package/lib/index.d.ts CHANGED
@@ -78,5 +78,7 @@ export { parse as parse };
78
78
  export { default as toHtml } from './exportHtml';
79
79
  export { default as toMarkdown } from './exportMarkdown';
80
80
  export { default as Writer } from './writer';
81
+ export { parseSelector, runSelector } from './selectors';
82
+ export type { SelectorDoc, ParsedSelector } from './selectors';
81
83
  declare const VERSION: any;
82
84
  export { VERSION as version };
package/lib/index.js CHANGED
@@ -25,7 +25,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
25
25
  return (mod && mod.__esModule) ? mod : { "default": mod };
26
26
  };
27
27
  Object.defineProperty(exports, "__esModule", { value: true });
28
- exports.version = exports.Writer = exports.toMarkdown = exports.toHtml = exports.parse = exports.toTree = exports.isValidateError = exports.validateAst = exports.validateAstTree = exports.validatePodliteAst = exports.getPodContentFromNode = exports.getTextContentFromNode = exports.toAst = exports.frozenIds = exports.cleanIds = exports.podlitePluggable = exports.toAnyRules = exports.pluginCleanLocation = exports.makeAttrs = exports.toAny = exports.isSemanticBlock = exports.isNamedBlock = exports.makeTransformer = exports.makeInterator = void 0;
28
+ exports.version = exports.runSelector = exports.parseSelector = exports.Writer = exports.toMarkdown = exports.toHtml = exports.parse = exports.toTree = exports.isValidateError = exports.validateAst = exports.validateAstTree = exports.validatePodliteAst = exports.getPodContentFromNode = exports.getTextContentFromNode = exports.toAst = exports.frozenIds = exports.cleanIds = exports.podlitePluggable = exports.toAnyRules = exports.pluginCleanLocation = exports.makeAttrs = exports.toAny = exports.isSemanticBlock = exports.isNamedBlock = exports.makeTransformer = exports.makeInterator = void 0;
29
29
  const ajv_1 = __importDefault(require("ajv"));
30
30
  const pointer = __importStar(require("json-pointer"));
31
31
  const jsonShemes = __importStar(require("../schema"));
@@ -189,6 +189,9 @@ var exportMarkdown_1 = require("./exportMarkdown");
189
189
  Object.defineProperty(exports, "toMarkdown", { enumerable: true, get: function () { return __importDefault(exportMarkdown_1).default; } });
190
190
  var writer_1 = require("./writer");
191
191
  Object.defineProperty(exports, "Writer", { enumerable: true, get: function () { return __importDefault(writer_1).default; } });
192
+ var selectors_1 = require("./selectors");
193
+ Object.defineProperty(exports, "parseSelector", { enumerable: true, get: function () { return selectors_1.parseSelector; } });
194
+ Object.defineProperty(exports, "runSelector", { enumerable: true, get: function () { return selectors_1.runSelector; } });
192
195
  // Cannot be `import` as it's not under TS root dir
193
196
  // https://stackoverflow.com/questions/51070138/how-to-import-package-json-into-typescript-file-without-including-it-in-the-comp
194
197
  const { version: VERSION } = require('../package.json');