@podlite/schema 0.0.38 → 0.0.40
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.podlite +14 -0
- package/esm/index.d.ts +2 -0
- package/esm/index.js +1 -0
- package/esm/index.js.map +1 -1
- package/esm/plugin-tables.js +430 -46
- package/esm/plugin-tables.js.map +1 -1
- package/esm/selectors.d.ts +18 -0
- package/esm/selectors.js +158 -0
- package/esm/selectors.js.map +1 -0
- package/lib/index.d.ts +2 -0
- package/lib/index.js +4 -1
- package/lib/plugin-tables.js +430 -46
- package/lib/selectors.d.ts +18 -0
- package/lib/selectors.js +163 -0
- package/package.json +1 -1
package/lib/selectors.js
ADDED
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.runSelector = exports.parseSelector = void 0;
|
|
4
|
+
const index_1 = require("./index");
|
|
5
|
+
const parseSelector = (selector) => {
|
|
6
|
+
const trimmed = selector.trim();
|
|
7
|
+
if (!trimmed)
|
|
8
|
+
return undefined;
|
|
9
|
+
// Split on the first '|' — left is source, right is blocks selector
|
|
10
|
+
const pipeIdx = trimmed.indexOf('|');
|
|
11
|
+
const sourcePart = (pipeIdx === -1 ? trimmed : trimmed.slice(0, pipeIdx)).trim();
|
|
12
|
+
const filterPart = pipeIdx === -1 ? '' : trimmed.slice(pipeIdx + 1).trim();
|
|
13
|
+
const blockFilters = filterPart
|
|
14
|
+
? filterPart
|
|
15
|
+
.split(',')
|
|
16
|
+
.map(s => s.trim())
|
|
17
|
+
.filter(Boolean)
|
|
18
|
+
: [];
|
|
19
|
+
// Source: scheme:path or scheme:path#anchor
|
|
20
|
+
const sourceMatch = sourcePart.match(/^([^:]+):([^#]+)(?:#(.+))?$/);
|
|
21
|
+
if (sourceMatch) {
|
|
22
|
+
return {
|
|
23
|
+
scheme: sourceMatch[1],
|
|
24
|
+
document: sourceMatch[2].trim(),
|
|
25
|
+
anchor: sourceMatch[3],
|
|
26
|
+
blockFilters,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
if (blockFilters.length > 0) {
|
|
30
|
+
return { blockFilters };
|
|
31
|
+
}
|
|
32
|
+
return undefined;
|
|
33
|
+
};
|
|
34
|
+
exports.parseSelector = parseSelector;
|
|
35
|
+
// Normalize a path for loose suffix comparison:
|
|
36
|
+
// 'src/foo.podlite' ~= 'foo.podlite'
|
|
37
|
+
// './includes/x.podlite' ~= 'includes/x.podlite'
|
|
38
|
+
const normalizePath = (p) => p.replace(/\\/g, '/').replace(/^\.\//, '');
|
|
39
|
+
const isGlobPattern = (s) => /[*?[]/.test(s);
|
|
40
|
+
// Convert a glob pattern to an anchored RegExp.
|
|
41
|
+
// **/ → (?:.*/)? zero or more directory segments (lets **/foo match root-level foo)
|
|
42
|
+
// ** → .* any characters, crosses /
|
|
43
|
+
// * → [^/]* any characters within a single segment
|
|
44
|
+
// ? → [^/] single character within a segment
|
|
45
|
+
// Other regex meta characters are escaped.
|
|
46
|
+
const globRegexCache = new Map();
|
|
47
|
+
const globToRegex = (glob) => {
|
|
48
|
+
const cached = globRegexCache.get(glob);
|
|
49
|
+
if (cached)
|
|
50
|
+
return cached;
|
|
51
|
+
let re = '';
|
|
52
|
+
let i = 0;
|
|
53
|
+
while (i < glob.length) {
|
|
54
|
+
const c = glob[i];
|
|
55
|
+
const next = glob[i + 1];
|
|
56
|
+
if (c === '*' && next === '*' && glob[i + 2] === '/') {
|
|
57
|
+
re += '(?:.*/)?';
|
|
58
|
+
i += 3;
|
|
59
|
+
}
|
|
60
|
+
else if (c === '*' && next === '*') {
|
|
61
|
+
re += '.*';
|
|
62
|
+
i += 2;
|
|
63
|
+
}
|
|
64
|
+
else if (c === '*') {
|
|
65
|
+
re += '[^/]*';
|
|
66
|
+
i += 1;
|
|
67
|
+
}
|
|
68
|
+
else if (c === '?') {
|
|
69
|
+
re += '[^/]';
|
|
70
|
+
i += 1;
|
|
71
|
+
}
|
|
72
|
+
else if (/[\\^$.()+|{}[\]]/.test(c)) {
|
|
73
|
+
re += '\\' + c;
|
|
74
|
+
i += 1;
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
re += c;
|
|
78
|
+
i += 1;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
const compiled = new RegExp(`^${re}$`);
|
|
82
|
+
globRegexCache.set(glob, compiled);
|
|
83
|
+
return compiled;
|
|
84
|
+
};
|
|
85
|
+
const filePathMatches = (docFile, target) => {
|
|
86
|
+
const a = normalizePath(docFile);
|
|
87
|
+
const b = normalizePath(target);
|
|
88
|
+
if (isGlobPattern(b)) {
|
|
89
|
+
const rx = globToRegex(b);
|
|
90
|
+
if (rx.test(a))
|
|
91
|
+
return true;
|
|
92
|
+
// Suffix-tolerant match: allow any parent prefix (consistent with
|
|
93
|
+
// non-glob suffix matching, so 'src/00-foo/x.pod' matches '00-foo/x.pod').
|
|
94
|
+
const body = rx.source.slice(1, -1);
|
|
95
|
+
const rxSuffix = new RegExp(`^(?:.*/)${body}$`);
|
|
96
|
+
return rxSuffix.test(a);
|
|
97
|
+
}
|
|
98
|
+
return a === b || a.endsWith('/' + b) || b.endsWith('/' + a);
|
|
99
|
+
};
|
|
100
|
+
const getDocIDs = (doc) => {
|
|
101
|
+
const ids = [];
|
|
102
|
+
(0, index_1.getFromTree)(doc.node, 'NAME', 'TITLE').forEach(block => {
|
|
103
|
+
const conf = (0, index_1.makeAttrs)(block, {});
|
|
104
|
+
const title = (0, index_1.getTextContentFromNode)(block).trim();
|
|
105
|
+
if (conf.exists('id')) {
|
|
106
|
+
const id = conf.getFirstValue('id');
|
|
107
|
+
if (id)
|
|
108
|
+
ids.push(id);
|
|
109
|
+
}
|
|
110
|
+
ids.push(title);
|
|
111
|
+
});
|
|
112
|
+
return ids;
|
|
113
|
+
};
|
|
114
|
+
function getMapIDsBlocks(srcNode) {
|
|
115
|
+
const idsMap = new Map();
|
|
116
|
+
(0, index_1.getFromTree)(srcNode, { type: 'block' }).forEach(i => {
|
|
117
|
+
const id = (0, index_1.getNodeId)(i, {});
|
|
118
|
+
if (id)
|
|
119
|
+
idsMap.set(id, i);
|
|
120
|
+
});
|
|
121
|
+
return idsMap;
|
|
122
|
+
}
|
|
123
|
+
const runSelector = (selector, docs) => {
|
|
124
|
+
const parsed = (0, exports.parseSelector)(selector);
|
|
125
|
+
if (!parsed)
|
|
126
|
+
return [];
|
|
127
|
+
const { scheme, document, anchor, blockFilters } = parsed;
|
|
128
|
+
let matchedDocs = docs;
|
|
129
|
+
if (scheme === 'doc' && document) {
|
|
130
|
+
matchedDocs = docs.filter(doc => getDocIDs(doc).includes(document));
|
|
131
|
+
}
|
|
132
|
+
else if (scheme === 'file' && document) {
|
|
133
|
+
matchedDocs = docs.filter(doc => filePathMatches(doc.file, document));
|
|
134
|
+
}
|
|
135
|
+
else if (scheme && scheme !== 'doc' && scheme !== 'file') {
|
|
136
|
+
return [];
|
|
137
|
+
}
|
|
138
|
+
// Anchor takes precedence — single-block-by-id lookup
|
|
139
|
+
if (anchor) {
|
|
140
|
+
const collectedBlocks = [];
|
|
141
|
+
for (const d of matchedDocs) {
|
|
142
|
+
const idsMap = getMapIDsBlocks(d.node);
|
|
143
|
+
const block = idsMap.get(anchor);
|
|
144
|
+
if (block)
|
|
145
|
+
collectedBlocks.push(block);
|
|
146
|
+
}
|
|
147
|
+
return collectedBlocks;
|
|
148
|
+
}
|
|
149
|
+
// Block filters — extract blocks by name across matched docs
|
|
150
|
+
if (blockFilters.length > 0) {
|
|
151
|
+
const collectedBlocks = [];
|
|
152
|
+
for (const d of matchedDocs) {
|
|
153
|
+
for (const name of blockFilters) {
|
|
154
|
+
collectedBlocks.push(...(0, index_1.getFromTree)(d.node, name));
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
return collectedBlocks;
|
|
158
|
+
}
|
|
159
|
+
// No anchor, no filter — return whole docs
|
|
160
|
+
return matchedDocs.map(d => d.node);
|
|
161
|
+
};
|
|
162
|
+
exports.runSelector = runSelector;
|
|
163
|
+
//# sourceMappingURL=selectors.js.map
|
package/package.json
CHANGED