@inshapardaz/likhari-converters 0.1.45-dev.331 → 0.1.45-dev.335
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/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/merge/index.d.ts +35 -0
- package/dist/merge/index.d.ts.map +1 -0
- package/dist/merge/index.js +77 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -4,4 +4,5 @@ export { plainTextConverter } from './plain-text/index.js';
|
|
|
4
4
|
export { lexicalJsonConverter } from './lexical-json/index.js';
|
|
5
5
|
export { markdownConverter } from './markdown/index.js';
|
|
6
6
|
export { htmlConverter } from './html/index.js';
|
|
7
|
+
export { mergePages, mergePagesToState, type MergePagesOptions, type PageInput } from './merge/index.js';
|
|
7
8
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC;AACxB,cAAc,YAAY,CAAC;AAC3B,OAAO,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAClD,OAAO,EAAE,oBAAoB,EAAE,MAAM,gBAAgB,CAAC;AACtD,OAAO,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAC/C,OAAO,EAAE,aAAa,EAAE,MAAM,QAAQ,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC;AACxB,cAAc,YAAY,CAAC;AAC3B,OAAO,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAClD,OAAO,EAAE,oBAAoB,EAAE,MAAM,gBAAgB,CAAC;AACtD,OAAO,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAC/C,OAAO,EAAE,aAAa,EAAE,MAAM,QAAQ,CAAC;AACvC,OAAO,EAAE,UAAU,EAAE,iBAAiB,EAAE,KAAK,iBAAiB,EAAE,KAAK,SAAS,EAAE,MAAM,SAAS,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -4,3 +4,4 @@ export { plainTextConverter } from './plain-text/index.js';
|
|
|
4
4
|
export { lexicalJsonConverter } from './lexical-json/index.js';
|
|
5
5
|
export { markdownConverter } from './markdown/index.js';
|
|
6
6
|
export { htmlConverter } from './html/index.js';
|
|
7
|
+
export { mergePages, mergePagesToState } from './merge/index.js';
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { SerializedEditorState } from 'lexical';
|
|
2
|
+
import { type FormatRegistry } from '../registry.js';
|
|
3
|
+
import type { ConverterContext, FormatId } from '../types.js';
|
|
4
|
+
/** One page: its text, plus its format when that differs from `MergePagesOptions.inputFormat`. */
|
|
5
|
+
export type PageInput = string | {
|
|
6
|
+
content: string;
|
|
7
|
+
format?: FormatId;
|
|
8
|
+
};
|
|
9
|
+
export interface MergePagesOptions {
|
|
10
|
+
/** The format of every page given as a bare string. Defaults to 'markdown'. */
|
|
11
|
+
inputFormat?: FormatId;
|
|
12
|
+
/** The format of the combined document. */
|
|
13
|
+
outputFormat: FormatId;
|
|
14
|
+
/** Leave out pages with no content: nothing, whitespace, or only empty paragraphs. Off by default. */
|
|
15
|
+
skipBlankPages?: boolean;
|
|
16
|
+
/** Passed to every parse and to the final serialize (image URL resolver and the like). */
|
|
17
|
+
context?: ConverterContext;
|
|
18
|
+
/** Defaults to the registry with all four built-in formats. */
|
|
19
|
+
registry?: FormatRegistry;
|
|
20
|
+
}
|
|
21
|
+
/** Parses each page and returns the combined document as Lexical state, without serializing it. */
|
|
22
|
+
export declare function mergePagesToState(pages: PageInput[], options?: Omit<MergePagesOptions, 'outputFormat'>): SerializedEditorState;
|
|
23
|
+
/**
|
|
24
|
+
* Merges pages (Markdown, Lexical JSON, or any other supported format) into one document,
|
|
25
|
+
* with a page break between each pair of pages, in the requested output format. Needs no
|
|
26
|
+
* editor or DOM, so it can run in a server, a worker or a build script.
|
|
27
|
+
*
|
|
28
|
+
* With `skipBlankPages`, blank pages are left out and no break is added for them.
|
|
29
|
+
*
|
|
30
|
+
* Footnote ids are only unique within a page, so a page whose footnote ids clash with an
|
|
31
|
+
* earlier page's gets them renamed (`1` becomes `1-2`), in references and notes alike; the
|
|
32
|
+
* notes of all pages end up in one list at the end of the document.
|
|
33
|
+
*/
|
|
34
|
+
export declare function mergePages(pages: PageInput[], options: MergePagesOptions): string;
|
|
35
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/merge/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,SAAS,CAAC;AACrD,OAAO,EAAyB,KAAK,cAAc,EAAE,MAAM,aAAa,CAAC;AACzE,OAAO,KAAK,EAAE,gBAAgB,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AAG3D,kGAAkG;AAClG,MAAM,MAAM,SAAS,GAAG,MAAM,GAAG;IAAE,OAAO,EAAE,MAAM,CAAC;IAAC,MAAM,CAAC,EAAE,QAAQ,CAAA;CAAE,CAAC;AAExE,MAAM,WAAW,iBAAiB;IAChC,+EAA+E;IAC/E,WAAW,CAAC,EAAE,QAAQ,CAAC;IACvB,2CAA2C;IAC3C,YAAY,EAAE,QAAQ,CAAC;IACvB,sGAAsG;IACtG,cAAc,CAAC,EAAE,OAAO,CAAC;IACzB,0FAA0F;IAC1F,OAAO,CAAC,EAAE,gBAAgB,CAAC;IAC3B,+DAA+D;IAC/D,QAAQ,CAAC,EAAE,cAAc,CAAC;CAC3B;AAED,mGAAmG;AACnG,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,SAAS,EAAE,EAAE,OAAO,GAAE,IAAI,CAAC,iBAAiB,EAAE,cAAc,CAAM,GAAG,qBAAqB,CAgClI;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,SAAS,EAAE,EAAE,OAAO,EAAE,iBAAiB,GAAG,MAAM,CAGjF"}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { defaultFormatRegistry } from '../registry.js';
|
|
2
|
+
import { elementBase, makeState, rootChildren } from '../shared/serialized.js';
|
|
3
|
+
/** Parses each page and returns the combined document as Lexical state, without serializing it. */
|
|
4
|
+
export function mergePagesToState(pages, options = {}) {
|
|
5
|
+
const { inputFormat = 'markdown', skipBlankPages = false, context, registry = defaultFormatRegistry } = options;
|
|
6
|
+
const body = [];
|
|
7
|
+
const footnotes = [];
|
|
8
|
+
const takenFootnoteIds = new Set();
|
|
9
|
+
let kept = 0;
|
|
10
|
+
pages.forEach((page, index) => {
|
|
11
|
+
const { content, format = inputFormat } = typeof page === 'string' ? { content: page } : page;
|
|
12
|
+
let blocks;
|
|
13
|
+
try {
|
|
14
|
+
blocks = structuredClone(rootChildren(registry.parse(format, content, context)));
|
|
15
|
+
}
|
|
16
|
+
catch (error) {
|
|
17
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
18
|
+
throw new Error(`mergePages: page ${index + 1} is not valid ${format}: ${reason}`);
|
|
19
|
+
}
|
|
20
|
+
if (skipBlankPages && blocks.every(isBlank))
|
|
21
|
+
return;
|
|
22
|
+
// Each page brings its own footnote list; they are combined into one at the end.
|
|
23
|
+
const pageLists = blocks.filter((block) => block.type === 'footnote-list');
|
|
24
|
+
const pageBody = blocks.filter((block) => block.type !== 'footnote-list');
|
|
25
|
+
uniquifyFootnoteIds([...pageBody, ...pageLists], takenFootnoteIds);
|
|
26
|
+
for (const list of pageLists)
|
|
27
|
+
footnotes.push(...(list.children ?? []));
|
|
28
|
+
if (kept > 0)
|
|
29
|
+
body.push({ type: 'page-break', version: 1 });
|
|
30
|
+
kept += 1;
|
|
31
|
+
body.push(...pageBody);
|
|
32
|
+
});
|
|
33
|
+
return makeState(footnotes.length > 0 ? [...body, elementBase('footnote-list', footnotes)] : body);
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Merges pages (Markdown, Lexical JSON, or any other supported format) into one document,
|
|
37
|
+
* with a page break between each pair of pages, in the requested output format. Needs no
|
|
38
|
+
* editor or DOM, so it can run in a server, a worker or a build script.
|
|
39
|
+
*
|
|
40
|
+
* With `skipBlankPages`, blank pages are left out and no break is added for them.
|
|
41
|
+
*
|
|
42
|
+
* Footnote ids are only unique within a page, so a page whose footnote ids clash with an
|
|
43
|
+
* earlier page's gets them renamed (`1` becomes `1-2`), in references and notes alike; the
|
|
44
|
+
* notes of all pages end up in one list at the end of the document.
|
|
45
|
+
*/
|
|
46
|
+
export function mergePages(pages, options) {
|
|
47
|
+
const registry = options.registry ?? defaultFormatRegistry;
|
|
48
|
+
return registry.serialize(options.outputFormat, mergePagesToState(pages, options), options.context);
|
|
49
|
+
}
|
|
50
|
+
/** A block with nothing in it: a paragraph holding only whitespace and line breaks. Anything else (an image, a rule, a table, a break) is content. */
|
|
51
|
+
function isBlank(block) {
|
|
52
|
+
if (block.type !== 'paragraph')
|
|
53
|
+
return false;
|
|
54
|
+
return (block.children ?? []).every((child) => (child.type === 'text' ? String(child.text ?? '').trim() === '' : child.type === 'linebreak'));
|
|
55
|
+
}
|
|
56
|
+
/** Renames this page's footnote ids that an earlier page already uses, then claims all its ids. */
|
|
57
|
+
function uniquifyFootnoteIds(blocks, taken) {
|
|
58
|
+
const nodes = [];
|
|
59
|
+
const collect = (node) => {
|
|
60
|
+
if (typeof node.footnoteId === 'string')
|
|
61
|
+
nodes.push(node);
|
|
62
|
+
(node.children ?? []).forEach(collect);
|
|
63
|
+
};
|
|
64
|
+
blocks.forEach(collect);
|
|
65
|
+
const renamed = new Map();
|
|
66
|
+
for (const node of nodes) {
|
|
67
|
+
const id = node.footnoteId;
|
|
68
|
+
if (!renamed.has(id)) {
|
|
69
|
+
let next = id;
|
|
70
|
+
for (let n = 2; taken.has(next); n += 1)
|
|
71
|
+
next = `${id}-${n}`;
|
|
72
|
+
renamed.set(id, next);
|
|
73
|
+
}
|
|
74
|
+
node.footnoteId = renamed.get(id);
|
|
75
|
+
}
|
|
76
|
+
renamed.forEach((id) => taken.add(id));
|
|
77
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inshapardaz/likhari-converters",
|
|
3
|
-
"version": "0.1.45-dev.
|
|
3
|
+
"version": "0.1.45-dev.335",
|
|
4
4
|
"description": "Format transformers (Markdown/HTML/plain-text/Lexical JSON) against the canonical Lexical EditorState, isolated from UI.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|