@podlite/schema 0.0.80 → 0.0.81
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 +44 -0
- package/esm/ast-helpers.d.ts +8 -1
- package/esm/ast-helpers.js +172 -47
- package/esm/ast-helpers.js.map +1 -1
- package/esm/exportAny.d.ts +1 -0
- package/esm/exportAny.js +11 -0
- package/esm/exportAny.js.map +1 -1
- package/esm/exportHtml.js +58 -1
- package/esm/exportHtml.js.map +1 -1
- package/esm/exportMarkdown.js +604 -137
- package/esm/exportMarkdown.js.map +1 -1
- package/esm/helpers/code-config.d.ts +1 -0
- package/esm/helpers/code-config.js +4 -0
- package/esm/helpers/code-config.js.map +1 -0
- package/esm/helpers/corePlugins.js +4 -0
- package/esm/helpers/corePlugins.js.map +1 -1
- package/esm/helpers/handlers.js +8 -4
- package/esm/helpers/handlers.js.map +1 -1
- package/esm/index.d.ts +2 -0
- package/esm/index.js +1 -0
- package/esm/index.js.map +1 -1
- package/esm/markdown-heading-text.d.ts +3 -0
- package/esm/markdown-heading-text.js +36 -0
- package/esm/markdown-heading-text.js.map +1 -0
- package/esm/plugin-formatting-codes.js +1 -3
- package/esm/plugin-formatting-codes.js.map +1 -1
- package/esm/plugin-tables.d.ts +4 -1
- package/esm/plugin-tables.js +99 -26
- package/esm/plugin-tables.js.map +1 -1
- package/esm/test-display.js +8 -1
- package/esm/test-display.js.map +1 -1
- package/esm/title.d.ts +13 -0
- package/esm/title.js +22 -0
- package/esm/title.js.map +1 -0
- package/esm/types.d.ts +9 -4
- package/esm/version.d.ts +1 -1
- package/esm/version.js +1 -1
- package/esm/writerMarkdown.d.ts +9 -0
- package/esm/writerMarkdown.js +103 -1
- package/esm/writerMarkdown.js.map +1 -1
- package/lib/ast-helpers.d.ts +8 -1
- package/lib/ast-helpers.js +176 -48
- package/lib/exportAny.d.ts +1 -0
- package/lib/exportAny.js +11 -0
- package/lib/exportHtml.js +58 -1
- package/lib/exportMarkdown.js +607 -140
- package/lib/helpers/code-config.d.ts +1 -0
- package/lib/helpers/code-config.js +8 -0
- package/lib/helpers/corePlugins.js +4 -0
- package/lib/helpers/handlers.js +8 -4
- package/lib/index.d.ts +2 -0
- package/lib/index.js +5 -2
- package/lib/markdown-heading-text.d.ts +3 -0
- package/lib/markdown-heading-text.js +40 -0
- package/lib/plugin-formatting-codes.js +2 -4
- package/lib/plugin-tables.d.ts +4 -1
- package/lib/plugin-tables.js +99 -26
- package/lib/test-display.js +8 -1
- package/lib/title.d.ts +13 -0
- package/lib/title.js +27 -0
- package/lib/types.d.ts +9 -4
- package/lib/version.d.ts +1 -1
- package/lib/version.js +1 -1
- package/lib/writerMarkdown.d.ts +9 -0
- package/lib/writerMarkdown.js +103 -1
- package/package.json +3 -2
- package/schema/AstTree.json +246 -0
- package/schema/PodliteDocument.json +246 -0
package/esm/exportMarkdown.js
CHANGED
|
@@ -1,37 +1,62 @@
|
|
|
1
1
|
import toAny from './exportAny.js';
|
|
2
2
|
import { isCovered } from './guard.js';
|
|
3
|
-
import { subUse, wrapContent, emptyContent, content, setFn, handleNested, maskText, expandTabs, collectText, } from './helpers/handlers.js';
|
|
3
|
+
import { subUse, wrapContent, emptyContent, content, setFn, handleNested as nestedLevels, maskText, expandTabs, collectText, } from './helpers/handlers.js';
|
|
4
4
|
import makeAttrs, { codeConfigWithDefaults } from './helpers/config.js';
|
|
5
5
|
import { mergeConfigSettings } from './helpers/configPropagation.js';
|
|
6
6
|
import { applyImageBase } from './image-base.js';
|
|
7
7
|
import writerMarkdown from './writerMarkdown.js';
|
|
8
8
|
import clean_plugin from './plugin-clean-location.js';
|
|
9
9
|
import { expandAlias } from './helpers/alias.js';
|
|
10
|
-
import { linkTarget, readerAnchors, sameDocTarget, writtenValue } from './ast-helpers.js';
|
|
10
|
+
import { decodeEntities, bindTarget, getExplicitNodeId, headingsWithCodeLinks, nodesOfTree, linkTarget, readerAnchors, sameDocTarget, toMarkdownFragment, writtenValue, } from './ast-helpers.js';
|
|
11
11
|
import { quoteAttribute } from './helpers/html-attr.js';
|
|
12
12
|
import { readLinkConfig } from './helpers/link-config.js';
|
|
13
13
|
import { testCaption, longestBacktickRun } from './test-display.js';
|
|
14
|
+
import { titleParts, isTitleBlock } from './title.js';
|
|
15
|
+
import { headingTextOf } from './markdown-heading-text.js';
|
|
14
16
|
// A markdown reader builds the anchor out of the heading itself, by its own rules,
|
|
15
17
|
// so a link written here has to match what that reader will produce.
|
|
16
18
|
// Built once per document and mode: a hidden block renders with a copy of the context,
|
|
17
19
|
// and a cache kept on the context itself would be rebuilt for every one of them.
|
|
18
20
|
const readerAnchorsCache = new WeakMap();
|
|
21
|
+
// The walk that learns the lines notes each heading where the page has it, and fills in
|
|
22
|
+
// its line when it is written; a title holds blocks written before its own line.
|
|
23
|
+
const noteHeading = (ctx, node) => {
|
|
24
|
+
const lines = ctx?.__headingLines;
|
|
25
|
+
if (!lines)
|
|
26
|
+
return () => { };
|
|
27
|
+
const at = lines.push({ node, line: null }) - 1;
|
|
28
|
+
return line => {
|
|
29
|
+
lines[at].line = line;
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
// With the texts of the headings read from the lines the output wrote, the addresses are
|
|
33
|
+
// the reader's own; the walk that learns those lines builds them from the tree.
|
|
19
34
|
const markdownAnchors = ctx => {
|
|
20
35
|
const index = ctx?.__anchors;
|
|
21
36
|
if (!index)
|
|
22
37
|
return undefined;
|
|
23
38
|
const mode = ctx.renderMode === 'draft' ? 'draft' : 'production';
|
|
24
|
-
|
|
39
|
+
const key = ctx.__readerTexts || index;
|
|
40
|
+
let byMode = readerAnchorsCache.get(key);
|
|
25
41
|
if (!byMode)
|
|
26
|
-
readerAnchorsCache.set(
|
|
42
|
+
readerAnchorsCache.set(key, (byMode = new Map()));
|
|
27
43
|
if (!byMode.has(mode))
|
|
28
|
-
byMode.set(mode, readerAnchors(index, mode));
|
|
44
|
+
byMode.set(mode, readerAnchors(index, mode, ctx.__readerTexts, ctx.__codeLinkHeadings));
|
|
29
45
|
return byMode.get(mode);
|
|
30
46
|
};
|
|
47
|
+
// a reader takes `©` in an address, a title or the alternative text of a picture for
|
|
48
|
+
// the sign; `&` keeps it as written for every reader, where some do not take off a backslash
|
|
49
|
+
const characterReference = /&(?=#?[A-Za-z0-9]+;)/g;
|
|
50
|
+
const keepReferences = (text) => text.replace(characterReference, '&');
|
|
51
|
+
// the title of a link or a picture, after its address in quotes
|
|
52
|
+
const quotedTitle = (title, inCode = false) => {
|
|
53
|
+
const text = inCode ? title : keepReferences(title.replace(/\\/g, '\\\\'));
|
|
54
|
+
return ` "${text.replace(/"/g, '\\"')}"`;
|
|
55
|
+
};
|
|
31
56
|
// a title written on a link inside hidden content is hidden with it
|
|
32
|
-
const linkTitle = (config, conceal = text => text) => {
|
|
57
|
+
const linkTitle = (config, conceal = text => text, inCode = false) => {
|
|
33
58
|
const { title } = readLinkConfig(config);
|
|
34
|
-
return title === undefined ? '' :
|
|
59
|
+
return title === undefined ? '' : quotedTitle(conceal(title), inCode);
|
|
35
60
|
};
|
|
36
61
|
// Markdown has no form for a link whose target the author never wrote — an empty
|
|
37
62
|
// address there claims the current document — so the text is written on its own.
|
|
@@ -40,18 +65,29 @@ const linkTitle = (config, conceal = text => text) => {
|
|
|
40
65
|
// markdown keeps for exactly that. An angle of its own is percent-encoded rather
|
|
41
66
|
// than dropped, so the address survives the round trip — no source text reaches
|
|
42
67
|
// here holding one today, since the parser ends the code on it.
|
|
43
|
-
|
|
44
|
-
const
|
|
68
|
+
// Inside code a reader shows a backslash, so there the address stays as it is.
|
|
69
|
+
const markdownAddress = (address, inCode = false) => {
|
|
70
|
+
const kept = inCode ? address : keepReferences(address.replace(/\\/g, '\\\\'));
|
|
71
|
+
return /[()\s]/.test(kept) ? `<${kept.replace(/</g, '%3C').replace(/>/g, '%3E')}>` : kept;
|
|
72
|
+
};
|
|
73
|
+
const linkWrap = (node, ctx, inCode = false) => {
|
|
45
74
|
const target = linkTarget(node);
|
|
46
75
|
if (target === undefined)
|
|
47
76
|
return wrapContent('', '');
|
|
48
77
|
const resolved = sameDocTarget(target, ctx, markdownAnchors(ctx), node);
|
|
78
|
+
// a link to this document in the code of a heading line is read as text: the heading
|
|
79
|
+
// and the target of the link take named anchors, so the text stays as it is
|
|
80
|
+
if (inCode && ctx.__codeLinks && ctx.__headingNode && typeof target === 'string' && target.startsWith('#')) {
|
|
81
|
+
const bound = ctx.__bindings ? bindTarget(target.slice(1), ctx.__bindings) : undefined;
|
|
82
|
+
if (bound?.found)
|
|
83
|
+
ctx.__codeLinks.add(bound.node).add(ctx.__headingNode);
|
|
84
|
+
}
|
|
49
85
|
// Markdown has no anchor without an address either, so a refused link keeps its
|
|
50
86
|
// text and loses the brackets.
|
|
51
87
|
if (resolved === undefined)
|
|
52
88
|
return wrapContent('', '');
|
|
53
89
|
const address = String(resolved);
|
|
54
|
-
return wrapContent(`[`, `](${markdownAddress(address)}${linkTitle(codeConfigWithDefaults(node, ctx), text => covered(node, ctx, text))})`);
|
|
90
|
+
return wrapContent(`[`, `](${markdownAddress(address, inCode)}${linkTitle(codeConfigWithDefaults(node, ctx), text => covered(node, ctx, text), inCode)})`);
|
|
55
91
|
};
|
|
56
92
|
// A cell's own text is written whole. Trimming each fragment on its own eats the
|
|
57
93
|
// spaces that sit between them — between a word and a markup code, or around a
|
|
@@ -78,7 +114,7 @@ const writeSourceBlock = (writer, node, ctx) => {
|
|
|
78
114
|
const text = collectText(node.content);
|
|
79
115
|
const fence = '`'.repeat(Math.max(3, longestBacktickRun(text) + 1));
|
|
80
116
|
writer.writeRaw(`${fence}podlite\n`);
|
|
81
|
-
writer
|
|
117
|
+
writeSource(writer, isCovered(node, ctx) ? maskText(text) : text);
|
|
82
118
|
if (!text.endsWith('\n'))
|
|
83
119
|
writer.writeRaw('\n');
|
|
84
120
|
writer.writeRaw(`${fence}\n`);
|
|
@@ -87,12 +123,269 @@ const writeSourceBlock = (writer, node, ctx) => {
|
|
|
87
123
|
const covered = (node, ctx, text) => (isCovered(node, ctx) ? maskText(text) : text);
|
|
88
124
|
const inlineCode = (text) => {
|
|
89
125
|
const ticks = '`'.repeat(longestBacktickRun(text) + 1);
|
|
90
|
-
|
|
126
|
+
// a reader takes one space off each side of code that has a space at both ends, a line
|
|
127
|
+
// break counted as a space, so such code, as code at a backtick, gets one more on each side
|
|
128
|
+
const spaced = /^[ \n]/.test(text) && /[ \n]$/.test(text) && /[^ \n]/.test(text);
|
|
129
|
+
const pad = text.startsWith('`') || text.endsWith('`') || spaced ? ' ' : '';
|
|
91
130
|
return `${ticks}${pad}${text}${pad}${ticks}`;
|
|
92
131
|
};
|
|
132
|
+
// Inside code a reader takes < and & as written; a cell of a table written as HTML
|
|
133
|
+
// stands inside a frame, and a fence must not hold across a cell folded onto a line.
|
|
134
|
+
const within = (writer, depth, write) => {
|
|
135
|
+
writer[depth] = (writer[depth] || 0) + 1;
|
|
136
|
+
try {
|
|
137
|
+
write();
|
|
138
|
+
}
|
|
139
|
+
finally {
|
|
140
|
+
writer[depth]--;
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
// What `write` writes is kept aside and returned. The line the writer stands on is not
|
|
144
|
+
// touched: the caller writes the result as one piece, and the writer counts its line
|
|
145
|
+
// from that piece.
|
|
146
|
+
const writeAside = (writer, write) => {
|
|
147
|
+
const original = writer.output;
|
|
148
|
+
const { lastWritten, lineText, paraStart, trailingBreaks, pendingBreak } = writer;
|
|
149
|
+
const parts = [];
|
|
150
|
+
writer.output = (str) => parts.push(str);
|
|
151
|
+
// a blank line owed to the content outside is not written into the piece aside
|
|
152
|
+
if (pendingBreak)
|
|
153
|
+
writer.pendingBreak = false;
|
|
154
|
+
try {
|
|
155
|
+
write();
|
|
156
|
+
}
|
|
157
|
+
finally {
|
|
158
|
+
writer.output = original;
|
|
159
|
+
writer.lastWritten = lastWritten;
|
|
160
|
+
writer.lineText = lineText;
|
|
161
|
+
writer.paraStart = paraStart;
|
|
162
|
+
writer.trailingBreaks = trailingBreaks;
|
|
163
|
+
writer.pendingBreak = pendingBreak;
|
|
164
|
+
}
|
|
165
|
+
return parts.join('');
|
|
166
|
+
};
|
|
167
|
+
// The markdown writer keeps the text of its line, so the lines of a quote and of an
|
|
168
|
+
// item can be written aside and given their prefix when they are done.
|
|
169
|
+
const ownsLines = writer => typeof writer.lineText === 'string' && typeof writer.output === 'function';
|
|
170
|
+
// the content of a quote or an item starts on a line of its own
|
|
171
|
+
const startLine = writer => {
|
|
172
|
+
writer.lineText = '';
|
|
173
|
+
writer.lastWritten = '';
|
|
174
|
+
writer.paraStart = false;
|
|
175
|
+
writer.trailingBreaks = null;
|
|
176
|
+
writer.pendingBreak = false;
|
|
177
|
+
};
|
|
178
|
+
// A quote is written aside, then every line of it gets the sign of each level, a blank
|
|
179
|
+
// one too, so a fence, a list or a second paragraph stays inside. A quote opened in the
|
|
180
|
+
// middle of a line, after the term of a definition, starts a line of its own. One blank
|
|
181
|
+
// line outside the quote ends it.
|
|
182
|
+
const quote = (writer, levels, write) => {
|
|
183
|
+
if (writer.lineText !== '')
|
|
184
|
+
writer.writeRaw('\n');
|
|
185
|
+
const body = writeAside(writer, () => {
|
|
186
|
+
startLine(writer);
|
|
187
|
+
write();
|
|
188
|
+
});
|
|
189
|
+
const sign = '> '.repeat(levels);
|
|
190
|
+
const lines = body.replace(/\n+$/, '').split('\n');
|
|
191
|
+
writer.writeRaw(lines.map(line => (line === '' ? sign.trimEnd() : sign + line)).join('\n') + '\n\n');
|
|
192
|
+
};
|
|
193
|
+
// :nested(n) of a block: a quote of n levels around what the block writes
|
|
194
|
+
const handleNested = (handler, implicitLevel) => (writer, processor) => {
|
|
195
|
+
const plain = nestedLevels(handler, implicitLevel)(writer, processor);
|
|
196
|
+
const own = handler(writer, processor);
|
|
197
|
+
return (node, ctx, interator) => {
|
|
198
|
+
if (!ownsLines(writer))
|
|
199
|
+
return plain(node, ctx, interator);
|
|
200
|
+
const levels = Number(makeAttrs(node, ctx).getFirstValue('nested') || implicitLevel || 0);
|
|
201
|
+
if (!(levels > 0))
|
|
202
|
+
return own(node, ctx, interator);
|
|
203
|
+
quote(writer, levels, () => own(node, ctx, interator));
|
|
204
|
+
};
|
|
205
|
+
};
|
|
206
|
+
// a writer given by the caller may be a plain one, without a source of its own
|
|
207
|
+
const writeSource = (writer, text) => typeof writer.writeSource === 'function' ? writer.writeSource(text) : writer.writeRaw(text);
|
|
208
|
+
// Code in a line is written aside first: its fence is one backtick longer than any run
|
|
209
|
+
// inside, so a backtick of the text does not end the code early.
|
|
210
|
+
// A cell of a pipe table is divided at every bar, before code is read, unless an odd run of
|
|
211
|
+
// backslashes stands before the bar; the reader then takes one backslash off. So a bar of
|
|
212
|
+
// code gets a backslash of its own after an even run, and one more after an odd run: the
|
|
213
|
+
// reader shows the code as written after an even run and one backslash more after an odd one.
|
|
214
|
+
// The whole code is taken at once, as a backslash may come from another piece of it.
|
|
215
|
+
const keepBarsInCell = (code) => code.replace(/(\\*)\|/g, (match, run) => `${run}${run.length % 2 ? '\\' : ''}\\|`);
|
|
216
|
+
const codeSpan = (writer, processor) => (node, ctx, interator) => {
|
|
217
|
+
const code = writeAside(writer, () => within(writer, 'codeDepth', () => node.content && interator(node.content, ctx)));
|
|
218
|
+
writer.writeRaw(inlineCode(writer.cellDepth > 0 ? keepBarsInCell(code) : code));
|
|
219
|
+
};
|
|
220
|
+
const htmlText = (text) => text.replace(/&/g, '&').replace(/</g, '<');
|
|
221
|
+
// A cell of a table written as HTML holds Markdown: a blank line ends the block of
|
|
222
|
+
// HTML before it and another one before the closing tag, so a reader takes what
|
|
223
|
+
// lies between as Markdown. A cell with nothing to show closes on its own line.
|
|
224
|
+
const writeIsland = (writer, open, close, write) => {
|
|
225
|
+
if (typeof writer.output !== 'function') {
|
|
226
|
+
writer.writeRaw(`${open}\n\n`);
|
|
227
|
+
write();
|
|
228
|
+
writer.writeRaw(`\n\n${close}\n`);
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
// the content of a cell starts anew: a block at its start needs no blank line before it
|
|
232
|
+
const body = writeAside(writer, () => {
|
|
233
|
+
writer.trailingBreaks = null;
|
|
234
|
+
write();
|
|
235
|
+
}).replace(/\n+$/, '');
|
|
236
|
+
writer.writeRaw(body.trim() === '' ? `${open}${close}\n` : `${open}\n\n${body}\n\n${close}\n`);
|
|
237
|
+
};
|
|
238
|
+
// a fence longer than any run of backticks in the body, opened at the start of a line
|
|
239
|
+
const writeFenced = (writer, info, write) => {
|
|
240
|
+
if (typeof writer.output !== 'function') {
|
|
241
|
+
writer.writeRaw('```' + info + '\n');
|
|
242
|
+
within(writer, 'codeDepth', write);
|
|
243
|
+
if (!writer.lastWritten.endsWith('\n'))
|
|
244
|
+
writer.writeRaw('\n');
|
|
245
|
+
writer.writeRaw('```\n');
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
const atLineStart = /^$|\n$/.test(writer.lastWritten);
|
|
249
|
+
const body = writeAside(writer, () => within(writer, 'codeDepth', write));
|
|
250
|
+
const fence = '`'.repeat(Math.max(3, longestBacktickRun(body) + 1));
|
|
251
|
+
writer.writeRaw(`${atLineStart ? '' : '\n'}${fence}${info}\n`);
|
|
252
|
+
if (body !== '')
|
|
253
|
+
writer.writeRaw(body.endsWith('\n') ? body : `${body}\n`);
|
|
254
|
+
writer.writeRaw(`${fence}\n`);
|
|
255
|
+
};
|
|
256
|
+
// A body shown as written goes into a fence a reader keeps as written. A fence holds only
|
|
257
|
+
// on a line of its own and outside a cell folded onto a line; there the body is written
|
|
258
|
+
// as text, so its words stay in sight. Says whether a fence was written.
|
|
259
|
+
const isBodyText = child => typeof child === 'string' || child.type === 'text' || child.type === 'verbatim';
|
|
260
|
+
const bodyValue = child => (typeof child === 'string' ? child : child.value);
|
|
261
|
+
const writeBodyAsWritten = (writer, node, ctx, interator, language = '') => {
|
|
262
|
+
const children = node.content || [];
|
|
263
|
+
if (writer.cellDepth > 0) {
|
|
264
|
+
children.forEach(child => isBodyText(child)
|
|
265
|
+
? writeText(writer, typeof child === 'string' ? node : child, ctx, bodyValue(child))
|
|
266
|
+
: interator([child], ctx));
|
|
267
|
+
if (!writer.lastWritten.endsWith('\n'))
|
|
268
|
+
writer.writeRaw('\n');
|
|
269
|
+
writer.writeRaw('\n');
|
|
270
|
+
return false;
|
|
271
|
+
}
|
|
272
|
+
const masked = isCovered(node, ctx);
|
|
273
|
+
writeFenced(writer, language, () => children.forEach(child => isBodyText(child)
|
|
274
|
+
? writeSource(writer, masked ? maskText(bodyValue(child)) : bodyValue(child))
|
|
275
|
+
: interator([child], ctx)));
|
|
276
|
+
return true;
|
|
277
|
+
};
|
|
278
|
+
// What a block writes, ended by one blank line, so the paragraph after it stands
|
|
279
|
+
// on its own
|
|
280
|
+
const writeWithBoundary = (writer, write) => {
|
|
281
|
+
if (typeof writer.output !== 'function') {
|
|
282
|
+
write();
|
|
283
|
+
writer.writeRaw('\n');
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
const text = writeAside(writer, write);
|
|
287
|
+
if (text === '')
|
|
288
|
+
return;
|
|
289
|
+
writer.writeRaw(text.endsWith('\n\n') ? text : text.endsWith('\n') ? `${text}\n` : `${text}\n\n`);
|
|
290
|
+
};
|
|
291
|
+
// A block no rule knows holds a body nobody has read: text only. Its names never come
|
|
292
|
+
// from a rule, a semantic block or an =include, which is a directive.
|
|
293
|
+
const isBareBody = node => {
|
|
294
|
+
const name = node.name || '';
|
|
295
|
+
if (node.type !== 'block' || name === name.toUpperCase())
|
|
296
|
+
return false;
|
|
297
|
+
if (['root', 'caption', 'Html', 'include'].includes(name))
|
|
298
|
+
return false;
|
|
299
|
+
const children = node.content;
|
|
300
|
+
return Array.isArray(children) && children.length > 0 && children.every(isBodyText);
|
|
301
|
+
};
|
|
302
|
+
// the names the plugins give a picture and a block of Markdown, as an author may write them
|
|
303
|
+
const isPluginPicture = node => (node.name === 'picture' || node.name === 'Image') &&
|
|
304
|
+
node.sourceFormat !== 'markdown' &&
|
|
305
|
+
Array.isArray(node.content) &&
|
|
306
|
+
node.content[0]?.type === 'image';
|
|
307
|
+
const isPluginMarkdown = node => (node.name === 'markdown' || node.name === 'Markdown') &&
|
|
308
|
+
node.sourceFormat !== 'markdown' &&
|
|
309
|
+
node.content &&
|
|
310
|
+
!Array.isArray(node.content) &&
|
|
311
|
+
node.content.name === 'root';
|
|
312
|
+
// A block without a rule of its own is shown as written: a fence without a language,
|
|
313
|
+
// the body as the source has it, a diagram marked mermaid unless it is hidden. A
|
|
314
|
+
// picture from its plugin stands as a picture with its caption a paragraph after it;
|
|
315
|
+
// a block of Markdown read by its plugin keeps its own structure. Each ends with a
|
|
316
|
+
// blank line, so the paragraph after it does not join it.
|
|
317
|
+
const blockWithoutRule = (writer, node, ctx, interator) => {
|
|
318
|
+
if (isBareBody(node)) {
|
|
319
|
+
const body = node.content.map(bodyValue).join('');
|
|
320
|
+
if (body.trim() === '')
|
|
321
|
+
return true;
|
|
322
|
+
const diagram = (node.name === 'Mermaid' || node.name === 'Diagram') && !isCovered(node, ctx);
|
|
323
|
+
if (writeBodyAsWritten(writer, node, ctx, interator, diagram ? 'mermaid' : ''))
|
|
324
|
+
writer.writeRaw('\n');
|
|
325
|
+
return true;
|
|
326
|
+
}
|
|
327
|
+
if (isPluginPicture(node)) {
|
|
328
|
+
const [image, ...rest] = node.content;
|
|
329
|
+
writeWithBoundary(writer, () => {
|
|
330
|
+
interator([image], ctx);
|
|
331
|
+
writer.writeRaw('\n');
|
|
332
|
+
});
|
|
333
|
+
rest.forEach(part => part?.type === 'block' && part.name === 'caption'
|
|
334
|
+
? writeWithBoundary(writer, () => paragraph(writer, ctx, () => interator(part.content, ctx)))
|
|
335
|
+
: interator([part], ctx));
|
|
336
|
+
return true;
|
|
337
|
+
}
|
|
338
|
+
if (isPluginMarkdown(node)) {
|
|
339
|
+
writeWithBoundary(writer, () => interator(node.content, ctx));
|
|
340
|
+
return true;
|
|
341
|
+
}
|
|
342
|
+
return false;
|
|
343
|
+
};
|
|
344
|
+
// Input and output are written as code a reader keeps as written.
|
|
345
|
+
const sampleBlock = (writer, processor) => (node, ctx, interator) => {
|
|
346
|
+
writeBodyAsWritten(writer, node, ctx, interator);
|
|
347
|
+
};
|
|
348
|
+
// HTML the reader of Markdown made stays as written. An author's =Html is shown, not run:
|
|
349
|
+
// raw, a style could hide what follows it and an open comment could take the rest.
|
|
350
|
+
const htmlBlock = (writer, processor) => (node, ctx, interator) => {
|
|
351
|
+
if (node.sourceFormat === 'markdown')
|
|
352
|
+
return markdownHtml(writer, node, ctx, interator);
|
|
353
|
+
const body = (node.content || []).map(child => (isBodyText(child) ? bodyValue(child) : ' ')).join('');
|
|
354
|
+
if (body.trim() === '')
|
|
355
|
+
return;
|
|
356
|
+
if (writeBodyAsWritten(writer, node, ctx, interator))
|
|
357
|
+
writer.writeRaw('\n');
|
|
358
|
+
};
|
|
359
|
+
// HTML of Markdown that stands for a block needs a blank line before it, or a reader
|
|
360
|
+
// takes it into the paragraph above, and one before what follows it in the same
|
|
361
|
+
// content, or a reader takes that into the HTML. Nothing is added at the start of
|
|
362
|
+
// the content or at its end.
|
|
363
|
+
const markdownHtml = (writer, node, ctx, interator) => {
|
|
364
|
+
if (node.markdownHtml !== 'block' || !ownsLines(writer) || writer.cellDepth > 0)
|
|
365
|
+
return interator(node.content, ctx);
|
|
366
|
+
const missing = writer.trailingBreaks === null ? 0 : 2 - writer.trailingBreaks;
|
|
367
|
+
if (missing > 0)
|
|
368
|
+
writer.writeRaw('\n'.repeat(missing));
|
|
369
|
+
interator(node.content, ctx);
|
|
370
|
+
if (writer.trailingBreaks === 0)
|
|
371
|
+
writer.writeRaw('\n');
|
|
372
|
+
writer.pendingBreak = true;
|
|
373
|
+
};
|
|
374
|
+
// The first visible line of a paragraph is written without its indent. Inside an item
|
|
375
|
+
// the indent of a later paragraph is what keeps it in the item, so it stays there.
|
|
376
|
+
const paragraph = (writer, ctx, write) => {
|
|
377
|
+
if (ctx.inItem || typeof writer.paraStart !== 'boolean')
|
|
378
|
+
return write();
|
|
379
|
+
writer.paraStart = true;
|
|
380
|
+
try {
|
|
381
|
+
write();
|
|
382
|
+
}
|
|
383
|
+
finally {
|
|
384
|
+
writer.paraStart = false;
|
|
385
|
+
}
|
|
386
|
+
};
|
|
93
387
|
const implicitPara = handleNested((writer, processor) => (node, ctx, interator) => {
|
|
94
|
-
|
|
95
|
-
interator(node.content, ctx);
|
|
388
|
+
paragraph(writer, ctx, () => node.content && interator(node.content, ctx));
|
|
96
389
|
writer.writeRaw('\n');
|
|
97
390
|
});
|
|
98
391
|
const termPara = (writer, processor) => (node, ctx, interator) => {
|
|
@@ -107,19 +400,115 @@ const writtenCode = (writer, node, ctx, interator) => {
|
|
|
107
400
|
interator(node.content, ctx);
|
|
108
401
|
writer.write(close);
|
|
109
402
|
};
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
403
|
+
// text of the document, hidden or not by the node it comes from
|
|
404
|
+
const writeText = (writer, node, ctx, value) => {
|
|
405
|
+
if (ctx.keepSpaces) {
|
|
406
|
+
// inside S<>: every space stays and does not break, a line break stays a line break
|
|
407
|
+
const text = writer.escape(expandTabs(isCovered(node, ctx) ? maskText(value) : value, ctx.keepSpaces));
|
|
408
|
+
writer.writeRaw(text.replace(/ /g, ' ').replace(/\n/g, keptBreak(writer, ctx)));
|
|
409
|
+
}
|
|
410
|
+
else {
|
|
411
|
+
const text = isCovered(node, ctx) ? maskText(value) : value;
|
|
412
|
+
const shown = writer.paraStart && !(writer.codeDepth > 0) ? text.replace(/^[ \t\n]+/, '') : text;
|
|
413
|
+
writer.write(ctx.headingLine && writer.codeDepth > 0 ? shown.replace(/\n/g, ' ') : shown);
|
|
414
|
+
}
|
|
415
|
+
};
|
|
416
|
+
// A line break S<> keeps: two spaces before the break, and in the line of a heading,
|
|
417
|
+
// which holds no break, the tag of one; inside code there it is a space, as code in
|
|
418
|
+
// a line has no break of its own.
|
|
419
|
+
const keptBreak = (writer, ctx) => (!ctx.headingLine ? ' \n' : writer.codeDepth > 0 ? ' ' : '<br>');
|
|
420
|
+
// The line of a heading is written aside and given back as one line: a break of code
|
|
421
|
+
// or of S<> was settled when it was written, and every other break, with the spaces
|
|
422
|
+
// around it, becomes one space. The break that ends the paragraph goes, and the
|
|
423
|
+
// spaces before it stay, so a heading on one line is written as before.
|
|
424
|
+
const headingLine = (writer, write) => {
|
|
425
|
+
const line = writeAside(writer, write);
|
|
426
|
+
const ended = line.endsWith('\n');
|
|
427
|
+
return (ended ? line.slice(0, -1) : line).replace(/[ \t]*\n[ \t]*/g, ' ') + (ended ? '\n' : '');
|
|
428
|
+
};
|
|
429
|
+
// a paragraph a title takes apart keeps its own mask for the text it gives the title
|
|
430
|
+
const maskedOwner = (owner, ctx) => {
|
|
431
|
+
if (ctx?.maskMode || ctx?.renderMode === 'draft')
|
|
432
|
+
return false;
|
|
433
|
+
if (owner.guarded)
|
|
434
|
+
return true;
|
|
435
|
+
const conf = makeAttrs(owner, ctx || {});
|
|
436
|
+
return Boolean(conf.exists('masked') && conf.getFirstValue('masked'));
|
|
437
|
+
};
|
|
438
|
+
// =TITLE shows its content as the title of the document, as HTML and to-jsx do: its
|
|
439
|
+
// paragraphs in one heading of the first level, other blocks after it, a directive
|
|
440
|
+
// acting from where it stands. A title with nothing to show writes no heading, and
|
|
441
|
+
// an anchor only when the author named it.
|
|
442
|
+
const titleBlock = (writer, node, ctx, interator) => {
|
|
443
|
+
const note = noteHeading(ctx, node);
|
|
444
|
+
const heading = [];
|
|
445
|
+
const rest = [];
|
|
446
|
+
titleParts(node).forEach(entry => {
|
|
447
|
+
if (entry.kind === 'rest') {
|
|
448
|
+
rest.push(writeAside(writer, () => interator([entry.node], ctx)));
|
|
449
|
+
return;
|
|
116
450
|
}
|
|
117
|
-
|
|
118
|
-
|
|
451
|
+
const masked = entry.owners.some(owner => maskedOwner(owner, ctx));
|
|
452
|
+
const partCtx = { ...ctx, headingLine: true, __headingNode: node, ...(masked ? { maskMode: true } : {}) };
|
|
453
|
+
// the line breaks and spaces at the edges of a paragraph go; a no-break space stays
|
|
454
|
+
const part = headingLine(writer, () => interator(entry.content, partCtx)).replace(/^[ \t\n]+|[ \t\n]+$/g, '');
|
|
455
|
+
if (/\S/.test(part))
|
|
456
|
+
heading.push(part);
|
|
457
|
+
});
|
|
458
|
+
// a link reaches a title by the name its author gave it, shaped as for a reader and kept
|
|
459
|
+
// off the addresses the reader builds for the headings
|
|
460
|
+
const written = getExplicitNodeId(node, ctx);
|
|
461
|
+
const anchors = markdownAnchors(ctx);
|
|
462
|
+
const own = written === null
|
|
463
|
+
? ''
|
|
464
|
+
: anchors?.byNode.has(node)
|
|
465
|
+
? anchors.named?.has(node)
|
|
466
|
+
? anchors.byNode.get(node)
|
|
467
|
+
: ''
|
|
468
|
+
: toMarkdownFragment(written).toLowerCase();
|
|
469
|
+
if (own)
|
|
470
|
+
writer.writeRaw(`<a name="${quoteAttribute(own)}"></a>\n\n`);
|
|
471
|
+
note(heading.length ? heading.join(' ') : null);
|
|
472
|
+
if (heading.length)
|
|
473
|
+
writer.writeRaw(`# ${heading.join(' ')}\n\n`);
|
|
474
|
+
rest.forEach(text => writer.writeRaw(text));
|
|
475
|
+
};
|
|
476
|
+
// HTML the markdown plugin finds inside a line comes as an Html block of one line
|
|
477
|
+
const inlineHtml = node => node.name === 'Html' &&
|
|
478
|
+
(node.content || []).every(part => part.type === 'verbatim' && !String(part.value).includes('\n'));
|
|
479
|
+
// A cell holds a block when it holds more than one paragraph or anything but a paragraph
|
|
480
|
+
// and text; a comment, data and a directive show nothing and count for nothing.
|
|
481
|
+
const holdsBlock = cell => {
|
|
482
|
+
let paragraphs = 0;
|
|
483
|
+
let inline = false;
|
|
484
|
+
let block = false;
|
|
485
|
+
const visit = nodes => (nodes || []).forEach(node => {
|
|
486
|
+
if (typeof node === 'string' || node.type === 'text') {
|
|
487
|
+
if (String(typeof node === 'string' ? node : node.value || '').trim() !== '')
|
|
488
|
+
inline = true;
|
|
119
489
|
}
|
|
120
|
-
else
|
|
490
|
+
else if (node.type === 'fcode' || (node.type === 'block' && inlineHtml(node)))
|
|
491
|
+
inline = true;
|
|
492
|
+
else if (node.type === 'para')
|
|
493
|
+
paragraphs++;
|
|
494
|
+
else if (node.type === 'block' && node.name === 'para')
|
|
495
|
+
visit(node.content);
|
|
496
|
+
else if (node.type === 'blankline' || node.type === 'config' || node.type === 'alias')
|
|
497
|
+
return;
|
|
498
|
+
else if (node.type === 'block' && (node.name === 'comment' || node.name === 'data'))
|
|
499
|
+
return;
|
|
500
|
+
else
|
|
501
|
+
block = true;
|
|
502
|
+
});
|
|
503
|
+
visit(cell.content);
|
|
504
|
+
return block || paragraphs + (inline ? 1 : 0) > 1;
|
|
505
|
+
};
|
|
506
|
+
const rules = {
|
|
507
|
+
':text': (writer, processor) => (node, ctx, interator) => {
|
|
508
|
+
if (node.value)
|
|
509
|
+
writeText(writer, node, ctx, node.value);
|
|
510
|
+
else
|
|
121
511
|
interator(node.content, ctx);
|
|
122
|
-
}
|
|
123
512
|
},
|
|
124
513
|
// Formatting codes
|
|
125
514
|
'A<>': (writer, processor) => (node, ctx, interator) => {
|
|
@@ -136,11 +525,19 @@ const rules = {
|
|
|
136
525
|
else {
|
|
137
526
|
interator(tree, ctx);
|
|
138
527
|
}
|
|
139
|
-
},
|
|
528
|
+
},
|
|
529
|
+
// the walk that learns the lines of the headings stays quiet: the walk after it speaks
|
|
530
|
+
message => ctx.__headingLines || console.warn(`[podlite] ${message}`)).expanded;
|
|
140
531
|
if (!expanded)
|
|
141
|
-
writer.
|
|
532
|
+
writer.write(`A<${covered(node, ctx, term)}>`);
|
|
533
|
+
},
|
|
534
|
+
'C<>': codeSpan,
|
|
535
|
+
// the characters of an entity are text like any other in the same place
|
|
536
|
+
'E<>': (writer, processor) => (node, ctx, interator) => {
|
|
537
|
+
const text = decodeEntities(node.content);
|
|
538
|
+
if (text)
|
|
539
|
+
writeText(writer, node, ctx, text);
|
|
142
540
|
},
|
|
143
|
-
'C<>': wrapContent('`', '`'),
|
|
144
541
|
'D<>': (writer, processor) => (node, ctx, interator) => {
|
|
145
542
|
if (!writer.hasOwnProperty('DEFINITIONS')) {
|
|
146
543
|
writer.DEFINITIONS = [];
|
|
@@ -153,7 +550,7 @@ const rules = {
|
|
|
153
550
|
'B<>': wrapContent('**', '**'),
|
|
154
551
|
'I<>': wrapContent('*', '*'),
|
|
155
552
|
'R<>': wrapContent('*', '*'),
|
|
156
|
-
'K<>':
|
|
553
|
+
'K<>': codeSpan,
|
|
157
554
|
'O<>': wrapContent('~~', '~~'),
|
|
158
555
|
'G<>': (writer, processor) => (node, ctx, interator) => {
|
|
159
556
|
if (ctx.renderMode === 'draft') {
|
|
@@ -163,14 +560,17 @@ const rules = {
|
|
|
163
560
|
const masked = maskText(collectText(node.content));
|
|
164
561
|
// inside S<> the spaces of the hidden phrase stay and do not break, as all its text
|
|
165
562
|
if (ctx.keepSpaces)
|
|
166
|
-
writer.writeRaw(writer
|
|
563
|
+
writer.writeRaw(writer
|
|
564
|
+
.escape(expandTabs(masked, ctx.keepSpaces))
|
|
565
|
+
.replace(/ /g, ' ')
|
|
566
|
+
.replace(/\n/g, keptBreak(writer, ctx)));
|
|
167
567
|
else
|
|
168
|
-
writer.write(masked);
|
|
568
|
+
writer.write(ctx.headingLine && writer.codeDepth > 0 ? masked.replace(/\n/g, ' ') : masked);
|
|
169
569
|
},
|
|
170
570
|
'H<>': wrapContent('<sup>', '</sup>'),
|
|
171
571
|
'J<>': wrapContent('<sub>', '</sub>'),
|
|
172
|
-
'L<>':
|
|
173
|
-
'W<>':
|
|
572
|
+
'L<>': (writer, processor) => (node, ctx, interator) => linkWrap(node, ctx, writer.codeDepth > 0)(writer, processor)(node, ctx, interator),
|
|
573
|
+
'W<>': (writer, processor) => (node, ctx, interator) => linkWrap(node, ctx, writer.codeDepth > 0)(writer, processor)(node, ctx, interator),
|
|
174
574
|
'N<>': (writer, processor) => {
|
|
175
575
|
writer.addListener('end', () => {
|
|
176
576
|
if (!writer.hasOwnProperty('FOOTNOTES')) {
|
|
@@ -203,8 +603,9 @@ const rules = {
|
|
|
203
603
|
gid,
|
|
204
604
|
fnRefId: `fnref:${gid}`,
|
|
205
605
|
fnId: `fn:${gid}`,
|
|
606
|
+
// the note is written at the end of the document, not in the line it is called from
|
|
206
607
|
make: () => {
|
|
207
|
-
interator(node.content, ctx);
|
|
608
|
+
interator(node.content, ctx.headingLine ? { ...ctx, headingLine: false } : ctx);
|
|
208
609
|
},
|
|
209
610
|
});
|
|
210
611
|
};
|
|
@@ -221,7 +622,7 @@ const rules = {
|
|
|
221
622
|
// a reserved code is shown as written: its letter and brackets around what it holds
|
|
222
623
|
'Q<>': (writer, processor) => (node, ctx, interator) => writtenCode(writer, node, ctx, interator),
|
|
223
624
|
'Y<>': (writer, processor) => (node, ctx, interator) => writtenCode(writer, node, ctx, interator),
|
|
224
|
-
'T<>':
|
|
625
|
+
'T<>': codeSpan,
|
|
225
626
|
'U<>': wrapContent('', ''),
|
|
226
627
|
'V<>': content,
|
|
227
628
|
'X<>': (writer, processor) => (node, ctx, interator) => {
|
|
@@ -244,52 +645,44 @@ const rules = {
|
|
|
244
645
|
':code': handleNested((writer, processor) => (node, ctx, interator) => {
|
|
245
646
|
const conf = makeAttrs(node, ctx);
|
|
246
647
|
const lang = conf.exists('lang') ? conf.getFirstValue('lang') : '';
|
|
247
|
-
writer.writeRaw('```' + lang + '\n');
|
|
248
648
|
const masked = isCovered(node, ctx);
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
});
|
|
261
|
-
}
|
|
262
|
-
// code at the end of input may lack its final line break
|
|
263
|
-
if (!writer.lastWritten.endsWith('\n'))
|
|
264
|
-
writer.writeRaw('\n');
|
|
265
|
-
writer.writeRaw('```\n');
|
|
649
|
+
writeFenced(writer, lang, () => (node.content || []).forEach(child => {
|
|
650
|
+
if (typeof child === 'string') {
|
|
651
|
+
writeSource(writer, masked ? maskText(child) : child);
|
|
652
|
+
}
|
|
653
|
+
else if (child.type === 'verbatim') {
|
|
654
|
+
writeSource(writer, masked ? maskText(child.value) : child.value);
|
|
655
|
+
}
|
|
656
|
+
else {
|
|
657
|
+
interator([child], ctx);
|
|
658
|
+
}
|
|
659
|
+
}));
|
|
266
660
|
}),
|
|
267
661
|
code: handleNested((writer, processor) => (node, ctx, interator) => {
|
|
268
662
|
const conf = makeAttrs(node, ctx);
|
|
269
663
|
const lang = conf.exists('lang') ? conf.getFirstValue('lang') : '';
|
|
270
|
-
writer.writeRaw('```' + lang + '\n');
|
|
271
664
|
const masked = isCovered(node, ctx);
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
});
|
|
284
|
-
}
|
|
285
|
-
writer.writeRaw('```\n');
|
|
665
|
+
writeFenced(writer, lang, () => (node.content || []).forEach(child => {
|
|
666
|
+
if (typeof child === 'string') {
|
|
667
|
+
writeSource(writer, masked ? maskText(child) : child);
|
|
668
|
+
}
|
|
669
|
+
else if (child.type === 'verbatim') {
|
|
670
|
+
writeSource(writer, masked ? maskText(child.value) : child.value);
|
|
671
|
+
}
|
|
672
|
+
else {
|
|
673
|
+
interator([child], ctx);
|
|
674
|
+
}
|
|
675
|
+
}));
|
|
286
676
|
}),
|
|
287
677
|
data: emptyContent,
|
|
678
|
+
Html: htmlBlock,
|
|
288
679
|
':verbatim': (writer, processor) => (node, ctx, interator) => {
|
|
289
680
|
if (node.error) {
|
|
290
681
|
writer.emit('errors', node.location);
|
|
291
682
|
}
|
|
292
|
-
|
|
683
|
+
const text = isCovered(node, ctx) ? maskText(node.value) : node.value;
|
|
684
|
+
// inside a frame of HTML a raw </table> would close the frame and drop the cells after it
|
|
685
|
+
writeSource(writer, writer.htmlDepth > 0 ? htmlText(text) : text);
|
|
293
686
|
},
|
|
294
687
|
':blankline': emptyContent,
|
|
295
688
|
':ambient': emptyContent,
|
|
@@ -307,7 +700,7 @@ const rules = {
|
|
|
307
700
|
return emptyContent;
|
|
308
701
|
}),
|
|
309
702
|
// block =para
|
|
310
|
-
para: handleNested(content),
|
|
703
|
+
para: handleNested((writer, processor) => (node, ctx, interator) => paragraph(writer, ctx, () => node.content && interator(node.content, ctx))),
|
|
311
704
|
':para': implicitPara,
|
|
312
705
|
'head:block': subUse({
|
|
313
706
|
':para': content,
|
|
@@ -324,26 +717,52 @@ const rules = {
|
|
|
324
717
|
writer.writeRaw(prefix);
|
|
325
718
|
if (numberPrefix)
|
|
326
719
|
writer.writeRaw(numberPrefix);
|
|
327
|
-
|
|
328
|
-
|
|
720
|
+
const note = noteHeading(ctx, node);
|
|
721
|
+
const line = headingLine(writer, () => node.content && interator(node.content, { ...ctx, headingLine: true, __headingNode: node }));
|
|
722
|
+
// a reader of Markdown has six levels of headings; a line of more signs is a paragraph
|
|
723
|
+
note(level <= 6 ? numberPrefix + line.replace(/\n$/, '') : null);
|
|
724
|
+
writer.writeRaw(line);
|
|
329
725
|
writer.writeRaw('\n');
|
|
330
726
|
};
|
|
331
727
|
})),
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
interator(node.content, newCtx);
|
|
728
|
+
// A list deeper than an item right after it belongs to that item: it is written inside
|
|
729
|
+
// the item, with the context of its own list, so a mask of the item does not reach it.
|
|
730
|
+
':list': (writer, processor) => (node, ctx, interator) => {
|
|
731
|
+
// pass list type through context
|
|
732
|
+
const newCtx = {
|
|
733
|
+
...ctx,
|
|
734
|
+
itemTail: undefined,
|
|
735
|
+
listType: node.list,
|
|
736
|
+
listCounter: { value: 1 },
|
|
737
|
+
listDepth: (ctx.listDepth || 0) + 1,
|
|
343
738
|
};
|
|
344
|
-
|
|
739
|
+
if (!node.content)
|
|
740
|
+
return;
|
|
741
|
+
if (!ownsLines(writer))
|
|
742
|
+
return interator(node.content, newCtx);
|
|
743
|
+
const children = node.content;
|
|
744
|
+
for (let at = 0; at < children.length; at++) {
|
|
745
|
+
const child = children[at];
|
|
746
|
+
if (child?.type !== 'block' || child.name !== 'item') {
|
|
747
|
+
interator([child], newCtx);
|
|
748
|
+
continue;
|
|
749
|
+
}
|
|
750
|
+
let last = at;
|
|
751
|
+
for (let next = at + 1; next < children.length; next++) {
|
|
752
|
+
const after = children[next];
|
|
753
|
+
if (after?.type === 'list' && Number(after.level) > Number(child.level))
|
|
754
|
+
last = next;
|
|
755
|
+
else if (after?.type !== 'blankline')
|
|
756
|
+
break;
|
|
757
|
+
}
|
|
758
|
+
const tail = children.slice(at + 1, last + 1);
|
|
759
|
+
interator([child], tail.length > 0 ? { ...newCtx, itemTail: { nodes: tail, ctx: newCtx } } : newCtx);
|
|
760
|
+
at = last;
|
|
761
|
+
}
|
|
762
|
+
},
|
|
763
|
+
// The marker of an item stands on the first line of what the item writes; the lines
|
|
764
|
+
// after it are indented by the width of the marker, so they stay in the item.
|
|
345
765
|
'item:block': (writer, processor) => (node, ctx, interator) => {
|
|
346
|
-
const indent = ' '.repeat(Math.max(0, (ctx.listDepth || 1) - 1));
|
|
347
766
|
const listType = ctx.listType || 'unordered';
|
|
348
767
|
let prefix;
|
|
349
768
|
if (listType === 'ordered') {
|
|
@@ -351,18 +770,32 @@ const rules = {
|
|
|
351
770
|
prefix = `${counter.value}. `;
|
|
352
771
|
counter.value++;
|
|
353
772
|
}
|
|
354
|
-
else if (listType === 'variable') {
|
|
355
|
-
prefix = '- ';
|
|
356
|
-
}
|
|
357
773
|
else {
|
|
358
774
|
prefix = '- ';
|
|
359
775
|
}
|
|
360
|
-
|
|
361
|
-
|
|
776
|
+
const box = node.checked === undefined ? '' : node.checked ? '[x] ' : '[ ] ';
|
|
777
|
+
if (!ownsLines(writer)) {
|
|
778
|
+
writer.writeRaw(' '.repeat(Math.max(0, (ctx.listDepth || 1) - 1)) + prefix + box);
|
|
779
|
+
if (node.content)
|
|
780
|
+
interator(node.content, { ...ctx, inItem: true });
|
|
781
|
+
return;
|
|
362
782
|
}
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
783
|
+
const tail = ctx.itemTail;
|
|
784
|
+
const body = writeAside(writer, () => {
|
|
785
|
+
startLine(writer);
|
|
786
|
+
if (box)
|
|
787
|
+
writer.writeRaw(box);
|
|
788
|
+
if (node.content)
|
|
789
|
+
interator(node.content, { ...ctx, itemTail: undefined });
|
|
790
|
+
if (tail)
|
|
791
|
+
interator(tail.nodes, tail.ctx);
|
|
792
|
+
});
|
|
793
|
+
const indent = ' '.repeat(prefix.length);
|
|
794
|
+
const lines = body.replace(/\n+$/, '').split('\n');
|
|
795
|
+
// an item ends as its content does: a blank line after it keeps a loose list loose,
|
|
796
|
+
// and a tight list of markdown stays tight
|
|
797
|
+
const end = /\n\n$/.test(body) ? '\n\n' : '\n';
|
|
798
|
+
writer.writeRaw(lines.map((line, i) => (i === 0 ? prefix + line : line === '' ? '' : indent + line)).join('\n') + end);
|
|
366
799
|
},
|
|
367
800
|
'comment:block': emptyContent,
|
|
368
801
|
'test:block': (writer, processor) => (node, ctx, interator) => {
|
|
@@ -377,7 +810,9 @@ const rules = {
|
|
|
377
810
|
const conf = makeAttrs(node, ctx);
|
|
378
811
|
const absent = conf.exists('absent') && Boolean(conf.getFirstValue('absent'));
|
|
379
812
|
const selector = collectText(node.content).trim();
|
|
380
|
-
writer.writeRaw(
|
|
813
|
+
writer.writeRaw('- ');
|
|
814
|
+
writeSource(writer, inlineCode(isCovered(node, ctx) ? maskText(selector) : selector));
|
|
815
|
+
writer.writeRaw(' ');
|
|
381
816
|
writer.writeRaw(absent ? 'must find no block' : 'must find a block');
|
|
382
817
|
if (conf.exists('caption')) {
|
|
383
818
|
writer.writeRaw(': ');
|
|
@@ -388,8 +823,10 @@ const rules = {
|
|
|
388
823
|
'resource:block': (writer, processor) => (node, ctx, interator) => {
|
|
389
824
|
const conf = makeAttrs(node, ctx);
|
|
390
825
|
writer.writeRaw('\nResource');
|
|
391
|
-
if (conf.exists('name'))
|
|
392
|
-
writer.writeRaw(
|
|
826
|
+
if (conf.exists('name')) {
|
|
827
|
+
writer.writeRaw(' ');
|
|
828
|
+
writeSource(writer, inlineCode(covered(node, ctx, String(conf.getFirstValue('name')))));
|
|
829
|
+
}
|
|
393
830
|
writer.writeRaw(':\n\n');
|
|
394
831
|
writeSourceBlock(writer, node, ctx);
|
|
395
832
|
},
|
|
@@ -416,25 +853,15 @@ const rules = {
|
|
|
416
853
|
}),
|
|
417
854
|
'term:para': termPara,
|
|
418
855
|
nested: handleNested(content, 1),
|
|
419
|
-
output: handleNested(
|
|
420
|
-
|
|
421
|
-
if (node.content)
|
|
422
|
-
interator(node.content, ctx);
|
|
423
|
-
writer.writeRaw('```\n');
|
|
424
|
-
}, 1),
|
|
425
|
-
input: handleNested((writer, processor) => (node, ctx, interator) => {
|
|
426
|
-
writer.writeRaw('```\n');
|
|
427
|
-
if (node.content)
|
|
428
|
-
interator(node.content, ctx);
|
|
429
|
-
writer.writeRaw('```\n');
|
|
430
|
-
}, 1),
|
|
856
|
+
output: handleNested(sampleBlock),
|
|
857
|
+
input: handleNested(sampleBlock),
|
|
431
858
|
// table section
|
|
432
859
|
'table:block': handleNested((writer, processor) => (node, ctx, interator) => {
|
|
433
860
|
const conf = makeAttrs(node, ctx);
|
|
434
861
|
// a caption with codes of its own stands as a paragraph: bold around it would
|
|
435
862
|
// hide the bold inside and break on an emphasis that touches a word
|
|
436
863
|
if (node.caption) {
|
|
437
|
-
interator(node.caption.content, ctx);
|
|
864
|
+
paragraph(writer, ctx, () => interator(node.caption.content, ctx));
|
|
438
865
|
writer.writeRaw('\n\n');
|
|
439
866
|
}
|
|
440
867
|
else if (conf.exists('caption')) {
|
|
@@ -456,14 +883,15 @@ const rules = {
|
|
|
456
883
|
const n = Number(entry.value);
|
|
457
884
|
return Number.isFinite(n) && n > 1 ? n : 0;
|
|
458
885
|
};
|
|
459
|
-
// GFM pipe-tables
|
|
460
|
-
|
|
886
|
+
// GFM pipe-tables hold neither a merged cell nor a block in a cell: such a table is
|
|
887
|
+
// written as a frame of HTML whose cells hold Markdown
|
|
888
|
+
const hasSpans = rows.some(row => (row.content || []).some(c => c.name === 'cell' && (cellSpan(c, 'colspan') || cellSpan(c, 'rowspan') || holdsBlock(c))));
|
|
461
889
|
const hasHeader = isRowHeader(rows[0]);
|
|
462
890
|
if (hasSpans) {
|
|
463
891
|
writer.writeRaw('<table>\n');
|
|
464
|
-
rows.forEach(row => {
|
|
892
|
+
within(writer, 'htmlDepth', () => rows.forEach(row => {
|
|
465
893
|
const tag = isRowHeader(row) ? 'th' : 'td';
|
|
466
|
-
writer.writeRaw('<tr
|
|
894
|
+
writer.writeRaw('<tr>\n');
|
|
467
895
|
const cells = (row.content || []).filter(c => c.name === 'cell');
|
|
468
896
|
cells.forEach(cell => {
|
|
469
897
|
const colspan = cellSpan(cell, 'colspan');
|
|
@@ -473,32 +901,21 @@ const rules = {
|
|
|
473
901
|
attrs += ` colspan="${colspan}"`;
|
|
474
902
|
if (rowspan)
|
|
475
903
|
attrs += ` rowspan="${rowspan}"`;
|
|
476
|
-
writer
|
|
477
|
-
writeCellParts(cell, writer, interator, ctx);
|
|
478
|
-
writer.writeRaw(`</${tag}>`);
|
|
904
|
+
writeIsland(writer, `<${tag}${attrs}>`, `</${tag}>`, () => writeCellParts(cell, writer, interator, ctx));
|
|
479
905
|
});
|
|
480
906
|
writer.writeRaw('</tr>\n');
|
|
481
|
-
});
|
|
482
|
-
|
|
907
|
+
}));
|
|
908
|
+
// a blank line ends the block of HTML, or the text after it is read as HTML too
|
|
909
|
+
writer.writeRaw('</table>\n\n');
|
|
483
910
|
return;
|
|
484
911
|
}
|
|
485
912
|
// A cell holds a paragraph when the table is written as blocks, and a
|
|
486
913
|
// paragraph prints line breaks that would tear the row apart. The cell is
|
|
487
914
|
// rendered aside and folded onto one line.
|
|
488
915
|
const renderCell = cell => {
|
|
489
|
-
|
|
490
|
-
const original = writer.output;
|
|
491
|
-
if (typeof original !== 'function')
|
|
916
|
+
if (typeof writer.output !== 'function')
|
|
492
917
|
return null;
|
|
493
|
-
writer
|
|
494
|
-
try {
|
|
495
|
-
writeCellParts(cell, writer, interator, ctx);
|
|
496
|
-
}
|
|
497
|
-
finally {
|
|
498
|
-
writer.output = original;
|
|
499
|
-
}
|
|
500
|
-
return parts
|
|
501
|
-
.join('')
|
|
918
|
+
return writeAside(writer, () => within(writer, 'cellDepth', () => writeCellParts(cell, writer, interator, ctx)))
|
|
502
919
|
.replace(/\s*\n+\s*/g, ' ')
|
|
503
920
|
.trim();
|
|
504
921
|
};
|
|
@@ -510,7 +927,7 @@ const rules = {
|
|
|
510
927
|
writer.writeRaw(' ');
|
|
511
928
|
const folded = renderCell(cell);
|
|
512
929
|
if (folded === null) {
|
|
513
|
-
writeCellParts(cell, writer, interator, ctx);
|
|
930
|
+
within(writer, 'cellDepth', () => writeCellParts(cell, writer, interator, ctx));
|
|
514
931
|
}
|
|
515
932
|
else {
|
|
516
933
|
writer.writeRaw(folded);
|
|
@@ -547,13 +964,56 @@ const rules = {
|
|
|
547
964
|
':image': (writer, processor) => (node, ctx, interator) => {
|
|
548
965
|
// the alternative text sits between square brackets, where a `]` of its own
|
|
549
966
|
// would end it, and the address between round ones — the same shape a link has
|
|
550
|
-
const
|
|
551
|
-
const
|
|
552
|
-
|
|
967
|
+
const inCode = writer.codeDepth > 0;
|
|
968
|
+
const bracketed = covered(node, ctx, writtenValue(node.alt) ?? '').replace(/([[\]\\])/g, '\\$1');
|
|
969
|
+
const alt = inCode ? bracketed : keepReferences(bracketed);
|
|
970
|
+
const src = markdownAddress(String(applyImageBase(node.src, ctx?.base) ?? ''), inCode);
|
|
971
|
+
const title = typeof node.title === 'string' ? quotedTitle(covered(node, ctx, node.title), inCode) : '';
|
|
972
|
+
writer.writeRaw(``);
|
|
553
973
|
},
|
|
554
974
|
};
|
|
975
|
+
// The output walks the document aside to learn the line it writes for each heading, and
|
|
976
|
+
// builds the addresses of its links from the text a reader shows for those lines. When a
|
|
977
|
+
// heading line holds a link to this document in code, the line depends on the addresses:
|
|
978
|
+
// the heading and its target are named, and the walk is repeated until the texts stay.
|
|
979
|
+
// The walk aside writes as the writer of the walk does when that writer writes Markdown.
|
|
980
|
+
const asideWriter = writer => (writer instanceof writerMarkdown ? writer.constructor : writerMarkdown);
|
|
981
|
+
const prepare = (tree, context, walk) => {
|
|
982
|
+
const named = headingsWithCodeLinks(tree, context.__bindings);
|
|
983
|
+
const ofTree = nodesOfTree(tree);
|
|
984
|
+
let written;
|
|
985
|
+
let stable = false;
|
|
986
|
+
// Each walk settles one more link of a chain of such headings, and a chain is no longer
|
|
987
|
+
// than the headings the first walk wrote: the number of walks is fixed by then.
|
|
988
|
+
for (let read = 1, limit = 2; read <= limit; read++) {
|
|
989
|
+
const lines = [];
|
|
990
|
+
const codeLinks = new Set();
|
|
991
|
+
walk({ ...context, __headingLines: lines, __codeLinks: codeLinks, __codeLinkHeadings: named, __readerTexts: written }, asideWriter);
|
|
992
|
+
if (read === 1)
|
|
993
|
+
limit = lines.length + 2;
|
|
994
|
+
const next = lines.map(({ node, line }) => ({ node, text: line === null ? null : headingTextOf(line) }));
|
|
995
|
+
const before = named.size;
|
|
996
|
+
// a heading an alias brings is a new node on every walk: it cannot be named, and its
|
|
997
|
+
// text is compared, not the node
|
|
998
|
+
codeLinks.forEach(node => ofTree.has(node) && named.add(node));
|
|
999
|
+
const same = written !== undefined &&
|
|
1000
|
+
written.length === next.length &&
|
|
1001
|
+
written.every((entry, at) => entry.text === next[at].text);
|
|
1002
|
+
written = next;
|
|
1003
|
+
// without links in code, no line depends on an address and one walk is enough
|
|
1004
|
+
if (codeLinks.size === 0 || (same && named.size === before)) {
|
|
1005
|
+
stable = true;
|
|
1006
|
+
break;
|
|
1007
|
+
}
|
|
1008
|
+
}
|
|
1009
|
+
if (!stable)
|
|
1010
|
+
console.warn('[podlite] the addresses of the markdown headings do not settle; a link may lead to the wrong one');
|
|
1011
|
+
context.__codeLinkHeadings = named;
|
|
1012
|
+
context.__readerTexts = written;
|
|
1013
|
+
};
|
|
555
1014
|
const toMarkdown = opt => toAny({
|
|
556
1015
|
writer: writerMarkdown,
|
|
1016
|
+
prepare,
|
|
557
1017
|
...opt,
|
|
558
1018
|
context: { renderMode: opt?.renderMode || 'production', base: opt?.base, ...(opt?.context || {}) },
|
|
559
1019
|
})
|
|
@@ -564,11 +1024,18 @@ const toMarkdown = opt => toAny({
|
|
|
564
1024
|
const isTypeBlock = (node.type || '') === 'block';
|
|
565
1025
|
return isTypeBlock && name === name.toUpperCase();
|
|
566
1026
|
};
|
|
1027
|
+
if (blockWithoutRule(writer, node, ctx, interator))
|
|
1028
|
+
return;
|
|
1029
|
+
if (isTitleBlock(node))
|
|
1030
|
+
return titleBlock(writer, node, ctx, interator);
|
|
567
1031
|
if (isSemanticBlock(node)) {
|
|
568
|
-
const name = node.name;
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
1032
|
+
const name = writeAside(writer, () => writer.write(covered(node, ctx, String(node.name))));
|
|
1033
|
+
noteHeading(ctx, node)(name);
|
|
1034
|
+
const anchors = markdownAnchors(ctx);
|
|
1035
|
+
const own = anchors?.named?.has(node) ? anchors.byNode.get(node) : undefined;
|
|
1036
|
+
if (own)
|
|
1037
|
+
writer.writeRaw(`<a name="${quoteAttribute(own)}"></a>\n\n`);
|
|
1038
|
+
writer.writeRaw(`# ${name}\n\n`);
|
|
572
1039
|
}
|
|
573
1040
|
if (node.hasOwnProperty('content')) {
|
|
574
1041
|
interator(node.content, ctx);
|