merong-docs 0.1.11 → 1.0.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.
Files changed (63) hide show
  1. package/OVERVIEW.md +19 -19
  2. package/OVERVIEW.zh-CN.md +19 -22
  3. package/README.md +77 -77
  4. package/README.zh-CN.md +81 -81
  5. package/client/android-file-share.ts +25 -25
  6. package/client/controls.tsx +163 -163
  7. package/client/document-panel.tsx +166 -166
  8. package/client/external-file.tsx +78 -78
  9. package/client/file-reader.tsx +43 -43
  10. package/client/markdown-panel.tsx +737 -737
  11. package/client/markdown-scroll.ts +122 -122
  12. package/client/messages.ts +224 -224
  13. package/client/native-contacts.ts +145 -145
  14. package/client/page-render-queue.ts +12 -12
  15. package/client/page-selection.tsx +227 -227
  16. package/client/page-tools.tsx +72 -72
  17. package/client/pdf-geometry.ts +96 -96
  18. package/client/pdf-panel.tsx +477 -477
  19. package/client/reader-find.tsx +190 -190
  20. package/client/reader-notice.tsx +46 -46
  21. package/client/settings.tsx +143 -143
  22. package/client/syntax.tsx +55 -55
  23. package/client/viewport-geometry.ts +61 -61
  24. package/client/web.ts +462 -462
  25. package/client/worksheet-panel.tsx +631 -631
  26. package/client/zoom-controls.tsx +140 -140
  27. package/index.client.tsx +36 -36
  28. package/index.server.ts +73 -73
  29. package/node_modules/markdown-it/bin/markdown-it.mjs +0 -0
  30. package/node_modules/yaml/bin.mjs +0 -0
  31. package/npm-shrinkwrap.json +2 -2
  32. package/package.json +1 -1
  33. package/scripts/prepare-office.mjs +20 -20
  34. package/server/engine.ts +89 -89
  35. package/server/external-file.ts +42 -42
  36. package/server/generated/document-worker.ts +1 -1
  37. package/server/generated/rust-reader_bg.wasm +0 -0
  38. package/server/markdown-index.ts +305 -305
  39. package/server/markdown-links.ts +51 -51
  40. package/server/markdown-page.ts +120 -120
  41. package/server/office-fonts.cjs +824 -824
  42. package/server/office-location.ts +41 -41
  43. package/server/office-preview.ts +186 -186
  44. package/server/office-worker.cjs +49 -49
  45. package/server/office-xml.ts +79 -79
  46. package/server/pdf-engine.ts +168 -168
  47. package/server/pdf-text-geometry.ts +60 -60
  48. package/server/rust-reader.ts +40 -40
  49. package/server/structured-document.ts +69 -69
  50. package/server/text-search.ts +15 -15
  51. package/server/worker-entry.ts +42 -42
  52. package/server/worker-protocol.ts +38 -38
  53. package/server/worksheet.ts +67 -67
  54. package/server/workspace-file.ts +63 -63
  55. package/shared/contracts.ts +187 -187
  56. package/shared/file-limits.ts +5 -5
  57. package/shared/markdown-constants.ts +4 -4
  58. package/shared/markdown-math.ts +198 -198
  59. package/shared/markdown-references.ts +83 -83
  60. package/shared/markdown-source-location.ts +8 -8
  61. package/shared/reader-settings.ts +15 -15
  62. package/shared/reader-shortcuts.ts +132 -132
  63. package/vendor/office2pdf/office2pdf_bg.wasm +2 -2
@@ -1,51 +1,51 @@
1
- import { dirname, isAbsolute, relative, resolve, sep } from "node:path";
2
- import { decodeString } from "micromark-util-decode-string";
3
- import { markdownExtensions } from "../shared/markdown-constants";
4
- import { markdownEvents } from "../shared/markdown-references";
5
-
6
- const localLineSuffix = /(?::\d+(?::\d+)?(?:-\d+(?::\d+)?)?|\(\d+(?:,\d+)?(?:-\d+(?:,\d+)?)?\))$/;
7
- const bareMarkdownFile = new RegExp(`^[^:/?#]+\\.(?:${markdownExtensions.join("|")})$`, "i");
8
-
9
- function workspaceDestination(target: string, root: string, directory: string): string | undefined {
10
- if (!target || /^[/\\~#?]/.test(target)) return;
11
- const hash = target.indexOf("#");
12
- const route = hash === -1 ? target : target.slice(0, hash);
13
- if (route.includes("?")) return;
14
- const lines = localLineSuffix.exec(route)?.[0] ?? "";
15
- const fileRoute = route.slice(0, route.length - lines.length);
16
- if (/^[a-z][a-z\d+.-]*:/i.test(route) && !(lines.startsWith(":") && bareMarkdownFile.test(fileRoute))) return;
17
- let decoded: string;
18
- try {
19
- decoded = decodeURIComponent(fileRoute);
20
- } catch {
21
- return;
22
- }
23
- const path = relative(root, resolve(directory, decoded));
24
- if (!path || path === ".." || path.startsWith(`..${sep}`) || isAbsolute(path)) return;
25
- const encoded = path.split(sep).map((part) =>
26
- encodeURIComponent(part).replace(/[()]/g, (mark) => mark === "(" ? "%28" : "%29"),
27
- ).join("/");
28
- return `./${encoded}${lines}${hash === -1 ? "" : target.slice(hash)}`;
29
- }
30
-
31
- export function rebaseMarkdownDestination(destination: string, root: string, filePath: string) {
32
- return workspaceDestination(destination, root, dirname(resolve(root, filePath))) ?? destination;
33
- }
34
-
35
- export function rebaseMarkdownLinks(markdown: string, root: string, filePath: string): string {
36
- const resolvedRoot = resolve(root);
37
- const directory = dirname(resolve(resolvedRoot, filePath));
38
- if (directory === resolvedRoot || (!markdown.includes("](") && !markdown.includes("]:"))) return markdown;
39
- const events = markdownEvents(markdown);
40
- const parts: string[] = [];
41
- let offset = 0;
42
- for (const [action, token] of events) {
43
- if (action !== "enter" || (token.type !== "resourceDestinationString" && token.type !== "definitionDestinationString")) continue;
44
- const { start, end } = token;
45
- const destination = workspaceDestination(decodeString(markdown.slice(start.offset, end.offset)), resolvedRoot, directory);
46
- if (destination === undefined) continue;
47
- parts.push(markdown.slice(offset, start.offset), destination);
48
- offset = end.offset;
49
- }
50
- return offset === 0 ? markdown : parts.join("") + markdown.slice(offset);
51
- }
1
+ import { dirname, isAbsolute, relative, resolve, sep } from "node:path";
2
+ import { decodeString } from "micromark-util-decode-string";
3
+ import { markdownExtensions } from "../shared/markdown-constants";
4
+ import { markdownEvents } from "../shared/markdown-references";
5
+
6
+ const localLineSuffix = /(?::\d+(?::\d+)?(?:-\d+(?::\d+)?)?|\(\d+(?:,\d+)?(?:-\d+(?:,\d+)?)?\))$/;
7
+ const bareMarkdownFile = new RegExp(`^[^:/?#]+\\.(?:${markdownExtensions.join("|")})$`, "i");
8
+
9
+ function workspaceDestination(target: string, root: string, directory: string): string | undefined {
10
+ if (!target || /^[/\\~#?]/.test(target)) return;
11
+ const hash = target.indexOf("#");
12
+ const route = hash === -1 ? target : target.slice(0, hash);
13
+ if (route.includes("?")) return;
14
+ const lines = localLineSuffix.exec(route)?.[0] ?? "";
15
+ const fileRoute = route.slice(0, route.length - lines.length);
16
+ if (/^[a-z][a-z\d+.-]*:/i.test(route) && !(lines.startsWith(":") && bareMarkdownFile.test(fileRoute))) return;
17
+ let decoded: string;
18
+ try {
19
+ decoded = decodeURIComponent(fileRoute);
20
+ } catch {
21
+ return;
22
+ }
23
+ const path = relative(root, resolve(directory, decoded));
24
+ if (!path || path === ".." || path.startsWith(`..${sep}`) || isAbsolute(path)) return;
25
+ const encoded = path.split(sep).map((part) =>
26
+ encodeURIComponent(part).replace(/[()]/g, (mark) => mark === "(" ? "%28" : "%29"),
27
+ ).join("/");
28
+ return `./${encoded}${lines}${hash === -1 ? "" : target.slice(hash)}`;
29
+ }
30
+
31
+ export function rebaseMarkdownDestination(destination: string, root: string, filePath: string) {
32
+ return workspaceDestination(destination, root, dirname(resolve(root, filePath))) ?? destination;
33
+ }
34
+
35
+ export function rebaseMarkdownLinks(markdown: string, root: string, filePath: string): string {
36
+ const resolvedRoot = resolve(root);
37
+ const directory = dirname(resolve(resolvedRoot, filePath));
38
+ if (directory === resolvedRoot || (!markdown.includes("](") && !markdown.includes("]:"))) return markdown;
39
+ const events = markdownEvents(markdown);
40
+ const parts: string[] = [];
41
+ let offset = 0;
42
+ for (const [action, token] of events) {
43
+ if (action !== "enter" || (token.type !== "resourceDestinationString" && token.type !== "definitionDestinationString")) continue;
44
+ const { start, end } = token;
45
+ const destination = workspaceDestination(decodeString(markdown.slice(start.offset, end.offset)), resolvedRoot, directory);
46
+ if (destination === undefined) continue;
47
+ parts.push(markdown.slice(offset, start.offset), destination);
48
+ offset = end.offset;
49
+ }
50
+ return offset === 0 ? markdown : parts.join("") + markdown.slice(offset);
51
+ }
@@ -1,120 +1,120 @@
1
- import { StringDecoder } from "node:string_decoder";
2
- import type { Fence, TableContinuation } from "../shared/contracts";
3
- import { markdownExtensions, maxFormulaLength, maxMarkdownTableLine } from "../shared/markdown-constants";
4
- import { findFormulas, lineOffsets, possibleMath } from "../shared/markdown-math";
5
- import { rebaseMarkdownLinks } from "./markdown-links";
6
- import { markdownPreviewPosition, nextFence } from "./markdown-index";
7
- import { markdownStructure, rustReader } from "./rust-reader";
8
- import { withWorkspaceFile } from "./workspace-file";
9
-
10
- export const markdownPageBytes = 24 * 1024;
11
- const mathLookaheadBytes = (maxFormulaLength + 16) * 4;
12
-
13
- interface Boundary {
14
- end: number;
15
- fence?: Fence;
16
- table?: TableContinuation;
17
- paragraphEnd?: boolean;
18
- }
19
-
20
- function chooseBoundary(text: string, initialFence: Fence | undefined): Boundary {
21
- let fence = initialFence;
22
- let lastLine = { end: 0, fence };
23
- let paragraph = lastLine;
24
- let display: string | undefined;
25
- for (const match of text.matchAll(/[^\r\n]*(?:\r\n|\r(?!$)|\n)/g)) {
26
- const line = match[0].replace(/[\r\n]+$/, "");
27
- const end = match.index + match[0].length;
28
- if (!display) fence = nextFence(line, fence);
29
- if (!fence) {
30
- const delimiter = line.trim();
31
- if (delimiter === "$$") display = display === "$$" ? undefined : display ?? "$$";
32
- if (delimiter === "\\[" && !display) display = "\\]";
33
- if (delimiter === "\\]" && display === "\\]") display = undefined;
34
- }
35
- lastLine = { end, fence };
36
- if (!fence && !display && !line.trim()) paragraph = lastLine;
37
- }
38
- if (paragraph.end > 0 && paragraph.end >= text.length / 2) return { ...paragraph, paragraphEnd: true };
39
- if (lastLine.end >= text.length / 2) return lastLine;
40
- return { end: text.length, fence };
41
- }
42
-
43
- function keepFormulasTogether(text: string, prefix: string, boundary: Boundary): Boundary {
44
- const source = prefix + text;
45
- if (!possibleMath.test(source)) return boundary;
46
- const end = prefix.length + boundary.end;
47
- const crossing = findFormulas(source).find((span) => span.start < end && end < span.end);
48
- if (!crossing) return boundary;
49
- const before = text.slice(0, Math.max(0, crossing.start - prefix.length));
50
- const lineStart = Math.max(before.lastIndexOf("\n"), before.lastIndexOf("\r")) + 1;
51
- if (lineStart >= boundary.end / 2) return { end: lineStart, fence: undefined };
52
- const formulaEnd = crossing.end - prefix.length;
53
- if (crossing.block) return { end: formulaEnd, fence: undefined };
54
- const restOfLine = /^[^\r\n]*(?:\r\n|\r|\n)/.exec(text.slice(formulaEnd));
55
- return { end: restOfLine ? formulaEnd + restOfLine[0].length : text.length, fence: undefined };
56
- }
57
-
58
- async function continueTable(text: string, prefix: string, boundary: Boundary): Promise<Boundary> {
59
- const source = prefix + text;
60
- if (boundary.paragraphEnd || boundary.fence || !/^[ \t]*\|?[ \t]*:?-+:?[ \t]*\|/m.test(source)) return boundary;
61
- const offsets = lineOffsets(source);
62
- const end = prefix.length + boundary.end;
63
- const table = markdownStructure(await rustReader(), source).tables.find(([first, last]) =>
64
- offsets[first] < end && end < offsets[last]);
65
- if (!table) return boundary;
66
- const [first, last] = table;
67
- const headEnd = offsets[first + 2];
68
- const header = source.slice(offsets[first], offsets[first + 1]).trimEnd();
69
- const separator = source.slice(offsets[first + 1], headEnd).trimEnd();
70
- if (header.length > maxMarkdownTableLine || separator.length > maxMarkdownTableLine) return boundary;
71
-
72
- let next = last;
73
- while (next > first + 2 && offsets[next] > end) next--;
74
- const split = Math.max(headEnd, offsets[next]);
75
- if (split <= prefix.length) return boundary;
76
- return { end: split - prefix.length, table: { header, separator } };
77
- }
78
-
79
- export async function loadMarkdownPage(
80
- root: string, path: string, offset: number, revision?: string, fence?: Fence, table?: TableContinuation,
81
- firstLine = 1, atLine?: number,
82
- ) {
83
- return withWorkspaceFile(root, path, markdownExtensions, async (file, size, stamp, actual) => {
84
- if ((revision && stamp !== revision) || offset > size) throw new Error("File changed. Reload to read the new version.");
85
- if (atLine !== undefined) {
86
- const position = await markdownPreviewPosition(file, size, stamp, actual, atLine);
87
- offset = position.offset;
88
- firstLine = position.line;
89
- fence = position.fence;
90
- table = position.table;
91
- }
92
- const pageBytes = Math.min(markdownPageBytes, size - offset);
93
- const bytes = Buffer.alloc(Math.min(pageBytes + mathLookaheadBytes, size - offset));
94
- let length = 0;
95
- while (length < bytes.length) {
96
- const { bytesRead } = await file.read(bytes, length, bytes.length - length, offset + length);
97
- if (!bytesRead) throw new Error("File ended while reading.");
98
- length += bytesRead;
99
- }
100
- const eof = offset + pageBytes === size;
101
- const prefix = fence ? `${fence.opening}\n` : table ? `${table.header}\n${table.separator}\n` : "";
102
- const decoder = new StringDecoder("utf8");
103
- const text = offset + length === size ? decoder.end(bytes) : decoder.write(bytes);
104
- const current = eof ? text : new StringDecoder("utf8").write(bytes.subarray(0, pageBytes));
105
- const boundary = eof ? { end: text.length }
106
- : await continueTable(text, prefix, keepFormulasTogether(text, prefix, chooseBoundary(current, fence)));
107
- const kept = text.slice(0, boundary.end);
108
- const encoded = Buffer.from(kept);
109
- if (!bytes.subarray(0, encoded.length).equals(encoded)) throw new Error("Reader supports UTF-8 text files.");
110
- const source = offset === 0 ? kept.replace(/^\uFEFF/, "") : kept;
111
- const suffix = boundary.fence ? `\n${boundary.fence.marker}\n` : "";
112
- const nextOffset = offset + encoded.length < size ? offset + encoded.length : null;
113
- return {
114
- source, markdown: rebaseMarkdownLinks(prefix + source + suffix, root, path),
115
- nextOffset, offset, revision: stamp, fence: boundary.fence, table: boundary.table,
116
- firstLine, nextLine: firstLine + lineOffsets(source).length - 2,
117
- prefixLines: fence ? 1 : table ? 2 : 0,
118
- };
119
- });
120
- }
1
+ import { StringDecoder } from "node:string_decoder";
2
+ import type { Fence, TableContinuation } from "../shared/contracts";
3
+ import { markdownExtensions, maxFormulaLength, maxMarkdownTableLine } from "../shared/markdown-constants";
4
+ import { findFormulas, lineOffsets, possibleMath } from "../shared/markdown-math";
5
+ import { rebaseMarkdownLinks } from "./markdown-links";
6
+ import { markdownPreviewPosition, nextFence } from "./markdown-index";
7
+ import { markdownStructure, rustReader } from "./rust-reader";
8
+ import { withWorkspaceFile } from "./workspace-file";
9
+
10
+ export const markdownPageBytes = 24 * 1024;
11
+ const mathLookaheadBytes = (maxFormulaLength + 16) * 4;
12
+
13
+ interface Boundary {
14
+ end: number;
15
+ fence?: Fence;
16
+ table?: TableContinuation;
17
+ paragraphEnd?: boolean;
18
+ }
19
+
20
+ function chooseBoundary(text: string, initialFence: Fence | undefined): Boundary {
21
+ let fence = initialFence;
22
+ let lastLine = { end: 0, fence };
23
+ let paragraph = lastLine;
24
+ let display: string | undefined;
25
+ for (const match of text.matchAll(/[^\r\n]*(?:\r\n|\r(?!$)|\n)/g)) {
26
+ const line = match[0].replace(/[\r\n]+$/, "");
27
+ const end = match.index + match[0].length;
28
+ if (!display) fence = nextFence(line, fence);
29
+ if (!fence) {
30
+ const delimiter = line.trim();
31
+ if (delimiter === "$$") display = display === "$$" ? undefined : display ?? "$$";
32
+ if (delimiter === "\\[" && !display) display = "\\]";
33
+ if (delimiter === "\\]" && display === "\\]") display = undefined;
34
+ }
35
+ lastLine = { end, fence };
36
+ if (!fence && !display && !line.trim()) paragraph = lastLine;
37
+ }
38
+ if (paragraph.end > 0 && paragraph.end >= text.length / 2) return { ...paragraph, paragraphEnd: true };
39
+ if (lastLine.end >= text.length / 2) return lastLine;
40
+ return { end: text.length, fence };
41
+ }
42
+
43
+ function keepFormulasTogether(text: string, prefix: string, boundary: Boundary): Boundary {
44
+ const source = prefix + text;
45
+ if (!possibleMath.test(source)) return boundary;
46
+ const end = prefix.length + boundary.end;
47
+ const crossing = findFormulas(source).find((span) => span.start < end && end < span.end);
48
+ if (!crossing) return boundary;
49
+ const before = text.slice(0, Math.max(0, crossing.start - prefix.length));
50
+ const lineStart = Math.max(before.lastIndexOf("\n"), before.lastIndexOf("\r")) + 1;
51
+ if (lineStart >= boundary.end / 2) return { end: lineStart, fence: undefined };
52
+ const formulaEnd = crossing.end - prefix.length;
53
+ if (crossing.block) return { end: formulaEnd, fence: undefined };
54
+ const restOfLine = /^[^\r\n]*(?:\r\n|\r|\n)/.exec(text.slice(formulaEnd));
55
+ return { end: restOfLine ? formulaEnd + restOfLine[0].length : text.length, fence: undefined };
56
+ }
57
+
58
+ async function continueTable(text: string, prefix: string, boundary: Boundary): Promise<Boundary> {
59
+ const source = prefix + text;
60
+ if (boundary.paragraphEnd || boundary.fence || !/^[ \t]*\|?[ \t]*:?-+:?[ \t]*\|/m.test(source)) return boundary;
61
+ const offsets = lineOffsets(source);
62
+ const end = prefix.length + boundary.end;
63
+ const table = markdownStructure(await rustReader(), source).tables.find(([first, last]) =>
64
+ offsets[first] < end && end < offsets[last]);
65
+ if (!table) return boundary;
66
+ const [first, last] = table;
67
+ const headEnd = offsets[first + 2];
68
+ const header = source.slice(offsets[first], offsets[first + 1]).trimEnd();
69
+ const separator = source.slice(offsets[first + 1], headEnd).trimEnd();
70
+ if (header.length > maxMarkdownTableLine || separator.length > maxMarkdownTableLine) return boundary;
71
+
72
+ let next = last;
73
+ while (next > first + 2 && offsets[next] > end) next--;
74
+ const split = Math.max(headEnd, offsets[next]);
75
+ if (split <= prefix.length) return boundary;
76
+ return { end: split - prefix.length, table: { header, separator } };
77
+ }
78
+
79
+ export async function loadMarkdownPage(
80
+ root: string, path: string, offset: number, revision?: string, fence?: Fence, table?: TableContinuation,
81
+ firstLine = 1, atLine?: number,
82
+ ) {
83
+ return withWorkspaceFile(root, path, markdownExtensions, async (file, size, stamp, actual) => {
84
+ if ((revision && stamp !== revision) || offset > size) throw new Error("File changed. Reload to read the new version.");
85
+ if (atLine !== undefined) {
86
+ const position = await markdownPreviewPosition(file, size, stamp, actual, atLine);
87
+ offset = position.offset;
88
+ firstLine = position.line;
89
+ fence = position.fence;
90
+ table = position.table;
91
+ }
92
+ const pageBytes = Math.min(markdownPageBytes, size - offset);
93
+ const bytes = Buffer.alloc(Math.min(pageBytes + mathLookaheadBytes, size - offset));
94
+ let length = 0;
95
+ while (length < bytes.length) {
96
+ const { bytesRead } = await file.read(bytes, length, bytes.length - length, offset + length);
97
+ if (!bytesRead) throw new Error("File ended while reading.");
98
+ length += bytesRead;
99
+ }
100
+ const eof = offset + pageBytes === size;
101
+ const prefix = fence ? `${fence.opening}\n` : table ? `${table.header}\n${table.separator}\n` : "";
102
+ const decoder = new StringDecoder("utf8");
103
+ const text = offset + length === size ? decoder.end(bytes) : decoder.write(bytes);
104
+ const current = eof ? text : new StringDecoder("utf8").write(bytes.subarray(0, pageBytes));
105
+ const boundary = eof ? { end: text.length }
106
+ : await continueTable(text, prefix, keepFormulasTogether(text, prefix, chooseBoundary(current, fence)));
107
+ const kept = text.slice(0, boundary.end);
108
+ const encoded = Buffer.from(kept);
109
+ if (!bytes.subarray(0, encoded.length).equals(encoded)) throw new Error("Reader supports UTF-8 text files.");
110
+ const source = offset === 0 ? kept.replace(/^\uFEFF/, "") : kept;
111
+ const suffix = boundary.fence ? `\n${boundary.fence.marker}\n` : "";
112
+ const nextOffset = offset + encoded.length < size ? offset + encoded.length : null;
113
+ return {
114
+ source, markdown: rebaseMarkdownLinks(prefix + source + suffix, root, path),
115
+ nextOffset, offset, revision: stamp, fence: boundary.fence, table: boundary.table,
116
+ firstLine, nextLine: firstLine + lineOffsets(source).length - 2,
117
+ prefixLines: fence ? 1 : table ? 2 : 0,
118
+ };
119
+ });
120
+ }