confluence-md-sync 0.11.2 → 0.13.0
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/README.md +16 -0
- package/dist/export/to-markdown.js +132 -9
- package/dist/macros/plugins/core.js +2 -1
- package/dist/macros/plugins/drawio.js +3 -2
- package/dist/macros/plugins/note.d.ts +3 -0
- package/dist/macros/plugins/note.js +13 -0
- package/dist/macros/plugins/structurizr.js +3 -3
- package/dist/macros/plugins/table-filter.js +1 -7
- package/dist/markdown/image.d.ts +2 -0
- package/dist/markdown/image.js +19 -0
- package/dist/markdown/inline-syntax.d.ts +13 -0
- package/dist/markdown/inline-syntax.js +72 -0
- package/dist/markdown/native.js +9 -3
- package/dist/markdown/note-layout.d.ts +1 -0
- package/dist/markdown/note-layout.js +41 -0
- package/dist/markdown/note.d.ts +6 -0
- package/dist/markdown/note.js +26 -0
- package/dist/markdown/placeholder.d.ts +8 -0
- package/dist/markdown/placeholder.js +23 -0
- package/dist/markdown/render.d.ts +2 -29
- package/dist/markdown/render.js +81 -84
- package/dist/syntax.d.ts +3 -0
- package/dist/syntax.js +4 -0
- package/package.json +5 -1
package/README.md
CHANGED
|
@@ -496,3 +496,19 @@ the `v*` tag triggers the npm publish workflow (tag must match
|
|
|
496
496
|
## License
|
|
497
497
|
|
|
498
498
|
[MIT](LICENSE)
|
|
499
|
+
|
|
500
|
+
### Inline Note and native image formatting
|
|
501
|
+
|
|
502
|
+
`{{note:Required category}}` works in prose and GFM cells. Nested `{{page:…}}`,
|
|
503
|
+
`{{jira:…}}` and `{{file:…}}` stay editable. Escape literal `|`, `{`, `}`, `:` with
|
|
504
|
+
backslashes. A newline (or `\n` in a plain body) creates a paragraph; `<br>` keeps
|
|
505
|
+
a line break within the same paragraph. For parameters or a multi-paragraph cell,
|
|
506
|
+
use a JSON-quoted body: `{{note:"First\nSecond<br>line"|title=Context|icon=false}}`.
|
|
507
|
+
Block `> [!NOTE]` is unchanged. Complex imported cell Notes retain storage losslessly.
|
|
508
|
+
|
|
509
|
+
Native attachment images forward Confluence image parameters: width/height, align,
|
|
510
|
+
border, thumbnail, title (Data Center caption), alt, hspace/vspace, class/style and
|
|
511
|
+
queryparams (effects). `link=https://…` hyperlinks an image; `display=link` displays
|
|
512
|
+
the attachment as a link. Structurizr and optional draw.io image mode share these
|
|
513
|
+
parameters; draw.io uses its native macro by default. Values containing separators
|
|
514
|
+
can use JSON quotes. Browser-safe authoring helpers are exported from `/syntax`.
|
|
@@ -24,6 +24,9 @@ import { escapeXmlAttr } from '../macros/xml.js';
|
|
|
24
24
|
import { processMacros } from '../macros/registry.js';
|
|
25
25
|
import { defaultMacroRegistry } from '../macros/index.js';
|
|
26
26
|
import { renderToStorage } from '../markdown/render.js';
|
|
27
|
+
import { parsePlaceholder, placeholder } from '../markdown/placeholder.js';
|
|
28
|
+
import { readInlineSyntax } from '../markdown/inline-syntax.js';
|
|
29
|
+
import { inlineNote } from '../markdown/note.js';
|
|
27
30
|
import { compareStorage } from './canonical.js';
|
|
28
31
|
import { NOTICE_MACRO_ID } from '../publish/notice.js';
|
|
29
32
|
import { rewriteStorageLinks } from './links.js';
|
|
@@ -160,6 +163,8 @@ class Converter {
|
|
|
160
163
|
return this.listToMd(el);
|
|
161
164
|
if (el.name === 'table') {
|
|
162
165
|
// tables:'records' — любую таблицу разворачиваем в записи.
|
|
166
|
+
if (this.hasComplexNote(el.children))
|
|
167
|
+
return this.fence(serializeStorage([el]));
|
|
163
168
|
if (this.tablesAsRecords)
|
|
164
169
|
return this.recordsTable(el);
|
|
165
170
|
try {
|
|
@@ -352,6 +357,7 @@ class Converter {
|
|
|
352
357
|
toc: 'toc', children: 'children', jira: 'jira', status: 'status',
|
|
353
358
|
anchor: 'anchor', detailssummary: 'properties-report',
|
|
354
359
|
'portfolio-for-jira-plan': 'portfolio-for-jira-plan',
|
|
360
|
+
'table-excerpt-include': 'table-excerpt-include',
|
|
355
361
|
};
|
|
356
362
|
tryNativeMd(el, name) {
|
|
357
363
|
const parts = this.macroParts(el);
|
|
@@ -363,6 +369,23 @@ class Converter {
|
|
|
363
369
|
const pmap = new Map(params.map((p) => [p.name, p.value]));
|
|
364
370
|
const oneline = (v) => v !== undefined && !/[\n|{}]/.test(v) && !v.includes('}}');
|
|
365
371
|
let candidate = null;
|
|
372
|
+
if (['table-filter', 'table-excerpt', 'table-joiner'].includes(name) && richBody !== null) {
|
|
373
|
+
if (elements(el.children).filter(p => p.name === 'ac:parameter').some(p => elements(p.children).length))
|
|
374
|
+
return null;
|
|
375
|
+
if (params.some(p => !/^[\w-]+$/.test(p.name)))
|
|
376
|
+
return null;
|
|
377
|
+
const bodyMd = this.bodyMd(richBody);
|
|
378
|
+
if (bodyMd === null)
|
|
379
|
+
return null;
|
|
380
|
+
candidate = `::: ${name}${params.map(p => ` ${p.name}=${JSON.stringify(p.value)}`).join('')}\n${bodyMd}\n:::`;
|
|
381
|
+
if (this.verifyMacroMarker(el, candidate))
|
|
382
|
+
return candidate;
|
|
383
|
+
if (this.verifyTableMacro(el, candidate)) {
|
|
384
|
+
this.stats.normalized++;
|
|
385
|
+
return candidate;
|
|
386
|
+
}
|
|
387
|
+
return null;
|
|
388
|
+
}
|
|
366
389
|
if ((name === 'drawio' || name === 'inc-drawio') && richBody === null) {
|
|
367
390
|
const filename = pmap.get('diagramName');
|
|
368
391
|
if (!filename || !oneline(filename) || /[/\\]/.test(filename))
|
|
@@ -444,7 +467,11 @@ class Converter {
|
|
|
444
467
|
const ph = Converter.NATIVE_PLACEHOLDER_NAME[name];
|
|
445
468
|
const attrs = [];
|
|
446
469
|
let head = '';
|
|
447
|
-
|
|
470
|
+
const nativeParams = name === 'table-excerpt-include'
|
|
471
|
+
? MACRO_EXTRACTORS[name]({ el, paramEls: elements(el.children).filter(p => p.name === 'ac:parameter'), richBody, plainBody })?.params : params;
|
|
472
|
+
if (!nativeParams)
|
|
473
|
+
return null;
|
|
474
|
+
for (const p of nativeParams) {
|
|
448
475
|
const value = /[|{}\n\r]/.test(p.value) || /^"/.test(p.value) ? JSON.stringify(p.value) : p.value;
|
|
449
476
|
if (name === 'jira' && p.name === 'key' && head === '') {
|
|
450
477
|
head = p.value;
|
|
@@ -459,7 +486,7 @@ class Converter {
|
|
|
459
486
|
continue;
|
|
460
487
|
}
|
|
461
488
|
const attrName = name === 'jira' && p.name === 'jqlQuery' ? 'jql' : p.name;
|
|
462
|
-
if (!/^[A-Za-
|
|
489
|
+
if (!/^[A-Za-z_][\w-]*$/.test(attrName))
|
|
463
490
|
return null;
|
|
464
491
|
attrs.push(`${attrName}=${value}`);
|
|
465
492
|
}
|
|
@@ -728,6 +755,36 @@ class Converter {
|
|
|
728
755
|
return false;
|
|
729
756
|
}
|
|
730
757
|
}
|
|
758
|
+
/** Accept editable GFM tables only when the surrounding macros/parameters and normalized cells match. */
|
|
759
|
+
verifyTableMacro(el, markdown) {
|
|
760
|
+
const saved = this.readable, stats = { ...this.stats };
|
|
761
|
+
try {
|
|
762
|
+
this.readable = true;
|
|
763
|
+
const render = (md) => processMacros(renderToStorage(md, { images: new Map(), files: new Map() }, { imageStyle: 'attachment', fileStyle: 'attachment', linkify: false }), this.registry).toString();
|
|
764
|
+
const normalize = (nodes) => nodes.flatMap(node => {
|
|
765
|
+
if (node.kind !== 'el')
|
|
766
|
+
return [node];
|
|
767
|
+
if (node.name === 'table') {
|
|
768
|
+
// Nested block macros require their own faithful conversion; never flatten their semantics.
|
|
769
|
+
const hasMacro = (ns) => ns.some(n => n.kind === 'el' && (n.name === 'ac:structured-macro' || hasMacro(n.children)));
|
|
770
|
+
if (hasMacro(node.children))
|
|
771
|
+
throw new Unrepresentable();
|
|
772
|
+
return parseStorage(render(this.readableTable(node)));
|
|
773
|
+
}
|
|
774
|
+
return [{ ...node, children: normalize(node.children) }];
|
|
775
|
+
});
|
|
776
|
+
return compareStorage(serializeStorage(normalize([el])), serializeStorage(normalize(parseStorage(render(markdown))))).equal;
|
|
777
|
+
}
|
|
778
|
+
catch (error) {
|
|
779
|
+
if (!(error instanceof Unrepresentable))
|
|
780
|
+
throw error;
|
|
781
|
+
return false;
|
|
782
|
+
}
|
|
783
|
+
finally {
|
|
784
|
+
this.readable = saved;
|
|
785
|
+
Object.assign(this.stats, stats);
|
|
786
|
+
}
|
|
787
|
+
}
|
|
731
788
|
containsMacroNamed(nodes, name) {
|
|
732
789
|
for (const n of nodes) {
|
|
733
790
|
if (n.kind !== 'el')
|
|
@@ -859,6 +916,33 @@ class Converter {
|
|
|
859
916
|
const row = (cells) => `| ${cells.join(' | ')} |`;
|
|
860
917
|
return [row(header), row(sep), ...bodyRows.map(row)].join('\n');
|
|
861
918
|
}
|
|
919
|
+
/** Only paragraphs with representable inline content; complex Notes keep storage. */
|
|
920
|
+
inlineNoteMd(el) {
|
|
921
|
+
const parts = this.macroParts(el);
|
|
922
|
+
if (!parts?.richBody || parts.plainBody || parts.params.some(p => !['title', 'icon'].includes(p.name)))
|
|
923
|
+
return null;
|
|
924
|
+
const readable = this.readable;
|
|
925
|
+
this.readable = false;
|
|
926
|
+
try {
|
|
927
|
+
const nodes = parts.richBody.children.filter(n => n.kind !== 'text' || n.raw.trim());
|
|
928
|
+
if (!nodes.length || nodes.some(n => n.kind !== 'el' || n.name !== 'p' || n.attrs.length))
|
|
929
|
+
return null;
|
|
930
|
+
const text = nodes.map(n => this.inlineToMd(n.children, { cell: true })).join('\n');
|
|
931
|
+
const candidate = inlineNote(text, parts.params.map(p => [p.name, p.value]));
|
|
932
|
+
return this.verifyMacroMarker(el, candidate) ? candidate : null;
|
|
933
|
+
}
|
|
934
|
+
catch (e) {
|
|
935
|
+
if (!(e instanceof Unrepresentable))
|
|
936
|
+
throw e;
|
|
937
|
+
return null;
|
|
938
|
+
}
|
|
939
|
+
finally {
|
|
940
|
+
this.readable = readable;
|
|
941
|
+
}
|
|
942
|
+
}
|
|
943
|
+
hasComplexNote(nodes) {
|
|
944
|
+
return nodes.some(n => n.kind === 'el' && ((n.name === 'ac:structured-macro' && getAttr(n, 'ac:name') === 'note' && this.inlineNoteMd(n) === null) || this.hasComplexNote(n.children)));
|
|
945
|
+
}
|
|
862
946
|
cellMd(cell) {
|
|
863
947
|
// Единственный <p> внутри ячейки — разворачиваем (типичная форма).
|
|
864
948
|
let content = cell.children;
|
|
@@ -867,7 +951,8 @@ class Converter {
|
|
|
867
951
|
content.every((n) => n.kind !== 'text' || /^[ \t\r\n]*$/.test(n.raw))) {
|
|
868
952
|
content = els[0].children;
|
|
869
953
|
}
|
|
870
|
-
const
|
|
954
|
+
const hasNote = content.some(n => n.kind === 'el' && n.name === 'ac:structured-macro' && getAttr(n, 'ac:name') === 'note');
|
|
955
|
+
const md = (hasNote ? content.map(n => n.kind === 'el' && n.name === 'p' && !n.attrs.length ? this.inlineToMd(n.children, { cell: true }) : this.inlineToMd([n], { cell: true })).join('') : this.inlineToMd(content, { cell: true })).trim();
|
|
871
956
|
if (md.includes('\n'))
|
|
872
957
|
throw new Unrepresentable();
|
|
873
958
|
// Пайпы экранируем один раз над всей ячейкой — покрывает и текст, и
|
|
@@ -1189,6 +1274,20 @@ class Converter {
|
|
|
1189
1274
|
}
|
|
1190
1275
|
return this.readable ? escapeMdText(alt, ctx, true) : this.rawInline(el, ctx);
|
|
1191
1276
|
}
|
|
1277
|
+
case 'ac:structured-macro': {
|
|
1278
|
+
if (getAttr(el, 'ac:name') === 'note') {
|
|
1279
|
+
const note = this.inlineNoteMd(el);
|
|
1280
|
+
if (note !== null)
|
|
1281
|
+
return note;
|
|
1282
|
+
throw new Unrepresentable();
|
|
1283
|
+
}
|
|
1284
|
+
const native = this.tryNativeMd(el, getAttr(el, 'ac:name') ?? '');
|
|
1285
|
+
if (native !== null && !native.includes('\n'))
|
|
1286
|
+
return native;
|
|
1287
|
+
if (this.readable)
|
|
1288
|
+
return escapeMdText(textContent(el.children), ctx, true);
|
|
1289
|
+
throw new Unrepresentable();
|
|
1290
|
+
}
|
|
1192
1291
|
case 'ac:image':
|
|
1193
1292
|
return this.acImageToMd(el);
|
|
1194
1293
|
case 'ac:link':
|
|
@@ -1237,6 +1336,14 @@ class Converter {
|
|
|
1237
1336
|
const href = getAttr(el, 'href') ?? '';
|
|
1238
1337
|
const inner = this.inlineToMd(el.children, ctx);
|
|
1239
1338
|
const onlyHref = el.attrs.length === 1 && el.attrs[0][0] === 'href';
|
|
1339
|
+
const children = elements(el.children);
|
|
1340
|
+
if (onlyHref && children.length === 1 && children[0].name === 'ac:image' && el.children.every(n => n.kind !== 'text' || !n.raw.trim())) {
|
|
1341
|
+
const image = readInlineSyntax(this.acImageToMd(children[0]), 0);
|
|
1342
|
+
if (image && ['img', 'structurizr', 'drawio'].includes(image.id)) {
|
|
1343
|
+
const ref = parsePlaceholder(image.body);
|
|
1344
|
+
return placeholder(image.id, ref.name, [...ref.attrs, ['link', href]]);
|
|
1345
|
+
}
|
|
1346
|
+
}
|
|
1240
1347
|
if ((onlyHref || this.readable) && SAFE_URL_RE.test(href) && !/[[\]]/.test(inner)) {
|
|
1241
1348
|
return `[${inner}](${href})`; // readable: доп. атрибуты ссылки отбрасываются
|
|
1242
1349
|
}
|
|
@@ -1257,7 +1364,10 @@ class Converter {
|
|
|
1257
1364
|
this.files.add(architecture[1]);
|
|
1258
1365
|
this.files.add(architecture[1].replace(/\.dsl$/, '.json'));
|
|
1259
1366
|
this.images.add(filename);
|
|
1260
|
-
|
|
1367
|
+
const attrs = el.attrs.map(([key]) => [key.slice(3), getAttr(el, key) ?? '']);
|
|
1368
|
+
if (el.attrs.some(([key]) => !key.startsWith('ac:')))
|
|
1369
|
+
throw new Unrepresentable();
|
|
1370
|
+
return placeholder('structurizr', architecture[1], [['view', view], ...attrs.filter(([key, value]) => key !== 'alt' || value !== `Structurizr · ${view}`)]);
|
|
1261
1371
|
}
|
|
1262
1372
|
}
|
|
1263
1373
|
const stem = filename.replace(/\.png$/i, '');
|
|
@@ -1268,7 +1378,10 @@ class Converter {
|
|
|
1268
1378
|
if (preview && !/[{}|\n/\\]/.test(preview)) {
|
|
1269
1379
|
this.files.add(drawioName);
|
|
1270
1380
|
this.images.add(preview);
|
|
1271
|
-
|
|
1381
|
+
if (el.attrs.some(([key]) => !key.startsWith('ac:')))
|
|
1382
|
+
throw new Unrepresentable();
|
|
1383
|
+
const attrs = el.attrs.map(([key]) => [key.slice(3), getAttr(el, key) ?? '']);
|
|
1384
|
+
return placeholder('drawio', drawioName, [['format', 'image'], ...(preview !== `${drawioName}.png` ? [['preview', preview]] : []), ...attrs]);
|
|
1272
1385
|
}
|
|
1273
1386
|
}
|
|
1274
1387
|
// attachments:'local' — картинка как HTML <img> (сохраняет размеры).
|
|
@@ -1325,12 +1438,9 @@ class Converter {
|
|
|
1325
1438
|
throw new Unrepresentable();
|
|
1326
1439
|
attrs.push([k.slice(3), getAttr(el, k) ?? '']);
|
|
1327
1440
|
}
|
|
1328
|
-
for (const [, v] of attrs)
|
|
1329
|
-
badPlaceholderPart(v);
|
|
1330
1441
|
badPlaceholderPart(filename);
|
|
1331
1442
|
this.images.add(filename);
|
|
1332
|
-
|
|
1333
|
-
return `{{img:${filename}${attrStr}}}`;
|
|
1443
|
+
return placeholder('img', filename, attrs);
|
|
1334
1444
|
}
|
|
1335
1445
|
acLinkToMd(el) {
|
|
1336
1446
|
// readable безотказен: непредставимая ссылка (user-mention, ссылка без цели,
|
|
@@ -1366,6 +1476,12 @@ class Converter {
|
|
|
1366
1476
|
throw new Unrepresentable();
|
|
1367
1477
|
}
|
|
1368
1478
|
const textAttr = text !== undefined ? `|text=${text}` : '';
|
|
1479
|
+
if (ref.name === 'ri:user') {
|
|
1480
|
+
const identity = ref.attrs.find(([key]) => ['ri:userkey', 'ri:username', 'ri:account-id'].includes(key));
|
|
1481
|
+
if (!identity || ref.attrs.length !== 1)
|
|
1482
|
+
throw new Unrepresentable();
|
|
1483
|
+
return placeholder('user', decodeEntities(identity[1]), [['type', identity[0].slice(3)], ...(text === undefined ? [] : [['text', text]])]);
|
|
1484
|
+
}
|
|
1369
1485
|
if (ref.name === 'ri:page') {
|
|
1370
1486
|
if (!ref.attrs.every(([k]) => ['ri:content-title', 'ri:space-key', 'ri:version-at-save'].includes(k))) {
|
|
1371
1487
|
throw new Unrepresentable();
|
|
@@ -1384,6 +1500,13 @@ class Converter {
|
|
|
1384
1500
|
}
|
|
1385
1501
|
const filename = getAttr(ref, 'ri:filename') ?? '';
|
|
1386
1502
|
this.files.add(filename);
|
|
1503
|
+
if (!this.localFiles && !text && /\.dsl\.([0-9a-f]{2})+\.png$/.test(filename)) {
|
|
1504
|
+
const image = readInlineSyntax(this.acImageToMd({ kind: 'el', name: 'ac:image', attrs: [], children: [ref], selfClosing: false }), 0);
|
|
1505
|
+
if (image?.id === 'structurizr') {
|
|
1506
|
+
const parsed = parsePlaceholder(image.body);
|
|
1507
|
+
return placeholder('structurizr', parsed.name, [...parsed.attrs, ['display', 'link']]);
|
|
1508
|
+
}
|
|
1509
|
+
}
|
|
1387
1510
|
// attachments:'local' — md-ссылка на локальный файл вместо {{file:}}.
|
|
1388
1511
|
if (this.localFiles) {
|
|
1389
1512
|
const label = (text ?? filename).replace(/[[\]]/g, '\\$&');
|
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
import { macro } from '../builder.js';
|
|
8
8
|
import { paramMap } from '../types.js';
|
|
9
9
|
import { pageLinkValue, structuredMacro } from '../xml.js';
|
|
10
|
+
import { noteMacro } from './note.js';
|
|
10
11
|
/** Rich-body macro that forwards the listed params (if present). */
|
|
11
12
|
function richBodyMacro(name, allowedParams) {
|
|
12
13
|
return {
|
|
@@ -103,7 +104,7 @@ export const coreMacrosPlugin = {
|
|
|
103
104
|
macros: [
|
|
104
105
|
// Rich-body containers
|
|
105
106
|
richBodyMacro('expand', ['title']),
|
|
106
|
-
|
|
107
|
+
noteMacro,
|
|
107
108
|
richBodyMacro('info', ['title', 'icon']),
|
|
108
109
|
richBodyMacro('warning', ['title', 'icon']),
|
|
109
110
|
richBodyMacro('tip', ['title', 'icon']),
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { macro } from '../builder.js';
|
|
2
2
|
import { paramMap } from '../types.js';
|
|
3
|
-
import {
|
|
3
|
+
import { structuredMacro } from '../xml.js';
|
|
4
|
+
import { attachmentImage } from '../../markdown/image.js';
|
|
4
5
|
/** XML keeps its attachment name, including extensionless Confluence diagrams. */
|
|
5
6
|
export const drawioPlugin = {
|
|
6
7
|
name: 'drawio',
|
|
@@ -10,7 +11,7 @@ export const drawioPlugin = {
|
|
|
10
11
|
if (!name)
|
|
11
12
|
throw new Error('drawio: diagramName is required');
|
|
12
13
|
if (params.format === 'image') {
|
|
13
|
-
return
|
|
14
|
+
return attachmentImage(params.preview || `${name}.png`, ctx.params.filter(p => !['diagramName', 'format', 'preview'].includes(p.name)).map(p => [p.name, p.value]));
|
|
14
15
|
}
|
|
15
16
|
if (params.format && params.format !== 'macro')
|
|
16
17
|
throw new Error('drawio: format must be macro or image');
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { parseStorage, serializeStorage } from '../../export/xhtml.js';
|
|
2
|
+
import { stableMacroId, structuredMacro } from '../xml.js';
|
|
3
|
+
export const noteMacro = {
|
|
4
|
+
name: 'note',
|
|
5
|
+
render: ctx => structuredMacro('note', ctx.macroId, { params: ctx.params.filter(p => ['title', 'icon'].includes(p.name)), richBody: ctx.body }),
|
|
6
|
+
};
|
|
7
|
+
export function renderInlineNote(body, attrs) {
|
|
8
|
+
body = serializeStorage(parseStorage(body));
|
|
9
|
+
const params = attrs.map(([name, value]) => ({ name, value })).sort((a, b) => a.name.localeCompare(b.name));
|
|
10
|
+
if (params.some(p => !['title', 'icon'].includes(p.name)))
|
|
11
|
+
throw new Error('Inline Note supports title and icon parameters');
|
|
12
|
+
return noteMacro.render({ macroId: stableMacroId(JSON.stringify([body, params])), params, body });
|
|
13
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { macro } from '../builder.js';
|
|
2
2
|
import { paramMap } from '../types.js';
|
|
3
|
-
import {
|
|
3
|
+
import { attachmentImage } from '../../markdown/image.js';
|
|
4
4
|
export function structurizrPreview(name, view) {
|
|
5
5
|
if (typeof name !== 'string' || !/\.dsl$/i.test(name) || /[/\\{}|]/.test(name) || !view || view.length > 120 || /[{}|\n\r]/.test(view))
|
|
6
6
|
throw new Error('structurizr: a .dsl attachment and view key are required');
|
|
@@ -10,8 +10,8 @@ export function structurizrPreview(name, view) {
|
|
|
10
10
|
export const structurizrPlugin = {
|
|
11
11
|
name: 'structurizr',
|
|
12
12
|
macros: [{ name: 'structurizr', render(ctx) {
|
|
13
|
-
const { name, view } = paramMap(ctx.params);
|
|
14
|
-
return
|
|
13
|
+
const { name, view, ...attrs } = paramMap(ctx.params);
|
|
14
|
+
return attachmentImage(structurizrPreview(name, view), Object.entries({ alt: `Structurizr · ${view}`, ...attrs }));
|
|
15
15
|
} }],
|
|
16
16
|
};
|
|
17
17
|
export const structurizr = (name, view) => macro('structurizr').param('name', name).param('view', view).toMarkdown();
|
|
@@ -63,13 +63,7 @@ export const tableFilterPlugin = {
|
|
|
63
63
|
// Параметры: name — название выборки; hide — скрывать ли содержимое.
|
|
64
64
|
name: 'table-excerpt',
|
|
65
65
|
render: (ctx) => {
|
|
66
|
-
|
|
67
|
-
const params = [];
|
|
68
|
-
if (map.hide === 'true')
|
|
69
|
-
params.push({ name: 'hide', value: 'true' });
|
|
70
|
-
if (map.name)
|
|
71
|
-
params.push({ name: 'name', value: map.name });
|
|
72
|
-
return structuredMacro('table-excerpt', ctx.macroId, { params, richBody: ctx.body });
|
|
66
|
+
return structuredMacro('table-excerpt', ctx.macroId, { params: ctx.params, richBody: ctx.body });
|
|
73
67
|
},
|
|
74
68
|
},
|
|
75
69
|
{
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { escapeXmlAttr } from '../macros/xml.js';
|
|
2
|
+
/** Native attachment images retain Confluence's image toolbar and formatting. */
|
|
3
|
+
export function attachmentImage(name, attrs) {
|
|
4
|
+
const values = new Map(attrs);
|
|
5
|
+
const attachment = `<ri:attachment ri:filename="${escapeXmlAttr(name)}" />`;
|
|
6
|
+
if (values.get('display') === 'link')
|
|
7
|
+
return `<ac:link>${attachment}</ac:link>`;
|
|
8
|
+
const attributes = attrs.filter(([key]) => !['display', 'link'].includes(key));
|
|
9
|
+
for (const [key] of attributes)
|
|
10
|
+
if (!/^[A-Za-z_][\w.-]*$/.test(key))
|
|
11
|
+
throw new Error(`Invalid image attribute: ${key}`);
|
|
12
|
+
const image = `<ac:image${attributes.map(([key, value]) => ` ac:${key}="${escapeXmlAttr(value)}"`).join('')}>${attachment}</ac:image>`;
|
|
13
|
+
const link = values.get('link');
|
|
14
|
+
if (!link)
|
|
15
|
+
return image;
|
|
16
|
+
if (/^[a-z][a-z0-9+.-]*:/i.test(link) && !/^(https?|mailto|tel):/i.test(link))
|
|
17
|
+
throw new Error('Unsupported image link protocol');
|
|
18
|
+
return `<a href="${escapeXmlAttr(link)}">${image}</a>`;
|
|
19
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export interface InlineSyntax {
|
|
2
|
+
id: string;
|
|
3
|
+
body: string;
|
|
4
|
+
raw: string;
|
|
5
|
+
from: number;
|
|
6
|
+
to: number;
|
|
7
|
+
}
|
|
8
|
+
/** Balanced braces, quoted parameters and escaped delimiters. */
|
|
9
|
+
export declare function readInlineSyntax(source: string, from: number): InlineSyntax | null;
|
|
10
|
+
/** Visit authored macros, excluding fenced and inline code. */
|
|
11
|
+
export declare function replaceInlineSyntax(source: string, replace: (macro: InlineSyntax) => string | undefined): string;
|
|
12
|
+
/** Remove the one GFM escaping layer; keep JSON-encoded backslashes intact. */
|
|
13
|
+
export declare const unescapeTablePipes: (raw: string) => string;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/** Balanced braces, quoted parameters and escaped delimiters. */
|
|
2
|
+
export function readInlineSyntax(source, from) {
|
|
3
|
+
const head = /^\{\{([a-z][a-z0-9-]*)(:|\}\})/.exec(source.slice(from));
|
|
4
|
+
if (!head)
|
|
5
|
+
return null;
|
|
6
|
+
if (head[2] === '}}')
|
|
7
|
+
return { id: head[1], body: '', raw: head[0], from, to: from + head[0].length };
|
|
8
|
+
const start = from + head[0].length;
|
|
9
|
+
let depth = 1, quoted = false;
|
|
10
|
+
for (let i = start; i < source.length; i++) {
|
|
11
|
+
if (source[i] === '\\') {
|
|
12
|
+
i++;
|
|
13
|
+
continue;
|
|
14
|
+
}
|
|
15
|
+
if (source[i] === '"') {
|
|
16
|
+
quoted = !quoted;
|
|
17
|
+
continue;
|
|
18
|
+
}
|
|
19
|
+
if (quoted)
|
|
20
|
+
continue;
|
|
21
|
+
if (source.slice(i, i + 2) === '{{') {
|
|
22
|
+
depth++;
|
|
23
|
+
i++;
|
|
24
|
+
}
|
|
25
|
+
else if (source.slice(i, i + 2) === '}}') {
|
|
26
|
+
if (--depth === 0)
|
|
27
|
+
return { id: head[1], body: source.slice(start, i), raw: source.slice(from, i + 2), from, to: i + 2 };
|
|
28
|
+
i++;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
/** Visit authored macros, excluding fenced and inline code. */
|
|
34
|
+
export function replaceInlineSyntax(source, replace) {
|
|
35
|
+
let out = '', pos = 0;
|
|
36
|
+
while (pos < source.length) {
|
|
37
|
+
if (pos === 0 || source[pos - 1] === '\n') {
|
|
38
|
+
const fence = /^ {0,3}(`{3,}|~{3,})[^\n]*(?:\n|$)/.exec(source.slice(pos));
|
|
39
|
+
if (fence) {
|
|
40
|
+
const end = new RegExp(`^ {0,3}${fence[1][0]}{${fence[1].length},}[ \\t]*(?:\\n|$)`, 'm');
|
|
41
|
+
const rest = source.slice(pos + fence[0].length), close = end.exec(rest);
|
|
42
|
+
const to = close ? pos + fence[0].length + close.index + close[0].length : source.length;
|
|
43
|
+
out += source.slice(pos, to);
|
|
44
|
+
pos = to;
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
if (source[pos] === '\\') {
|
|
49
|
+
out += source.slice(pos, pos + 2);
|
|
50
|
+
pos += 2;
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
if (source[pos] === '`') {
|
|
54
|
+
const run = /^`+/.exec(source.slice(pos))[0], end = source.indexOf(run, pos + run.length);
|
|
55
|
+
if (end >= 0) {
|
|
56
|
+
out += source.slice(pos, end + run.length);
|
|
57
|
+
pos = end + run.length;
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
const macro = source.slice(pos, pos + 2) === '{{' ? readInlineSyntax(source, pos) : null;
|
|
62
|
+
if (macro) {
|
|
63
|
+
out += replace(macro) ?? macro.raw;
|
|
64
|
+
pos = macro.to;
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
out += source[pos++];
|
|
68
|
+
}
|
|
69
|
+
return out;
|
|
70
|
+
}
|
|
71
|
+
/** Remove the one GFM escaping layer; keep JSON-encoded backslashes intact. */
|
|
72
|
+
export const unescapeTablePipes = (raw) => raw.replace(/(\\+)\|/g, (_match, slash) => (slash.length % 2 ? slash.slice(1) : slash) + '|');
|
package/dist/markdown/native.js
CHANGED
|
@@ -30,6 +30,7 @@
|
|
|
30
30
|
* Внутри fenced-код-блоков (``` / ~~~) синтаксис не интерпретируется.
|
|
31
31
|
*/
|
|
32
32
|
import { escapeParamValue } from '../macros/builder.js';
|
|
33
|
+
import { replaceInlineSyntax } from './inline-syntax.js';
|
|
33
34
|
/** Панели: тег admonition → имя макроса. */
|
|
34
35
|
export const NATIVE_ADMONITIONS = {
|
|
35
36
|
INFO: 'info',
|
|
@@ -42,6 +43,9 @@ export const NATIVE_DIRECTIVES = {
|
|
|
42
43
|
properties: 'details',
|
|
43
44
|
expand: 'expand',
|
|
44
45
|
panel: 'panel',
|
|
46
|
+
'table-filter': 'table-filter',
|
|
47
|
+
'table-excerpt': 'table-excerpt',
|
|
48
|
+
'table-joiner': 'table-joiner',
|
|
45
49
|
};
|
|
46
50
|
/** Строчные плейсхолдеры: имя → имя макроса. */
|
|
47
51
|
export const NATIVE_PLACEHOLDERS = {
|
|
@@ -55,6 +59,7 @@ export const NATIVE_PLACEHOLDERS = {
|
|
|
55
59
|
anchor: 'anchor',
|
|
56
60
|
'properties-report': 'detailssummary',
|
|
57
61
|
'portfolio-for-jira-plan': 'portfolio-for-jira-plan',
|
|
62
|
+
'table-excerpt-include': 'table-excerpt-include',
|
|
58
63
|
};
|
|
59
64
|
/** Полный перечень макросов с нативной md-разметкой (для документации/UI). */
|
|
60
65
|
export function nativeMacroList() {
|
|
@@ -63,12 +68,14 @@ export function nativeMacroList() {
|
|
|
63
68
|
{ macro: 'structurizr', syntax: '{{structurizr:workspace.dsl|view=containers}}' },
|
|
64
69
|
{ macro: 'drawio', syntax: '{{drawio:diagram.drawio|format=macro}} | {{drawio:diagram.drawio|format=image}}' },
|
|
65
70
|
{ macro: 'info', syntax: '> [!INFO] Заголовок?' },
|
|
66
|
-
{ macro: 'note', syntax: '> [!NOTE] Заголовок?' },
|
|
71
|
+
{ macro: 'note', syntax: '> [!NOTE] Заголовок? | {{note:Текст}}' },
|
|
67
72
|
{ macro: 'warning', syntax: '> [!WARNING] Заголовок?' },
|
|
68
73
|
{ macro: 'tip', syntax: '> [!TIP] Заголовок?' },
|
|
69
74
|
{ macro: 'details', syntax: '::: properties [id=…] [hidden=true] … :::' },
|
|
70
75
|
{ macro: 'expand', syntax: '::: expand Заголовок … :::' },
|
|
71
76
|
{ macro: 'panel', syntax: '::: panel title=… … :::' },
|
|
77
|
+
...['table-filter', 'table-excerpt', 'table-joiner'].map(macro => ({ macro, syntax: `::: ${macro} name="…" … :::` })),
|
|
78
|
+
{ macro: 'table-excerpt-include', syntax: '{{table-excerpt-include:name=…|page=…|space=…}}' },
|
|
72
79
|
{ macro: 'toc', syntax: '{{toc}} | {{toc:maxLevel=3}}' },
|
|
73
80
|
{ macro: 'children', syntax: '{{children}} | {{children:depth=2}}' },
|
|
74
81
|
{ macro: 'jira', syntax: '{{jira:KEY-1}} | {{jira:jql=…|maximumIssues=20}}' },
|
|
@@ -81,7 +88,6 @@ export function nativeMacroList() {
|
|
|
81
88
|
const ADMONITION_FIRST_RE = /^>\s*\[!([A-Za-z]+)\]\s*(.*)$/;
|
|
82
89
|
const DIRECTIVE_OPEN_RE = /^:::\s+([a-z-]+)(?:\s+(.*?))?\s*$/;
|
|
83
90
|
const DIRECTIVE_CLOSE_RE = /^:::\s*$/;
|
|
84
|
-
const PLACEHOLDER_INLINE_RE = /\{\{(search|drawio|structurizr|toc|children|jira|status|anchor|properties-report|portfolio-for-jira-plan)(?::((?:"(?:\\.|[^"\\])*"|[^{}"]|\{[^{])*?))?\}\}/g;
|
|
85
91
|
const FENCE_RE = /^\s*(`{3,}|~{3,})/;
|
|
86
92
|
/** Компактная пара маркеров без тела — для строчного контекста. */
|
|
87
93
|
function inlineMarker(macroName, params) {
|
|
@@ -174,7 +180,7 @@ function placeholderParams(kind, raw) {
|
|
|
174
180
|
return params;
|
|
175
181
|
}
|
|
176
182
|
function replaceInline(line) {
|
|
177
|
-
return line
|
|
183
|
+
return replaceInlineSyntax(line, ref => ref.id in NATIVE_PLACEHOLDERS ? inlineMarker(NATIVE_PLACEHOLDERS[ref.id], placeholderParams(ref.id, ref.body)) : undefined);
|
|
178
184
|
}
|
|
179
185
|
/** Параметры директивы: токены `k=v` (значение можно в кавычках) + свободный текст → title. */
|
|
180
186
|
function directiveParams(name, rest) {
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function liftInlineNotes(html: string): string;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { getAttr, parseStorage, serializeStorage } from '../export/xhtml.js';
|
|
2
|
+
const isNote = (n) => n.kind === 'el' && n.name === 'ac:structured-macro' && getAttr(n, 'ac:name') === 'note';
|
|
3
|
+
/** Split inline wrappers as well, so a Note never becomes a block inside <p>/<strong>. */
|
|
4
|
+
function split(nodes) {
|
|
5
|
+
const result = [[]];
|
|
6
|
+
for (const n of nodes) {
|
|
7
|
+
if (isNote(n)) {
|
|
8
|
+
result.push([n], []);
|
|
9
|
+
continue;
|
|
10
|
+
}
|
|
11
|
+
if (n.kind !== 'el') {
|
|
12
|
+
result[result.length - 1].push(n);
|
|
13
|
+
continue;
|
|
14
|
+
}
|
|
15
|
+
const inner = split(n.children);
|
|
16
|
+
for (const chunk of inner) {
|
|
17
|
+
if (chunk.length === 1 && isNote(chunk[0]))
|
|
18
|
+
result.push(chunk, []);
|
|
19
|
+
else if (chunk.length)
|
|
20
|
+
result[result.length - 1].push({ ...n, children: chunk });
|
|
21
|
+
}
|
|
22
|
+
if (!n.children.length)
|
|
23
|
+
result[result.length - 1].push(n);
|
|
24
|
+
}
|
|
25
|
+
return result.filter(chunk => chunk.length);
|
|
26
|
+
}
|
|
27
|
+
export function liftInlineNotes(html) {
|
|
28
|
+
const walk = (nodes) => nodes.flatMap(n => {
|
|
29
|
+
if (n.kind !== 'el')
|
|
30
|
+
return [n];
|
|
31
|
+
const children = walk(n.children);
|
|
32
|
+
if (['td', 'th'].includes(n.name) && children.some(isNote)) {
|
|
33
|
+
const blocks = split(children).flatMap(chunk => chunk.every(n => isNote(n) || (n.kind === 'el' && n.name === 'p')) ? chunk : [{ kind: 'el', name: 'p', attrs: [], children: chunk, selfClosing: false }]);
|
|
34
|
+
return [{ ...n, children: blocks }];
|
|
35
|
+
}
|
|
36
|
+
if (n.name !== 'p')
|
|
37
|
+
return [{ ...n, children }];
|
|
38
|
+
return split(children).map(chunk => chunk.length === 1 && isNote(chunk[0]) ? chunk[0] : { ...n, children: chunk });
|
|
39
|
+
});
|
|
40
|
+
return serializeStorage(walk(parseStorage(html)));
|
|
41
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/** Plain body is Markdown; quoted body permits parameters and a single-line table cell. */
|
|
2
|
+
export declare function parseInlineNote(body: string): {
|
|
3
|
+
text: string;
|
|
4
|
+
attrs: [string, string][];
|
|
5
|
+
};
|
|
6
|
+
export declare function inlineNote(text: string, attrs?: [string, string][]): string;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { parsePlaceholder, quotePlaceholder } from './placeholder.js';
|
|
2
|
+
/** Plain body is Markdown; quoted body permits parameters and a single-line table cell. */
|
|
3
|
+
export function parseInlineNote(body) {
|
|
4
|
+
if (body.startsWith('"')) {
|
|
5
|
+
const ref = parsePlaceholder(body);
|
|
6
|
+
return { text: ref.name, attrs: ref.attrs };
|
|
7
|
+
}
|
|
8
|
+
let text = '';
|
|
9
|
+
for (let i = 0; i < body.length; i++) {
|
|
10
|
+
if (body[i] === '\\' && body[i + 1] === 'n') {
|
|
11
|
+
text += '\n';
|
|
12
|
+
i++;
|
|
13
|
+
}
|
|
14
|
+
else if (body[i] === '\\' && body[i + 1] === '\\') {
|
|
15
|
+
text += '\\\\';
|
|
16
|
+
i++;
|
|
17
|
+
}
|
|
18
|
+
else
|
|
19
|
+
text += body[i];
|
|
20
|
+
}
|
|
21
|
+
return { text, attrs: [] };
|
|
22
|
+
}
|
|
23
|
+
export function inlineNote(text, attrs = []) {
|
|
24
|
+
// JSON quotes avoid confusing nested macro pipes with Note parameters.
|
|
25
|
+
return `{{note:${JSON.stringify(text)}${attrs.map(([key, value]) => `|${key}=${quotePlaceholder(value)}`).join('')}}}`;
|
|
26
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export interface PlaceholderRef {
|
|
2
|
+
name: string;
|
|
3
|
+
attrs: Array<[string, string]>;
|
|
4
|
+
}
|
|
5
|
+
export declare const PLACEHOLDER_RE: RegExp;
|
|
6
|
+
export declare const quotePlaceholder: (value: string) => string;
|
|
7
|
+
export declare function parsePlaceholder(body: string): PlaceholderRef;
|
|
8
|
+
export declare function placeholder(kind: string, name: string, attrs?: Array<[string, string]>): string;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export const PLACEHOLDER_RE = /\{\{(img|file|page):((?:"(?:\\.|[^"\\])*"|[^{}"]|\{[^{])*?)\}\}/g;
|
|
2
|
+
const unquote = (value) => {
|
|
3
|
+
if (value.startsWith('"') && value.endsWith('"')) {
|
|
4
|
+
try {
|
|
5
|
+
return JSON.parse(value);
|
|
6
|
+
}
|
|
7
|
+
catch { /* Legacy values remain literal. */ }
|
|
8
|
+
}
|
|
9
|
+
return value;
|
|
10
|
+
};
|
|
11
|
+
export const quotePlaceholder = (value) => /[|{}"\\\r\n]/.test(value) ? JSON.stringify(value) : value;
|
|
12
|
+
export function parsePlaceholder(body) {
|
|
13
|
+
const parts = body.match(/(?:"(?:\\.|[^"\\])*"|[^|"])+/g) ?? [];
|
|
14
|
+
const name = unquote((parts.shift() ?? '').trim());
|
|
15
|
+
const attrs = parts.map(part => {
|
|
16
|
+
const eq = part.indexOf('=');
|
|
17
|
+
return eq < 0 ? [part.trim(), ''] : [part.slice(0, eq).trim(), unquote(part.slice(eq + 1).trim())];
|
|
18
|
+
});
|
|
19
|
+
return { name, attrs };
|
|
20
|
+
}
|
|
21
|
+
export function placeholder(kind, name, attrs = []) {
|
|
22
|
+
return `{{${kind}:${quotePlaceholder(name)}${attrs.map(([key, value]) => `|${key}=${quotePlaceholder(value)}`).join('')}}}`;
|
|
23
|
+
}
|
|
@@ -1,18 +1,10 @@
|
|
|
1
|
+
export { PLACEHOLDER_RE, parsePlaceholder, type PlaceholderRef } from './placeholder.js';
|
|
1
2
|
/**
|
|
2
3
|
* Плейсхолдеры: `{{img:name}}`, `{{file:name}}`, `{{page:Title}}`.
|
|
3
4
|
* После имени допустимы `|key=value`-атрибуты:
|
|
4
5
|
* {{img:chart.png|thumbnail=true|height=250}}
|
|
5
6
|
* {{page:Другая страница|space=DOCS|text=якорный текст}}
|
|
6
7
|
*/
|
|
7
|
-
export declare const PLACEHOLDER_RE: RegExp;
|
|
8
|
-
export interface PlaceholderRef {
|
|
9
|
-
/** Имя файла (img/file) или title страницы (page). */
|
|
10
|
-
name: string;
|
|
11
|
-
/** `|key=value`-атрибуты в порядке записи. */
|
|
12
|
-
attrs: Array<[string, string]>;
|
|
13
|
-
}
|
|
14
|
-
/** Разбирает содержимое плейсхолдера: `name|k=v|k2=v2`. */
|
|
15
|
-
export declare function parsePlaceholder(body: string): PlaceholderRef;
|
|
16
8
|
export interface ExtractedPlaceholders {
|
|
17
9
|
images: string[];
|
|
18
10
|
files: string[];
|
|
@@ -67,24 +59,5 @@ export interface RenderStorageOptions {
|
|
|
67
59
|
export declare class MissingAttachmentUrlError extends Error {
|
|
68
60
|
constructor(message: string);
|
|
69
61
|
}
|
|
70
|
-
/**
|
|
71
|
-
* Превращает Markdown в Confluence storage format.
|
|
72
|
-
*
|
|
73
|
-
* Логика двушаговая:
|
|
74
|
-
* 1) markdown-it рендерит сам Markdown (без знания про плейсхолдеры).
|
|
75
|
-
* `{{img:foo}}` и `{{file:foo}}` для markdown-it — обычный текст
|
|
76
|
-
* без спецсимволов, доходят до выхода нетронутыми (могут быть
|
|
77
|
-
* обёрнуты в <p>...</p>, что нас устраивает).
|
|
78
|
-
* 2) В готовом HTML регуляркой подставляем `<img src=URL/>` и
|
|
79
|
-
* `<a href=URL>filename</a>` на места плейсхолдеров. URL обязан
|
|
80
|
-
* существовать для каждого плейсхолдера — иначе
|
|
81
|
-
* MissingAttachmentUrlError.
|
|
82
|
-
*
|
|
83
|
-
* Раньше использовалась схема через sentinel-токены (` CFLIMG_0 `):
|
|
84
|
-
* markdown-it трогал окружающие пробелы при упаковке в <p>, sentinel
|
|
85
|
-
* терял пробелы, итоговый .split() не находил совпадения и в Confluence
|
|
86
|
-
* вместо картинки попадал кусок «CFLIMG_0». Прямая регекс-замена по
|
|
87
|
-
* HTML этой проблемы лишена — `{{img:foo}}` мимо markdown-it проходит
|
|
88
|
-
* посимвольно.
|
|
89
|
-
*/
|
|
62
|
+
/** Render Markdown after shielding authored macros from GFM table/inline parsing. */
|
|
90
63
|
export declare function renderToStorage(markdown: string, urls: AttachmentUrls, opts?: RenderStorageOptions): string;
|
package/dist/markdown/render.js
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
import MarkdownIt from 'markdown-it';
|
|
2
|
-
import { nativeToMarkers } from './native.js';
|
|
2
|
+
import { nativeToMarkers, NATIVE_PLACEHOLDERS } from './native.js';
|
|
3
|
+
import { parsePlaceholder, placeholder } from './placeholder.js';
|
|
4
|
+
import { attachmentImage } from './image.js';
|
|
5
|
+
import { replaceInlineSyntax, unescapeTablePipes } from './inline-syntax.js';
|
|
6
|
+
import { parseInlineNote } from './note.js';
|
|
7
|
+
import { renderInlineNote } from '../macros/plugins/note.js';
|
|
8
|
+
import { liftInlineNotes } from './note-layout.js';
|
|
9
|
+
export { PLACEHOLDER_RE, parsePlaceholder } from './placeholder.js';
|
|
3
10
|
// xhtmlOut: true — Confluence storage format = XHTML, void-элементы
|
|
4
11
|
// (<hr/>, <br/>, <img/>) обязаны быть самозакрывающимися.
|
|
5
12
|
// html: true — разрешить HTML (нужно для <!-- MACRO:... --> комментариев)
|
|
@@ -15,40 +22,20 @@ const md = new MarkdownIt(mdOptions);
|
|
|
15
22
|
// для страниц из exportPage — иначе round-trip превращал бы нессылочные
|
|
16
23
|
// упоминания URL в <a>.
|
|
17
24
|
const mdNoLinkify = new MarkdownIt({ ...mdOptions, linkify: false });
|
|
18
|
-
/**
|
|
19
|
-
* Плейсхолдеры: `{{img:name}}`, `{{file:name}}`, `{{page:Title}}`.
|
|
20
|
-
* После имени допустимы `|key=value`-атрибуты:
|
|
21
|
-
* {{img:chart.png|thumbnail=true|height=250}}
|
|
22
|
-
* {{page:Другая страница|space=DOCS|text=якорный текст}}
|
|
23
|
-
*/
|
|
24
|
-
export const PLACEHOLDER_RE = /\{\{(img|file|page):([^}]+)\}\}/g;
|
|
25
|
-
/** Разбирает содержимое плейсхолдера: `name|k=v|k2=v2`. */
|
|
26
|
-
export function parsePlaceholder(body) {
|
|
27
|
-
const parts = body.split('|');
|
|
28
|
-
const name = parts[0].trim();
|
|
29
|
-
const attrs = [];
|
|
30
|
-
for (const part of parts.slice(1)) {
|
|
31
|
-
const eq = part.indexOf('=');
|
|
32
|
-
if (eq === -1)
|
|
33
|
-
attrs.push([part.trim(), '']);
|
|
34
|
-
else
|
|
35
|
-
attrs.push([part.slice(0, eq).trim(), part.slice(eq + 1).trim()]);
|
|
36
|
-
}
|
|
37
|
-
return { name, attrs };
|
|
38
|
-
}
|
|
39
25
|
export function extractPlaceholders(markdown) {
|
|
40
26
|
const images = new Set();
|
|
41
27
|
const files = new Set();
|
|
42
28
|
const pages = new Set();
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
files.add(name);
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
}
|
|
29
|
+
const collect = (source) => replaceInlineSyntax(source, ref => {
|
|
30
|
+
if (ref.id === 'note')
|
|
31
|
+
collect(parseInlineNote(ref.body).text);
|
|
32
|
+
else if (['img', 'file', 'page'].includes(ref.id)) {
|
|
33
|
+
const { name } = parsePlaceholder(ref.body);
|
|
34
|
+
(ref.id === 'img' ? images : ref.id === 'file' ? files : pages).add(name);
|
|
35
|
+
}
|
|
36
|
+
return undefined;
|
|
37
|
+
});
|
|
38
|
+
collect(markdown);
|
|
52
39
|
return { images: [...images], files: [...files], pages: [...pages] };
|
|
53
40
|
}
|
|
54
41
|
function escapeXmlAttr(s) {
|
|
@@ -71,13 +58,15 @@ function escapeXmlAttr(s) {
|
|
|
71
58
|
export function renameImagePlaceholders(markdown, renames) {
|
|
72
59
|
if (renames.size === 0)
|
|
73
60
|
return markdown;
|
|
74
|
-
return markdown
|
|
75
|
-
|
|
61
|
+
return replaceInlineSyntax(markdown, ref => {
|
|
62
|
+
if (ref.id !== 'img')
|
|
63
|
+
return;
|
|
64
|
+
const full = ref.raw;
|
|
65
|
+
const { name, attrs } = parsePlaceholder(ref.body);
|
|
76
66
|
const renamed = renames.get(name);
|
|
77
67
|
if (!renamed)
|
|
78
68
|
return full;
|
|
79
|
-
|
|
80
|
-
return `{{img:${renamed}${attrStr}}}`;
|
|
69
|
+
return placeholder('img', renamed, attrs);
|
|
81
70
|
});
|
|
82
71
|
}
|
|
83
72
|
export class MissingAttachmentUrlError extends Error {
|
|
@@ -86,32 +75,68 @@ export class MissingAttachmentUrlError extends Error {
|
|
|
86
75
|
this.name = 'MissingAttachmentUrlError';
|
|
87
76
|
}
|
|
88
77
|
}
|
|
89
|
-
/**
|
|
90
|
-
* Превращает Markdown в Confluence storage format.
|
|
91
|
-
*
|
|
92
|
-
* Логика двушаговая:
|
|
93
|
-
* 1) markdown-it рендерит сам Markdown (без знания про плейсхолдеры).
|
|
94
|
-
* `{{img:foo}}` и `{{file:foo}}` для markdown-it — обычный текст
|
|
95
|
-
* без спецсимволов, доходят до выхода нетронутыми (могут быть
|
|
96
|
-
* обёрнуты в <p>...</p>, что нас устраивает).
|
|
97
|
-
* 2) В готовом HTML регуляркой подставляем `<img src=URL/>` и
|
|
98
|
-
* `<a href=URL>filename</a>` на места плейсхолдеров. URL обязан
|
|
99
|
-
* существовать для каждого плейсхолдера — иначе
|
|
100
|
-
* MissingAttachmentUrlError.
|
|
101
|
-
*
|
|
102
|
-
* Раньше использовалась схема через sentinel-токены (` CFLIMG_0 `):
|
|
103
|
-
* markdown-it трогал окружающие пробелы при упаковке в <p>, sentinel
|
|
104
|
-
* терял пробелы, итоговый .split() не находил совпадения и в Confluence
|
|
105
|
-
* вместо картинки попадал кусок «CFLIMG_0». Прямая регекс-замена по
|
|
106
|
-
* HTML этой проблемы лишена — `{{img:foo}}` мимо markdown-it проходит
|
|
107
|
-
* посимвольно.
|
|
108
|
-
*/
|
|
78
|
+
/** Render Markdown after shielding authored macros from GFM table/inline parsing. */
|
|
109
79
|
export function renderToStorage(markdown, urls, opts = {}) {
|
|
110
80
|
// Нативный md-синтаксис макросов (панели, ::: properties, {{toc}} …) —
|
|
111
81
|
// сахар над маркерами: переписываем до markdown-it (см. native.ts).
|
|
82
|
+
const placeholders = [];
|
|
83
|
+
let prefix = 'CFLATTACHTOKEN';
|
|
84
|
+
while (markdown.includes(prefix))
|
|
85
|
+
prefix += 'X';
|
|
86
|
+
markdown = replaceInlineSyntax(markdown, ref => {
|
|
87
|
+
if (!['img', 'file', 'page', 'user', 'note'].includes(ref.id) && !(ref.id in NATIVE_PLACEHOLDERS))
|
|
88
|
+
return;
|
|
89
|
+
const line = markdown.slice(markdown.lastIndexOf('\n', ref.from - 1) + 1, ref.from);
|
|
90
|
+
const cell = line.trimStart().startsWith('|');
|
|
91
|
+
placeholders.push(cell ? { ...ref, raw: unescapeTablePipes(ref.raw), body: unescapeTablePipes(ref.body) } : ref);
|
|
92
|
+
return `${prefix}${placeholders.length - 1}TOKEN`;
|
|
93
|
+
});
|
|
112
94
|
markdown = nativeToMarkers(markdown);
|
|
113
95
|
const renderer = opts.linkify === false ? mdNoLinkify : md;
|
|
114
96
|
let html = renderer.render(markdown);
|
|
97
|
+
const renderPlaceholder = (kind, body) => {
|
|
98
|
+
const { name, attrs } = parsePlaceholder(body);
|
|
99
|
+
if (kind === 'user') {
|
|
100
|
+
const map = Object.fromEntries(attrs), type = map.type || 'userkey';
|
|
101
|
+
if (!['userkey', 'username', 'account-id'].includes(type))
|
|
102
|
+
throw new Error('Unknown Confluence user identity type');
|
|
103
|
+
const body = map.text === undefined ? '' : `<ac:plain-text-link-body><![CDATA[${map.text.replace(/]]>/g, ']]]]><![CDATA[>')}]]></ac:plain-text-link-body>`;
|
|
104
|
+
return `<ac:link><ri:user ri:${type}="${escapeXmlAttr(name)}" />${body}</ac:link>`;
|
|
105
|
+
}
|
|
106
|
+
if (kind === 'page') {
|
|
107
|
+
return renderPageLink(name, attrs);
|
|
108
|
+
}
|
|
109
|
+
if (kind === 'img') {
|
|
110
|
+
if (opts.imageStyle === 'attachment') {
|
|
111
|
+
return attachmentImage(name, attrs);
|
|
112
|
+
}
|
|
113
|
+
const url = urls.images.get(name);
|
|
114
|
+
if (!url) {
|
|
115
|
+
throw new MissingAttachmentUrlError(`No uploaded URL for image '${name}' — was it included in publishPage().images and successfully uploaded?`);
|
|
116
|
+
}
|
|
117
|
+
return `<img src="${escapeXmlAttr(url)}" alt="${escapeXmlAttr(name)}" />`;
|
|
118
|
+
}
|
|
119
|
+
if (opts.fileStyle === 'attachment') {
|
|
120
|
+
const text = attrs.find(([k]) => k === 'text')?.[1];
|
|
121
|
+
const body = text !== undefined ? plainTextLinkBody(text) : '';
|
|
122
|
+
return `<ac:link><ri:attachment ri:filename="${escapeXmlAttr(name)}" />${body}</ac:link>`;
|
|
123
|
+
}
|
|
124
|
+
const url = urls.files.get(name);
|
|
125
|
+
if (!url) {
|
|
126
|
+
throw new MissingAttachmentUrlError(`No uploaded URL for file '${name}' — was it included in publishPage().files and successfully uploaded?`);
|
|
127
|
+
}
|
|
128
|
+
return `<a href="${escapeXmlAttr(url)}">${escapeXmlAttr(name)}</a>`;
|
|
129
|
+
};
|
|
130
|
+
html = html.replace(new RegExp(prefix + '(\\d+)TOKEN', 'g'), (_full, index) => {
|
|
131
|
+
const ref = placeholders[Number(index)];
|
|
132
|
+
if (ref.id === 'note') {
|
|
133
|
+
const note = parseInlineNote(ref.body);
|
|
134
|
+
const body = renderToStorage(note.text.replace(/\r?\n/g, '\n\n'), urls, opts);
|
|
135
|
+
return renderInlineNote(body.trim(), note.attrs);
|
|
136
|
+
}
|
|
137
|
+
return ['img', 'file', 'page', 'user'].includes(ref.id) ? renderPlaceholder(ref.id, ref.body) : nativeToMarkers(ref.raw);
|
|
138
|
+
});
|
|
139
|
+
html = html.replace(/<p>((?:\s*<!-- MACRO:start:[^>]* -->\s*<!-- MACRO:end:[^>]* -->\s*)+)<\/p>/g, '$1');
|
|
115
140
|
// ```confluence-storage — транспорт для дословного XHTML (ac:/ri:-теги
|
|
116
141
|
// markdown-it сквозь себя не пропускает). Fence рендерится в экранированный
|
|
117
142
|
// <pre><code>; здесь разэкранируем содержимое обратно в живую разметку.
|
|
@@ -160,35 +185,7 @@ export function renderToStorage(markdown, urls, opts = {}) {
|
|
|
160
185
|
return `<ac:link><ri:attachment ri:filename="${escapeXmlAttr(resource.attachment)}" />${body}</ac:link>`;
|
|
161
186
|
});
|
|
162
187
|
}
|
|
163
|
-
return
|
|
164
|
-
const { name, attrs } = parsePlaceholder(String(rawBody));
|
|
165
|
-
if (kind === 'page') {
|
|
166
|
-
return renderPageLink(name, attrs);
|
|
167
|
-
}
|
|
168
|
-
if (kind === 'img') {
|
|
169
|
-
if (opts.imageStyle === 'attachment') {
|
|
170
|
-
const acAttrs = attrs
|
|
171
|
-
.map(([k, v]) => ` ac:${k}="${escapeXmlAttr(v)}"`)
|
|
172
|
-
.join('');
|
|
173
|
-
return `<ac:image${acAttrs}><ri:attachment ri:filename="${escapeXmlAttr(name)}" /></ac:image>`;
|
|
174
|
-
}
|
|
175
|
-
const url = urls.images.get(name);
|
|
176
|
-
if (!url) {
|
|
177
|
-
throw new MissingAttachmentUrlError(`No uploaded URL for image '${name}' — was it included in publishPage().images and successfully uploaded?`);
|
|
178
|
-
}
|
|
179
|
-
return `<img src="${escapeXmlAttr(url)}" alt="${escapeXmlAttr(name)}" />`;
|
|
180
|
-
}
|
|
181
|
-
if (opts.fileStyle === 'attachment') {
|
|
182
|
-
const text = attrs.find(([k]) => k === 'text')?.[1];
|
|
183
|
-
const body = text !== undefined ? plainTextLinkBody(text) : '';
|
|
184
|
-
return `<ac:link><ri:attachment ri:filename="${escapeXmlAttr(name)}" />${body}</ac:link>`;
|
|
185
|
-
}
|
|
186
|
-
const url = urls.files.get(name);
|
|
187
|
-
if (!url) {
|
|
188
|
-
throw new MissingAttachmentUrlError(`No uploaded URL for file '${name}' — was it included in publishPage().files and successfully uploaded?`);
|
|
189
|
-
}
|
|
190
|
-
return `<a href="${escapeXmlAttr(url)}">${escapeXmlAttr(name)}</a>`;
|
|
191
|
-
});
|
|
188
|
+
return placeholders.some(ref => ref.id === 'note') ? liftInlineNotes(html) : html;
|
|
192
189
|
}
|
|
193
190
|
function renderPageLink(title, attrs) {
|
|
194
191
|
const space = attrs.find(([k]) => k === 'space')?.[1];
|
package/dist/syntax.d.ts
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { PLACEHOLDER_RE, parsePlaceholder, placeholder, quotePlaceholder, type PlaceholderRef } from './markdown/placeholder.js';
|
|
2
|
+
export { readInlineSyntax, replaceInlineSyntax, unescapeTablePipes, type InlineSyntax } from './markdown/inline-syntax.js';
|
|
3
|
+
export { parseInlineNote, inlineNote } from './markdown/note.js';
|
package/dist/syntax.js
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
// Browser-safe authoring helpers shared with editors.
|
|
2
|
+
export { PLACEHOLDER_RE, parsePlaceholder, placeholder, quotePlaceholder } from './markdown/placeholder.js';
|
|
3
|
+
export { readInlineSyntax, replaceInlineSyntax, unescapeTablePipes } from './markdown/inline-syntax.js';
|
|
4
|
+
export { parseInlineNote, inlineNote } from './markdown/note.js';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "confluence-md-sync",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.13.0",
|
|
4
4
|
"description": "Publish Markdown to Confluence (Data Center & Cloud): idempotent page sync, attachment dedup, tables and a pluggable macro system",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"confluence",
|
|
@@ -40,6 +40,10 @@
|
|
|
40
40
|
"./macros": {
|
|
41
41
|
"types": "./dist/macros/index.d.ts",
|
|
42
42
|
"import": "./dist/macros/index.js"
|
|
43
|
+
},
|
|
44
|
+
"./syntax": {
|
|
45
|
+
"types": "./dist/syntax.d.ts",
|
|
46
|
+
"import": "./dist/syntax.js"
|
|
43
47
|
}
|
|
44
48
|
},
|
|
45
49
|
"bin": {
|