@podlite/publisher 0.0.51 → 0.0.53

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.
Files changed (55) hide show
  1. package/CHANGELOG.podlite +40 -0
  2. package/esm/catalogue.d.ts +17 -0
  3. package/esm/catalogue.js +156 -0
  4. package/esm/catalogue.js.map +1 -0
  5. package/esm/images-plugin.js +3 -1
  6. package/esm/images-plugin.js.map +1 -1
  7. package/esm/include-resolve-plugin.d.ts +16 -1
  8. package/esm/include-resolve-plugin.js +238 -50
  9. package/esm/include-resolve-plugin.js.map +1 -1
  10. package/esm/links-plugin.d.ts +9 -1
  11. package/esm/links-plugin.js +144 -58
  12. package/esm/links-plugin.js.map +1 -1
  13. package/esm/node-utils.d.ts +5 -3
  14. package/esm/node-utils.js +81 -24
  15. package/esm/node-utils.js.map +1 -1
  16. package/esm/pubdate-plugin.js +7 -5
  17. package/esm/pubdate-plugin.js.map +1 -1
  18. package/esm/react-plugin.js +9 -1
  19. package/esm/react-plugin.js.map +1 -1
  20. package/esm/reading.d.ts +5 -0
  21. package/esm/reading.js +34 -0
  22. package/esm/reading.js.map +1 -0
  23. package/esm/record.d.ts +2 -0
  24. package/esm/record.js +1 -0
  25. package/esm/record.js.map +1 -1
  26. package/esm/site-data-plugin.d.ts +1 -0
  27. package/esm/site-data-plugin.js +12 -1
  28. package/esm/site-data-plugin.js.map +1 -1
  29. package/esm/source.d.ts +19 -0
  30. package/esm/source.js +85 -0
  31. package/esm/source.js.map +1 -0
  32. package/esm/version.d.ts +1 -1
  33. package/esm/version.js +1 -1
  34. package/lib/catalogue.d.ts +17 -0
  35. package/lib/catalogue.js +197 -0
  36. package/lib/images-plugin.js +3 -1
  37. package/lib/include-resolve-plugin.d.ts +16 -1
  38. package/lib/include-resolve-plugin.js +273 -49
  39. package/lib/links-plugin.d.ts +9 -1
  40. package/lib/links-plugin.js +177 -56
  41. package/lib/node-utils.d.ts +5 -3
  42. package/lib/node-utils.js +96 -37
  43. package/lib/pubdate-plugin.js +7 -5
  44. package/lib/react-plugin.js +9 -1
  45. package/lib/reading.d.ts +5 -0
  46. package/lib/reading.js +38 -0
  47. package/lib/record.d.ts +2 -0
  48. package/lib/record.js +6 -1
  49. package/lib/site-data-plugin.d.ts +1 -0
  50. package/lib/site-data-plugin.js +13 -1
  51. package/lib/source.d.ts +19 -0
  52. package/lib/source.js +94 -0
  53. package/lib/version.d.ts +1 -1
  54. package/lib/version.js +1 -1
  55. package/package.json +4 -4
@@ -1,63 +1,287 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
2
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.includePasses = exports.IncludeError = void 0;
37
+ const path = __importStar(require("path"));
3
38
  const schema_1 = require("@podlite/schema");
4
- const shared_1 = require("./shared");
5
- const plugin = () => {
6
- const outCtx = {};
7
- const docsMap = new Map();
8
- const onExit = ctx => ({ ...ctx, ...outCtx });
9
- const processNode = (node, recs) => {
10
- const rules = {
11
- // TODO: remove 'Include' due to duplicate to 'include'
12
- Include: node => {
13
- const { content } = node;
14
- const selector = (0, schema_1.getTextContentFromNode)(content).trim();
15
- console.warn(`[include] start resolve selector: ${selector}`);
16
- if (selector) {
17
- // try to resolve selector
18
- const [block] = (0, shared_1.runSelector)(selector, recs);
19
- if (typeof block === 'object' && !('file' in block)) {
20
- const updated = { content: block };
21
- return { ...node, ...updated };
22
- }
23
- if (!block) {
24
- console.warn(`[plugin: resolve ] selector ${selector} not found`);
39
+ const podlite_1 = require("podlite");
40
+ const refresh_tocs_1 = require("podlite/lib/refresh-tocs");
41
+ const catalogue_1 = require("./catalogue");
42
+ const node_utils_1 = require("./node-utils");
43
+ const reading_1 = require("./reading");
44
+ const site_data_plugin_1 = require("./site-data-plugin");
45
+ const source_1 = require("./source");
46
+ /*
47
+ =begin pod :kind<export>
48
+
49
+ =head2 IncludeError
50
+
51
+ The build stops when an include cannot place what it names:
52
+
53
+ =item the source or the address is not found;
54
+ =item the selector cannot be read;
55
+ =item the scheme is not one the site reads;
56
+ =item a name answers to two documents;
57
+ =item a file includes itself, directly or through other files.
58
+
59
+ C<problems> holds one line for each, with the file and the line of the directive.
60
+
61
+ =end pod
62
+ */
63
+ class IncludeError extends Error {
64
+ problems;
65
+ constructor(problems) {
66
+ super(`an include is not placed:\n${problems.map(line => ` ${line}`).join('\n')}`);
67
+ this.name = 'IncludeError';
68
+ this.problems = problems;
69
+ }
70
+ }
71
+ exports.IncludeError = IncludeError;
72
+ // lose what the include names, though the assembler reports them as warnings
73
+ const stopping = new Set(['unsupported-scheme', 'unparsed-selector', 'cycle', 'ambiguous']);
74
+ const shown = (file) => (path.isAbsolute(file) ? path.relative(process.cwd(), file) : file);
75
+ const isNode = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
76
+ const nodesOf = (tree, ...queries) => (0, schema_1.getFromTree)(tree, ...queries).flatMap(node => (isNode(node) ? [node] : []));
77
+ const isToc = (node) => node.type === 'block' && (node.name === 'toc' || node.name === 'Toc');
78
+ const hasToc = (tree) => nodesOf(tree, 'toc', 'Toc').some(isToc);
79
+ // the format a table of contents of the record is built again from; none for Markdown
80
+ const writtenFormat = (source) => {
81
+ const type = (0, node_utils_1.getParserTypeforFile)(source.file, source.mime);
82
+ return type === node_utils_1.PARSER_TYPES.MARKDOWN ? undefined : type === node_utils_1.PARSER_TYPES.PODLITE ? 'podlite' : 'default';
83
+ };
84
+ const passes = (options) => {
85
+ const late = new Set([site_data_plugin_1.SITE_DATA_DOCUMENT, ...(options.late ?? [])]);
86
+ let waiting = false;
87
+ const catalogue = (0, catalogue_1.catalogueOf)(options.catalogue ?? [], {
88
+ bounds: options.bounds,
89
+ parse: (text, file) => (0, reading_1.readRecordText)(text, file),
90
+ waits: name => waiting && late.has(name),
91
+ });
92
+ const parser = (0, podlite_1.podlite)({ importPlugins: true });
93
+ // the file a directive was written in, by the place a problem shares with it
94
+ const fileAt = new WeakMap();
95
+ const describe = (problem) => {
96
+ const steps = problem.chain.map(step => {
97
+ const file = (step.location && fileAt.get(step.location)) || step.file;
98
+ return `${shown(file)}${step.location ? `:${step.location.start.line}` : ''}`;
99
+ });
100
+ const at = steps.pop() ?? '<document>';
101
+ return `${at}: ${problem.message}${steps.length ? ` (included through ${steps.reverse().join(', ')})` : ''}`;
102
+ };
103
+ const stampOf = (where) => where.via ? { file: where.file, via: where.via } : { file: where.file };
104
+ // The file each node was written in goes onto the node itself, where a copy
105
+ // keeps it; the assembler's own table is lost on the first copy a plugin makes.
106
+ const stamp = (tree, origin) => {
107
+ const visit = (node) => {
108
+ if (Array.isArray(node))
109
+ return node.forEach(visit);
110
+ if (!isNode(node))
111
+ return;
112
+ const where = origin.get(node);
113
+ if (where && !node[source_1.nodeOriginKey])
114
+ node[source_1.nodeOriginKey] = stampOf(where);
115
+ const file = node[source_1.nodeOriginKey]?.file;
116
+ if (file && isNode(node.location))
117
+ fileAt.set(node.location, file);
118
+ visit(node.content);
119
+ };
120
+ visit(tree);
121
+ };
122
+ // what the nodes say of themselves is told to the assembler, so that a block an
123
+ // earlier pass brought is not taken for one of the record
124
+ const seed = (tree, origin) => {
125
+ const visit = (node) => {
126
+ if (Array.isArray(node))
127
+ return node.forEach(visit);
128
+ if (!isNode(node))
129
+ return;
130
+ const mark = node[source_1.nodeOriginKey];
131
+ if (mark)
132
+ origin.set(node, { file: mark.file, text: '', ...(mark.via ? { via: mark.via } : {}) });
133
+ visit(node.content);
134
+ };
135
+ visit(tree);
136
+ };
137
+ const run = (pass) => {
138
+ const outCtx = {};
139
+ const onExit = ctx => ({ ...ctx, ...outCtx });
140
+ const onProcess = (recs) => {
141
+ if (!options.catalogue && pass !== 'last') {
142
+ console.warn('[plugin: resolve ] no catalogue of the site is given; an include is looked for among the records at hand');
143
+ }
144
+ // a document a plugin made earlier in the chain is a source too
145
+ catalogue.add(recs);
146
+ waiting = pass === 'first';
147
+ const problems = [];
148
+ // directives already said why they are left in place
149
+ const told = new WeakSet();
150
+ const fail = (problem) => {
151
+ const at = problem.chain[problem.chain.length - 1]?.location;
152
+ if (at)
153
+ told.add(at);
154
+ problems.push(describe(problem));
155
+ };
156
+ const warn = (problem) => {
157
+ if (stopping.has(problem.kind))
158
+ fail(problem);
159
+ else
160
+ console.warn(`[plugin: resolve ] ${describe(problem)}`);
161
+ };
162
+ // a document placed last is placed as it is: an include in it would need a third pass
163
+ const lateFiles = new Set();
164
+ if (pass !== 'first') {
165
+ for (const name of late) {
166
+ for (const id of catalogue.named(name)) {
167
+ lateFiles.add(id);
168
+ const text = catalogue.textOf(id);
169
+ if (text !== undefined && nodesOf((0, reading_1.readRecordText)(text, id), 'include').length) {
170
+ problems.push(`${shown(id)}: ${name} is placed after the other plugins, so it may not hold an include`);
171
+ }
25
172
  }
26
173
  }
174
+ if (problems.length)
175
+ throw new IncludeError(problems);
176
+ }
177
+ // one field of a record, read from one file
178
+ const assemble = (tree, record) => {
179
+ if (typeof tree !== 'object' || tree === null)
180
+ return tree;
181
+ // the tables of contents are built again in the passes that place the record's
182
+ // own text; the last one only adds documents plugins made
183
+ if (!nodesOf(tree, 'include').length && (pass === 'last' || !hasToc(tree)))
184
+ return tree;
185
+ const self = catalogue.idOf(record);
186
+ const source = (0, source_1.recordOrigin)(record);
187
+ const file = self ?? (0, catalogue_1.idOfFile)(record.file);
188
+ const origin = new WeakMap();
189
+ seed(tree, origin);
190
+ let node = (0, podlite_1.assembleIncludes)(tree, {
191
+ sources: catalogue.sources,
192
+ context: path.dirname(file),
193
+ parse: reading_1.readRecordText,
194
+ file,
195
+ ...(source ? { text: source.text } : {}),
196
+ self,
197
+ origin,
198
+ reportCycles: true,
199
+ onError: fail,
200
+ onWarning: warn,
201
+ });
202
+ // the record's own tables of contents reach what the includes brought
203
+ const format = source && writtenFormat(source);
204
+ if (source && format && hasToc(node)) {
205
+ node = (0, refresh_tocs_1.refreshTocs)(node, (0, podlite_1.writtenTree)(parser, source.text, format), file, origin);
206
+ }
207
+ stamp(node, origin);
27
208
  return node;
28
- },
29
- include: node => {
30
- const { content } = node;
31
- const selector = (0, schema_1.getTextContentFromNode)(content).trim();
32
- console.warn(`[include] start resolve selector: ${selector}`);
33
- if (selector) {
34
- const result = (0, shared_1.runSelector)(selector, recs);
35
- const blocks = [];
36
- for (const item of result) {
37
- if (typeof item === 'object' && item !== null && !('file' in item)) {
38
- blocks.push(item);
209
+ };
210
+ // two records of one reading share their tree
211
+ const trees = new WeakMap();
212
+ const assembleTree = (record) => {
213
+ const known = trees.get(record.node);
214
+ if (known)
215
+ return known;
216
+ const made = assemble(record.node, record);
217
+ trees.set(record.node, made);
218
+ return made;
219
+ };
220
+ // a template is a record of the chain and is assembled as one; a template a record
221
+ // carries is left as it is, as the plugins of images and links leave it
222
+ const out = recs.map(record => {
223
+ const node = assembleTree(record);
224
+ const description = assemble(record.description, record);
225
+ const header = assemble(record.header, record);
226
+ const footer = assemble(record.footer, record);
227
+ const same = node === record.node &&
228
+ description === record.description &&
229
+ header === record.header &&
230
+ footer === record.footer;
231
+ return same ? record : { ...record, node, description, header, footer };
232
+ });
233
+ if (pass !== 'first') {
234
+ for (const record of out) {
235
+ for (const tree of [record.node, record.description, record.header, record.footer]) {
236
+ for (const left of nodesOf(tree, 'include')) {
237
+ if (left.location && told.has(left.location))
238
+ continue;
239
+ const file = left[source_1.nodeOriginKey]?.file ?? record.file;
240
+ const line = left.location ? `:${left.location.start.line}` : '';
241
+ problems.push(`${shown(file)}${line}: the include is left in place`);
242
+ }
243
+ // a picture or a link placed now comes after the plugins that make them work
244
+ for (const node of nodesOf(tree, () => true)) {
245
+ const from = node[source_1.nodeOriginKey]?.file;
246
+ if (!from || !lateFiles.has(from))
247
+ continue;
248
+ const what = node.type === 'image' ? 'a picture' : node.type === 'fcode' && node.name === 'L' ? 'a link' : '';
249
+ if (what)
250
+ problems.push(`${shown(from)}: ${what} is placed after the plugins that handle it`);
39
251
  }
40
252
  }
41
- if (blocks.length > 0) {
42
- return { ...node, content: blocks };
43
- }
44
- console.warn(`[plugin: resolve ] selector ${selector} not found`);
45
253
  }
46
- return node;
47
- },
254
+ }
255
+ if (problems.length)
256
+ throw new IncludeError([...new Set(problems)]);
257
+ return out;
48
258
  };
49
- return (0, schema_1.makeInterator)(rules)(node, {});
50
- };
51
- const onProcess = (recs) => {
52
- // convert all doc: links to file:: links
53
- const docsWithIncludesResolves = recs.map(item => {
54
- const node = processNode(item.node, recs);
55
- // const node = item.node
56
- return { ...item, node };
57
- });
58
- return docsWithIncludesResolves;
259
+ return [onProcess, onExit];
59
260
  };
60
- return [onProcess, onExit];
261
+ return { run, documents: (name) => catalogue.named(name) };
262
+ };
263
+ /*
264
+ =begin pod :kind<export>
265
+
266
+ =head2 includePasses
267
+
268
+ Gives the include plugin as two passes over one catalogue. C<first> goes before the
269
+ plugins of images, links and React and places all it can; an include of a
270
+ document a plugin makes later in the chain (the site data, and the names in
271
+ C<late>) waits there. C<last> goes after those plugins and places only such
272
+ documents. An include left after it stops the build. C<documents> gives the files
273
+ of the documents that answer to a name, for the links plugin to find a C<doc:>
274
+ link by the same names.
275
+
276
+ =end pod
277
+ */
278
+ const includePasses = (options = {}) => {
279
+ const { run, documents } = passes(options);
280
+ return { first: run('first'), last: run('last'), documents };
61
281
  };
282
+ exports.includePasses = includePasses;
283
+ // One pass over the records: nothing waits, and an include that does not place
284
+ // what it names stops the build.
285
+ const plugin = (options = {}) => passes(options).run('only');
62
286
  exports.default = plugin;
63
287
  //# sourceMappingURL=include-resolve-plugin.js.map
@@ -1,3 +1,11 @@
1
1
  import { PodliteWebPlugin } from './plugins';
2
- declare const plugin: () => PodliteWebPlugin;
2
+ export type LinksOptions = {
3
+ documents?: (name: string) => string[];
4
+ home?: string;
5
+ };
6
+ export declare class LinkError extends Error {
7
+ readonly problems: string[];
8
+ constructor(problems: string[]);
9
+ }
10
+ declare const plugin: (options?: LinksOptions) => PodliteWebPlugin;
3
11
  export default plugin;
@@ -1,86 +1,207 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
2
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.LinkError = void 0;
37
+ const path = __importStar(require("path"));
3
38
  const schema_1 = require("@podlite/schema");
4
39
  const node_utils_1 = require("./node-utils");
5
- const plugin = () => {
40
+ const source_1 = require("./source");
41
+ /*
42
+ =begin pod :kind<export>
43
+
44
+ =head2 LinkError
45
+
46
+ A C<doc:> link whose name no published document answers to, or that two
47
+ documents answer to, stops the build. C<problems> holds one line for each.
48
+
49
+ =end pod
50
+ */
51
+ class LinkError extends Error {
52
+ problems;
53
+ constructor(problems) {
54
+ super(`a link does not resolve:\n${problems.map(line => ` ${line}`).join('\n')}`);
55
+ this.name = 'LinkError';
56
+ this.problems = problems;
57
+ }
58
+ }
59
+ exports.LinkError = LinkError;
60
+ const isNode = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
61
+ const shown = (file) => (path.isAbsolute(file) ? path.relative(process.cwd(), file) : file);
62
+ // the file a record was read from, or cut out of
63
+ const fileOf = (record) => path.resolve((0, source_1.recordOrigin)(record)?.file ?? record.file);
64
+ const plugin = (options = {}) => {
6
65
  const outCtx = {};
7
- const docsMap = new Map();
8
66
  const onExit = ctx => ({ ...ctx, ...outCtx });
9
- const processNode = (node, srcfile) => {
10
- const rules = {
11
- 'L<>': node => {
12
- const { content, meta } = node;
13
- const link = meta ? meta : (0, schema_1.getTextContentFromNode)(content);
14
- const r = link.match(/doc:\s*(?<path>(.+))\s*$/);
15
- if (r?.groups?.path) {
16
- const docLink = r.groups.path;
17
- const { isRemote } = (0, node_utils_1.getPathToOpen)(docLink, srcfile);
18
- if (isRemote) {
19
- return node;
20
- }
21
- let title;
22
- let url;
23
- if (!docsMap.has(docLink)) {
24
- console.error(`doc: ${docLink} not found`);
25
- title = `doc: ${docLink} not found`;
26
- url = `doc: ${docLink} not found`;
27
- }
28
- else {
29
- const { title: title1, url: url1 } = docsMap.get(docLink);
30
- title = title1;
31
- url = url1;
32
- }
33
- const newContent = {
34
- type: 'text',
35
- value: `${title}`,
36
- };
37
- const updated = meta ? { meta: url } : { content: newContent, meta: url };
38
- return { ...node, ...updated };
39
- }
40
- return node;
41
- },
42
- };
43
- return (0, schema_1.makeInterator)(rules)(node, {});
44
- };
45
67
  const onProcess = (recs) => {
46
- // const collectlinksMap = makeLinksMap(recs)
47
- // collects docsMap map doc: to file
48
- for (const item of recs) {
49
- // get =TITLE
68
+ // the records that answer to each name, with the text of the block that names them
69
+ const named = new Map();
70
+ for (const record of recs) {
50
71
  const handler = node => {
72
+ // a name an include brought belongs to the document it was written in
73
+ const from = (0, source_1.nodeOrigin)(node)?.file;
74
+ if (from !== undefined && path.resolve(from) !== fileOf(record))
75
+ return;
51
76
  const conf = (0, schema_1.makeAttrs)(node, {});
52
77
  const title = (0, schema_1.getTextContentFromNode)(node).trim();
53
- if (conf.exists('id')) {
54
- const id = conf.getFirstValue('id');
55
- if (id) {
56
- docsMap.set(id, { file: item.file, title, url: item.publishUrl });
57
- }
78
+ const id = conf.exists('id') ? conf.getFirstValue('id') : undefined;
79
+ for (const name of new Set([id, title].filter(Boolean))) {
80
+ named.set(name, [...(named.get(name) ?? []), { record, title }]);
58
81
  }
59
- docsMap.set(title, { file: item.file, title, url: item.publishUrl });
60
82
  };
61
- (0, schema_1.makeInterator)({
62
- NAME: handler,
63
- TITLE: handler,
64
- })(item.node, {});
83
+ (0, schema_1.makeInterator)({ NAME: handler, TITLE: handler })(record.node, {});
65
84
  }
85
+ const problems = [];
86
+ // the address a page gives a section, by the rule the page is drawn with: empty
87
+ // for the block that names the document, which is the page itself
88
+ const addressIn = (record, section) => {
89
+ const bindings = (0, schema_1.buildBindingIndex)(record.node);
90
+ const bound = (0, schema_1.bindTarget)(section, bindings);
91
+ if (bound.found && isNode(bound.node) && (bound.node.name === 'NAME' || bound.node.name === 'TITLE'))
92
+ return '';
93
+ return (0, schema_1.sameDocTarget)(`#${section}`, { __bindings: bindings, __anchors: (0, schema_1.indexAnchors)(record.node) });
94
+ };
95
+ const home = options.home === undefined ? undefined : path.resolve(options.home);
96
+ const addressOf = (record) => record.publishUrl || (home !== undefined && path.resolve(record.file) === home ? '/' : '');
97
+ // the title and the address a name links to: of one document, at the place a
98
+ // search engine is told to index when it is published at more than one
99
+ const resolve = (name, section, at) => {
100
+ // a link has no place of its own; `at` is where the paragraph holding it starts
101
+ const written = section === undefined ? name : `${name}#${section}`;
102
+ const answering = (named.get(name) ?? []).filter(({ record }) => Boolean(addressOf(record)));
103
+ const files = options.documents
104
+ ? options.documents(name).map(file => path.resolve(file))
105
+ : [...new Set(answering.map(({ record }) => fileOf(record)))];
106
+ if (files.length > 1) {
107
+ problems.push(`${at}: a link in the text from this line, doc:${written}: more than one document is named ${name}: ${files
108
+ .map(shown)
109
+ .join(', ')}`);
110
+ return undefined;
111
+ }
112
+ const placed = answering.filter(({ record }) => fileOf(record) === files[0]);
113
+ if (!placed.length) {
114
+ problems.push(`${at}: a link in the text from this line, doc:${written}: no published document is named ${name}`);
115
+ return undefined;
116
+ }
117
+ const indexed = placed.filter(({ record }) => (0, node_utils_1.isIndexed)(record));
118
+ const { record, title } = (indexed.length ? indexed : placed)[(indexed.length ? indexed : placed).length - 1];
119
+ const url = addressOf(record);
120
+ if (section === undefined)
121
+ return { title, url };
122
+ // a section is the block the page gives that address to, by the rule the page is drawn with
123
+ const anchor = addressIn(record, section);
124
+ if (anchor === undefined) {
125
+ problems.push(`${at}: a link in the text from this line, doc:${written}: ${name} has no section ${section}`);
126
+ return undefined;
127
+ }
128
+ return { title, url: `${url}${anchor}` };
129
+ };
130
+ const processNode = (node, srcfile, page) => {
131
+ // the line the paragraph or block holding each link starts at
132
+ const lines = new WeakMap();
133
+ const mark = (tree, line) => {
134
+ if (Array.isArray(tree))
135
+ return tree.forEach(child => mark(child, line));
136
+ if (!isNode(tree))
137
+ return;
138
+ const here = (tree.type === 'block' || tree.type === 'para') && tree.location ? tree.location.start.line : line;
139
+ if (tree.type === 'fcode' && tree.name === 'L' && here !== undefined)
140
+ lines.set(tree, here);
141
+ mark(tree.content, here);
142
+ };
143
+ mark(node);
144
+ const rules = {
145
+ 'L<>': node => {
146
+ const { content, meta } = node;
147
+ const link = meta ? meta : (0, schema_1.getTextContentFromNode)(content);
148
+ const r = link.match(/doc:\s*(?<path>(.+))\s*$/);
149
+ if (r?.groups?.path) {
150
+ const docLink = r.groups.path;
151
+ const from = (0, source_1.nodeOrigin)(node)?.file ?? srcfile;
152
+ const { isRemote } = (0, node_utils_1.getPathToOpen)(docLink, from);
153
+ if (isRemote) {
154
+ return node;
155
+ }
156
+ const line = lines.get(node);
157
+ const at = `${shown(from)}${line !== undefined ? `:${line}` : ''}`;
158
+ // a section of the document is addressed after its name, of this page without one
159
+ const [name, ...section] = docLink.split('#');
160
+ const local = (written) => {
161
+ const anchor = addressIn(page, written);
162
+ if (anchor !== undefined)
163
+ return { title: written, url: anchor || '#' };
164
+ problems.push(`${at}: a link in the text from this line, doc:#${written}: the page has no section ${written}`);
165
+ return undefined;
166
+ };
167
+ const found = name === '' && section.length
168
+ ? local(section.join('#'))
169
+ : resolve(name, section.length ? section.join('#') : undefined, at);
170
+ if (!found)
171
+ return node;
172
+ const { url } = found;
173
+ const newContent = {
174
+ type: 'text',
175
+ value: `${found.title}`,
176
+ };
177
+ const updated = meta ? { meta: url } : { content: newContent, meta: url };
178
+ return { ...node, ...updated };
179
+ }
180
+ return node;
181
+ },
182
+ };
183
+ return (0, schema_1.makeInterator)(rules)(node, {});
184
+ };
66
185
  // convert all doc: links to file:: links
67
186
  const docToFileLinksConverted = recs.map(item => {
68
- const node = processNode(item.node, item.file);
187
+ const node = processNode(item.node, item.file, item);
69
188
  // process images inside description
70
189
  let extra = {};
71
190
  if (item.description) {
72
- extra.description = processNode(item.description, item.file);
191
+ extra.description = processNode(item.description, item.file, item);
73
192
  }
74
193
  // process file header and footer
75
194
  const { footer, header } = item;
76
195
  if (footer) {
77
- extra.footer = processNode(footer, item.file);
196
+ extra.footer = processNode(footer, item.file, item);
78
197
  }
79
198
  if (header) {
80
- extra.header = processNode(header, item.file);
199
+ extra.header = processNode(header, item.file, item);
81
200
  }
82
201
  return { ...item, node, ...extra };
83
202
  });
203
+ if (problems.length)
204
+ throw new LinkError([...new Set(problems)]);
84
205
  return (0, node_utils_1.convertFileLinksToUrl)(docToFileLinksConverted);
85
206
  };
86
207
  return [onProcess, onExit];
@@ -4,12 +4,13 @@ export declare const getPathToOpen: (filepath: any, parentDocPath: any) => {
4
4
  isRemote: boolean;
5
5
  path: any;
6
6
  };
7
+ export declare const isIndexed: (record: publishRecord) => boolean;
7
8
  export declare const makeLinksMap: (records: publishRecord[]) => {
8
- [link: string]: string;
9
+ [link: string]: string | null;
9
10
  };
10
11
  export declare const convertFileLinksToUrl: (records: publishRecord[], additinalMap?: {}) => publishRecord[];
11
12
  export declare function parseFiles(path: string): pubRecord[];
12
- declare const PARSER_TYPES: {
13
+ export declare const PARSER_TYPES: {
13
14
  MARKDOWN: "markdown";
14
15
  PODLITE: "podlite";
15
16
  DEFAULT: "default";
@@ -22,8 +23,9 @@ declare const MIME_TYPES_EXTENSIONS: {
22
23
  'audio/mpeg': readonly [".mp3"];
23
24
  };
24
25
  type PartserTypes = typeof PARSER_TYPES[keyof typeof PARSER_TYPES];
25
- type MimeTypes = keyof typeof MIME_TYPES_EXTENSIONS;
26
+ export type MimeTypes = keyof typeof MIME_TYPES_EXTENSIONS;
26
27
  export declare function getParserTypeforFile(filePath: string, mime?: MimeTypes): PartserTypes;
28
+ export declare function parseText(filePath: string, text: string, mime?: MimeTypes): RootBlock;
27
29
  export declare function parseFile(filePath: string, fileContent?: string, mime?: MimeTypes): RootBlock;
28
30
  export declare function getDocumentAttributes(node: PodliteDocument, filePath?: string): {
29
31
  header: any;