@inshapardaz/likhari-converters 0.1.45-dev.334 → 0.1.45-dev.337
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.
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { type SNode } from '../shared/serialized.js';
|
|
2
|
+
/**
|
|
3
|
+
* Whether a block's text stops mid-sentence: it does not end in a sentence mark (once any
|
|
4
|
+
* closing quote or bracket is set aside). A comma, colon, semicolon, dash or a plain word all
|
|
5
|
+
* count as unfinished. A block with no text at all counts as finished.
|
|
6
|
+
*/
|
|
7
|
+
export declare function endsMidSentence(block: SNode): boolean;
|
|
8
|
+
/**
|
|
9
|
+
* Whether two blocks could be one paragraph: both are plain paragraphs, the second is not
|
|
10
|
+
* indented (an indent starts a new paragraph) and has text, and they agree on text direction
|
|
11
|
+
* and alignment. A direction left unset on either side is compatible with anything.
|
|
12
|
+
*/
|
|
13
|
+
export declare function canJoinParagraphs(previous: SNode | undefined, next: SNode | undefined): boolean;
|
|
14
|
+
/** A paragraph with nothing in it: only whitespace and line breaks. */
|
|
15
|
+
export declare function isEmptyParagraph(block: SNode | undefined): boolean;
|
|
16
|
+
/**
|
|
17
|
+
* Whether a page's text ends with an empty line, which marks the end of a paragraph. Markdown
|
|
18
|
+
* cannot hold an empty paragraph (the parser drops it), so it is read from the raw text: the
|
|
19
|
+
* text must end with a blank line (`\n\n`), not just a final newline. For every other format it
|
|
20
|
+
* is a trailing empty paragraph in the parsed page.
|
|
21
|
+
*/
|
|
22
|
+
export declare function endsWithBlankLine(rawFormat: string, rawText: string, blocks: SNode[]): boolean;
|
|
23
|
+
/** Appends `next`'s content to `previous` (which is changed), separated by one space unless one is already there. */
|
|
24
|
+
export declare function joinParagraphs(previous: SNode, next: SNode): void;
|
|
25
|
+
//# sourceMappingURL=continuation.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"continuation.d.ts","sourceRoot":"","sources":["../../src/merge/continuation.ts"],"names":[],"mappings":"AAAA,OAAO,EAAY,KAAK,KAAK,EAAE,MAAM,sBAAsB,CAAC;AAc5D;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,KAAK,GAAG,OAAO,CAGrD;AAKD;;;;GAIG;AACH,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,KAAK,GAAG,SAAS,EAAE,IAAI,EAAE,KAAK,GAAG,SAAS,GAAG,OAAO,CAM/F;AAED,uEAAuE;AACvE,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,KAAK,GAAG,SAAS,GAAG,OAAO,CAElE;AAED;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,OAAO,CAG9F;AAKD,qHAAqH;AACrH,wBAAgB,cAAc,CAAC,QAAQ,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,GAAG,IAAI,CAOjE"}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { textNode } from '../shared/serialized.js';
|
|
2
|
+
/** Marks that end a sentence, in Urdu and Latin script: ۔ ؟ ! ? . … and the CJK full stop. */
|
|
3
|
+
const SENTENCE_END = /[۔؟!?.…。]/;
|
|
4
|
+
/** Closing quotes and brackets that may follow the mark and still end the sentence: ” ’ " ' ) ] » */
|
|
5
|
+
const CLOSERS = /[”’"')\]»]+$/;
|
|
6
|
+
/** The text of a block, from its text nodes only. */
|
|
7
|
+
function plainText(node) {
|
|
8
|
+
if (node.type === 'text')
|
|
9
|
+
return String(node.text ?? '');
|
|
10
|
+
if (node.type === 'linebreak')
|
|
11
|
+
return '\n';
|
|
12
|
+
return (node.children ?? []).map(plainText).join('');
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Whether a block's text stops mid-sentence: it does not end in a sentence mark (once any
|
|
16
|
+
* closing quote or bracket is set aside). A comma, colon, semicolon, dash or a plain word all
|
|
17
|
+
* count as unfinished. A block with no text at all counts as finished.
|
|
18
|
+
*/
|
|
19
|
+
export function endsMidSentence(block) {
|
|
20
|
+
const text = plainText(block).trimEnd().replace(CLOSERS, '').trimEnd();
|
|
21
|
+
return text !== '' && !SENTENCE_END.test(text.charAt(text.length - 1));
|
|
22
|
+
}
|
|
23
|
+
/** The alignment a paragraph effectively has: '' and 'start' are the same thing. */
|
|
24
|
+
const alignment = (block) => (block.format === '' || block.format == null ? 'start' : String(block.format));
|
|
25
|
+
/**
|
|
26
|
+
* Whether two blocks could be one paragraph: both are plain paragraphs, the second is not
|
|
27
|
+
* indented (an indent starts a new paragraph) and has text, and they agree on text direction
|
|
28
|
+
* and alignment. A direction left unset on either side is compatible with anything.
|
|
29
|
+
*/
|
|
30
|
+
export function canJoinParagraphs(previous, next) {
|
|
31
|
+
if (!previous || !next || previous.type !== 'paragraph' || next.type !== 'paragraph')
|
|
32
|
+
return false;
|
|
33
|
+
if (plainText(next).trim() === '' || plainText(previous).trim() === '')
|
|
34
|
+
return false;
|
|
35
|
+
if (Number(next.indent ?? 0) > 0)
|
|
36
|
+
return false;
|
|
37
|
+
if (previous.direction && next.direction && previous.direction !== next.direction)
|
|
38
|
+
return false;
|
|
39
|
+
return alignment(previous) === alignment(next);
|
|
40
|
+
}
|
|
41
|
+
/** A paragraph with nothing in it: only whitespace and line breaks. */
|
|
42
|
+
export function isEmptyParagraph(block) {
|
|
43
|
+
return !!block && block.type === 'paragraph' && plainText(block).trim() === '';
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Whether a page's text ends with an empty line, which marks the end of a paragraph. Markdown
|
|
47
|
+
* cannot hold an empty paragraph (the parser drops it), so it is read from the raw text: the
|
|
48
|
+
* text must end with a blank line (`\n\n`), not just a final newline. For every other format it
|
|
49
|
+
* is a trailing empty paragraph in the parsed page.
|
|
50
|
+
*/
|
|
51
|
+
export function endsWithBlankLine(rawFormat, rawText, blocks) {
|
|
52
|
+
if (rawFormat === 'markdown')
|
|
53
|
+
return /(\r?\n[ \t]*){2,}$/.test(rawText);
|
|
54
|
+
return isEmptyParagraph(blocks[blocks.length - 1]);
|
|
55
|
+
}
|
|
56
|
+
const startsWithSpace = (children) => /^\s/.test(plainText({ type: 'x', children: children.slice(0, 1) }));
|
|
57
|
+
const endsWithSpace = (children) => /\s$/.test(plainText({ type: 'x', children: children.slice(-1) }));
|
|
58
|
+
/** Appends `next`'s content to `previous` (which is changed), separated by one space unless one is already there. */
|
|
59
|
+
export function joinParagraphs(previous, next) {
|
|
60
|
+
const head = previous.children ?? [];
|
|
61
|
+
const tail = next.children ?? [];
|
|
62
|
+
const needsSpace = head.length > 0 && tail.length > 0 && !endsWithSpace(head) && !startsWithSpace(tail);
|
|
63
|
+
previous.children = [...head, ...(needsSpace ? [textNode(' ')] : []), ...tail];
|
|
64
|
+
// The merged paragraph keeps the first one's direction unless it had none.
|
|
65
|
+
if (!previous.direction && next.direction)
|
|
66
|
+
previous.direction = next.direction;
|
|
67
|
+
}
|
package/dist/merge/index.d.ts
CHANGED
|
@@ -11,6 +11,24 @@ export interface MergePagesOptions {
|
|
|
11
11
|
inputFormat?: FormatId;
|
|
12
12
|
/** The format of the combined document. */
|
|
13
13
|
outputFormat: FormatId;
|
|
14
|
+
/** Leave out pages with no content: nothing, whitespace, or only empty paragraphs. Off by default. */
|
|
15
|
+
skipBlankPages?: boolean;
|
|
16
|
+
/**
|
|
17
|
+
* Join pages that carry on the previous page's last paragraph into one paragraph, instead
|
|
18
|
+
* of breaking the page. Off by default. Both modes only join when the next page opens with an
|
|
19
|
+
* unindented paragraph of the same direction and alignment after a plain paragraph; headings,
|
|
20
|
+
* lists, quotes, tables and blank pages never join.
|
|
21
|
+
*
|
|
22
|
+
* - `'blank-line'`: a page that ends with an empty line or empty paragraph ends its paragraph,
|
|
23
|
+
* so the next page starts a new one; a page that does not, continues. The marker is consumed,
|
|
24
|
+
* so it does not appear in the output. Markdown is read from the raw text (it must end with a
|
|
25
|
+
* blank line, `\n\n`), because Markdown has no empty paragraph; see the README.
|
|
26
|
+
* - `'punctuation'`: a guess from the text. The page continues when its last paragraph does
|
|
27
|
+
* not end with `۔ ؟ ! ? . …` (closing quotes and brackets aside).
|
|
28
|
+
*
|
|
29
|
+
* A page break cannot sit inside a paragraph, so none is inserted where pages are joined.
|
|
30
|
+
*/
|
|
31
|
+
joinParagraphs?: 'blank-line' | 'punctuation';
|
|
14
32
|
/** Passed to every parse and to the final serialize (image URL resolver and the like). */
|
|
15
33
|
context?: ConverterContext;
|
|
16
34
|
/** Defaults to the registry with all four built-in formats. */
|
|
@@ -23,6 +41,8 @@ export declare function mergePagesToState(pages: PageInput[], options?: Omit<Mer
|
|
|
23
41
|
* with a page break between each pair of pages, in the requested output format. Needs no
|
|
24
42
|
* editor or DOM, so it can run in a server, a worker or a build script.
|
|
25
43
|
*
|
|
44
|
+
* With `skipBlankPages`, blank pages are left out and no break is added for them.
|
|
45
|
+
*
|
|
26
46
|
* Footnote ids are only unique within a page, so a page whose footnote ids clash with an
|
|
27
47
|
* earlier page's gets them renamed (`1` becomes `1-2`), in references and notes alike; the
|
|
28
48
|
* notes of all pages end up in one list at the end of the document.
|
|
@@ -1 +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;
|
|
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;AAI3D,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;;;;;;;;;;;;;;OAcG;IACH,cAAc,CAAC,EAAE,YAAY,GAAG,aAAa,CAAC;IAC9C,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,CA8ClI;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,SAAS,EAAE,EAAE,OAAO,EAAE,iBAAiB,GAAG,MAAM,CAGjF"}
|
package/dist/merge/index.js
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
import { defaultFormatRegistry } from '../registry.js';
|
|
2
2
|
import { elementBase, makeState, rootChildren } from '../shared/serialized.js';
|
|
3
|
+
import { canJoinParagraphs, endsMidSentence, endsWithBlankLine, isEmptyParagraph, joinParagraphs } from './continuation.js';
|
|
3
4
|
/** Parses each page and returns the combined document as Lexical state, without serializing it. */
|
|
4
5
|
export function mergePagesToState(pages, options = {}) {
|
|
5
|
-
const { inputFormat = 'markdown', context, registry = defaultFormatRegistry } = options;
|
|
6
|
+
const { inputFormat = 'markdown', skipBlankPages = false, joinParagraphs: joinMode, context, registry = defaultFormatRegistry } = options;
|
|
6
7
|
const body = [];
|
|
7
8
|
const footnotes = [];
|
|
8
9
|
const takenFootnoteIds = new Set();
|
|
10
|
+
let kept = 0;
|
|
11
|
+
let previousEndedParagraph = false;
|
|
9
12
|
pages.forEach((page, index) => {
|
|
10
13
|
const { content, format = inputFormat } = typeof page === 'string' ? { content: page } : page;
|
|
11
14
|
let blocks;
|
|
@@ -16,14 +19,33 @@ export function mergePagesToState(pages, options = {}) {
|
|
|
16
19
|
const reason = error instanceof Error ? error.message : String(error);
|
|
17
20
|
throw new Error(`mergePages: page ${index + 1} is not valid ${format}: ${reason}`);
|
|
18
21
|
}
|
|
22
|
+
if (skipBlankPages && blocks.every(isBlank))
|
|
23
|
+
return;
|
|
19
24
|
// Each page brings its own footnote list; they are combined into one at the end.
|
|
20
25
|
const pageLists = blocks.filter((block) => block.type === 'footnote-list');
|
|
21
26
|
const pageBody = blocks.filter((block) => block.type !== 'footnote-list');
|
|
22
27
|
uniquifyFootnoteIds([...pageBody, ...pageLists], takenFootnoteIds);
|
|
23
28
|
for (const list of pageLists)
|
|
24
29
|
footnotes.push(...(list.children ?? []));
|
|
25
|
-
|
|
30
|
+
const previous = body[body.length - 1];
|
|
31
|
+
let continues = false;
|
|
32
|
+
if (joinMode === 'punctuation')
|
|
33
|
+
continues = !!previous && endsMidSentence(previous);
|
|
34
|
+
if (joinMode === 'blank-line') {
|
|
35
|
+
continues = !previousEndedParagraph;
|
|
36
|
+
previousEndedParagraph = endsWithBlankLine(format, content, [...pageBody]);
|
|
37
|
+
// The empty line only marks the end of the paragraph; it is not content to keep.
|
|
38
|
+
if (pageBody.some((block) => !isEmptyParagraph(block))) {
|
|
39
|
+
while (isEmptyParagraph(pageBody[pageBody.length - 1]))
|
|
40
|
+
pageBody.pop();
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
const joins = kept > 0 && continues && canJoinParagraphs(previous, pageBody[0]);
|
|
44
|
+
if (joins)
|
|
45
|
+
joinParagraphs(previous, pageBody.shift());
|
|
46
|
+
else if (kept > 0)
|
|
26
47
|
body.push({ type: 'page-break', version: 1 });
|
|
48
|
+
kept += 1;
|
|
27
49
|
body.push(...pageBody);
|
|
28
50
|
});
|
|
29
51
|
return makeState(footnotes.length > 0 ? [...body, elementBase('footnote-list', footnotes)] : body);
|
|
@@ -33,6 +55,8 @@ export function mergePagesToState(pages, options = {}) {
|
|
|
33
55
|
* with a page break between each pair of pages, in the requested output format. Needs no
|
|
34
56
|
* editor or DOM, so it can run in a server, a worker or a build script.
|
|
35
57
|
*
|
|
58
|
+
* With `skipBlankPages`, blank pages are left out and no break is added for them.
|
|
59
|
+
*
|
|
36
60
|
* Footnote ids are only unique within a page, so a page whose footnote ids clash with an
|
|
37
61
|
* earlier page's gets them renamed (`1` becomes `1-2`), in references and notes alike; the
|
|
38
62
|
* notes of all pages end up in one list at the end of the document.
|
|
@@ -41,6 +65,12 @@ export function mergePages(pages, options) {
|
|
|
41
65
|
const registry = options.registry ?? defaultFormatRegistry;
|
|
42
66
|
return registry.serialize(options.outputFormat, mergePagesToState(pages, options), options.context);
|
|
43
67
|
}
|
|
68
|
+
/** 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. */
|
|
69
|
+
function isBlank(block) {
|
|
70
|
+
if (block.type !== 'paragraph')
|
|
71
|
+
return false;
|
|
72
|
+
return (block.children ?? []).every((child) => (child.type === 'text' ? String(child.text ?? '').trim() === '' : child.type === 'linebreak'));
|
|
73
|
+
}
|
|
44
74
|
/** Renames this page's footnote ids that an earlier page already uses, then claims all its ids. */
|
|
45
75
|
function uniquifyFootnoteIds(blocks, taken) {
|
|
46
76
|
const nodes = [];
|
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.337",
|
|
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": {
|