@jvs-milkdown/exception 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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020-present Mirone
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,7 @@
1
+ # @jvs-milkdown/exception
2
+
3
+ Internal exception for [milkdown](https://milkdown.dev/).
4
+
5
+ # License
6
+
7
+ Milkdown is open sourced software licensed under [MIT license](https://github.com/Milkdown/milkdown/blob/main/LICENSE).
package/lib/code.d.ts ADDED
@@ -0,0 +1,19 @@
1
+ export declare enum ErrorCode {
2
+ docTypeError = "docTypeError",
3
+ contextNotFound = "contextNotFound",
4
+ timerNotFound = "timerNotFound",
5
+ ctxCallOutOfScope = "ctxCallOutOfScope",
6
+ createNodeInParserFail = "createNodeInParserFail",
7
+ stackOverFlow = "stackOverFlow",
8
+ parserMatchError = "parserMatchError",
9
+ serializerMatchError = "serializerMatchError",
10
+ getAtomFromSchemaFail = "getAtomFromSchemaFail",
11
+ expectDomTypeError = "expectDomTypeError",
12
+ callCommandBeforeEditorView = "callCommandBeforeEditorView",
13
+ missingRootElement = "missingRootElement",
14
+ missingNodeInSchema = "missingNodeInSchema",
15
+ missingMarkInSchema = "missingMarkInSchema",
16
+ ctxNotBind = "ctxNotBind",
17
+ missingYjsDoc = "missingYjsDoc"
18
+ }
19
+ //# sourceMappingURL=code.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"code.d.ts","sourceRoot":"","sources":["../src/code.ts"],"names":[],"mappings":"AAAA,oBAAY,SAAS;IACnB,YAAY,iBAAiB;IAC7B,eAAe,oBAAoB;IACnC,aAAa,kBAAkB;IAC/B,iBAAiB,sBAAsB;IACvC,sBAAsB,2BAA2B;IACjD,aAAa,kBAAkB;IAC/B,gBAAgB,qBAAqB;IACrC,oBAAoB,yBAAyB;IAC7C,qBAAqB,0BAA0B;IAC/C,kBAAkB,uBAAuB;IACzC,2BAA2B,gCAAgC;IAC3D,kBAAkB,uBAAuB;IACzC,mBAAmB,wBAAwB;IAC3C,mBAAmB,wBAAwB;IAG3C,UAAU,eAAe;IACzB,aAAa,kBAAkB;CAChC"}
package/lib/error.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ import type { ErrorCode } from './code';
2
+ export declare class MilkdownError extends Error {
3
+ code: string;
4
+ constructor(code: ErrorCode, message: string);
5
+ }
6
+ //# sourceMappingURL=error.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"error.d.ts","sourceRoot":"","sources":["../src/error.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,QAAQ,CAAA;AAEvC,qBAAa,aAAc,SAAQ,KAAK;IAC/B,IAAI,EAAE,MAAM,CAAA;gBACP,IAAI,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM;CAK7C"}
package/lib/index.d.ts ADDED
@@ -0,0 +1,18 @@
1
+ import { MilkdownError } from './error';
2
+ export declare function docTypeError(type: unknown): MilkdownError;
3
+ export declare function contextNotFound(name: string): MilkdownError;
4
+ export declare function timerNotFound(name: string): MilkdownError;
5
+ export declare function ctxCallOutOfScope(): MilkdownError;
6
+ export declare function createNodeInParserFail(nodeType: object, attrs?: unknown, content?: unknown[]): MilkdownError;
7
+ export declare function stackOverFlow(): MilkdownError;
8
+ export declare function parserMatchError(node: unknown): MilkdownError;
9
+ export declare function serializerMatchError(node: unknown): MilkdownError;
10
+ export declare function getAtomFromSchemaFail(type: 'mark' | 'node', name: string): MilkdownError;
11
+ export declare function expectDomTypeError(node: unknown): MilkdownError;
12
+ export declare function callCommandBeforeEditorView(): MilkdownError;
13
+ export declare function missingRootElement(): MilkdownError;
14
+ export declare function missingNodeInSchema(name: string): MilkdownError;
15
+ export declare function missingMarkInSchema(name: string): MilkdownError;
16
+ export declare function ctxNotBind(): MilkdownError;
17
+ export declare function missingYjsDoc(): MilkdownError;
18
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,MAAM,SAAS,CAAA;AAOvC,wBAAgB,YAAY,CAAC,IAAI,EAAE,OAAO,iBAKzC;AAED,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,iBAK3C;AAED,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,iBAKzC;AAED,wBAAgB,iBAAiB,kBAKhC;AAED,wBAAgB,sBAAsB,CACpC,QAAQ,EAAE,MAAM,EAChB,KAAK,CAAC,EAAE,OAAO,EACf,OAAO,CAAC,EAAE,OAAO,EAAE,iBAqEpB;AAED,wBAAgB,aAAa,kBAK5B;AAED,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,OAAO,iBAK7C;AAED,wBAAgB,oBAAoB,CAAC,IAAI,EAAE,OAAO,iBAKjD;AAED,wBAAgB,qBAAqB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,IAAI,EAAE,MAAM,iBAKxE;AAED,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,OAAO,iBAK/C;AAED,wBAAgB,2BAA2B,kBAK1C;AAED,wBAAgB,kBAAkB,kBAKjC;AAED,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,MAAM,iBAK/C;AAED,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,MAAM,iBAK/C;AAED,wBAAgB,UAAU,kBAKzB;AAED,wBAAgB,aAAa,kBAK5B"}
package/lib/index.js ADDED
@@ -0,0 +1,114 @@
1
+ //#region src/code.ts
2
+ var ErrorCode = /* @__PURE__ */ function(ErrorCode) {
3
+ ErrorCode["docTypeError"] = "docTypeError";
4
+ ErrorCode["contextNotFound"] = "contextNotFound";
5
+ ErrorCode["timerNotFound"] = "timerNotFound";
6
+ ErrorCode["ctxCallOutOfScope"] = "ctxCallOutOfScope";
7
+ ErrorCode["createNodeInParserFail"] = "createNodeInParserFail";
8
+ ErrorCode["stackOverFlow"] = "stackOverFlow";
9
+ ErrorCode["parserMatchError"] = "parserMatchError";
10
+ ErrorCode["serializerMatchError"] = "serializerMatchError";
11
+ ErrorCode["getAtomFromSchemaFail"] = "getAtomFromSchemaFail";
12
+ ErrorCode["expectDomTypeError"] = "expectDomTypeError";
13
+ ErrorCode["callCommandBeforeEditorView"] = "callCommandBeforeEditorView";
14
+ ErrorCode["missingRootElement"] = "missingRootElement";
15
+ ErrorCode["missingNodeInSchema"] = "missingNodeInSchema";
16
+ ErrorCode["missingMarkInSchema"] = "missingMarkInSchema";
17
+ ErrorCode["ctxNotBind"] = "ctxNotBind";
18
+ ErrorCode["missingYjsDoc"] = "missingYjsDoc";
19
+ return ErrorCode;
20
+ }({});
21
+ //#endregion
22
+ //#region src/error.ts
23
+ var MilkdownError = class extends Error {
24
+ constructor(code, message) {
25
+ super(message);
26
+ this.name = "MilkdownError";
27
+ this.code = code;
28
+ }
29
+ };
30
+ //#endregion
31
+ //#region src/index.ts
32
+ var functionReplacer = (_, value) => typeof value === "function" ? "[Function]" : value;
33
+ var stringify = (x) => JSON.stringify(x, functionReplacer);
34
+ function docTypeError(type) {
35
+ return new MilkdownError(ErrorCode.docTypeError, `Doc type error, unsupported type: ${stringify(type)}`);
36
+ }
37
+ function contextNotFound(name) {
38
+ return new MilkdownError(ErrorCode.contextNotFound, `Context "${name}" not found, do you forget to inject it?`);
39
+ }
40
+ function timerNotFound(name) {
41
+ return new MilkdownError(ErrorCode.timerNotFound, `Timer "${name}" not found, do you forget to record it?`);
42
+ }
43
+ function ctxCallOutOfScope() {
44
+ return new MilkdownError(ErrorCode.ctxCallOutOfScope, "Should not call a context out of the plugin.");
45
+ }
46
+ function createNodeInParserFail(nodeType, attrs, content) {
47
+ const heading = `Cannot create node for ${"name" in nodeType ? nodeType.name : nodeType}`;
48
+ const serialize = (x) => {
49
+ if (x == null) return "null";
50
+ if (Array.isArray(x)) return `[${x.map(serialize).join(", ")}]`;
51
+ if (typeof x === "object") {
52
+ if ("toJSON" in x && typeof x.toJSON === "function") return JSON.stringify(x.toJSON());
53
+ if ("spec" in x) return JSON.stringify(x.spec);
54
+ return JSON.stringify(x);
55
+ }
56
+ if (typeof x === "string" || typeof x === "number" || typeof x === "boolean") return JSON.stringify(x);
57
+ if (typeof x === "function") return `[Function: ${x.name || "anonymous"}]`;
58
+ try {
59
+ return String(x);
60
+ } catch {
61
+ return "[Unserializable]";
62
+ }
63
+ };
64
+ const messages = [
65
+ ["[Description]", heading],
66
+ ["[Attributes]", attrs],
67
+ ["[Content]", (content ?? []).map((node) => {
68
+ if (!node) return "null";
69
+ if (typeof node === "object" && "type" in node) return `${node}`;
70
+ return serialize(node);
71
+ })]
72
+ ].reduce((acc, [title, value]) => {
73
+ const message = `${title}: ${serialize(value)}.`;
74
+ return acc.concat(message);
75
+ }, []);
76
+ return new MilkdownError(ErrorCode.createNodeInParserFail, messages.join("\n"));
77
+ }
78
+ function stackOverFlow() {
79
+ return new MilkdownError(ErrorCode.stackOverFlow, "Stack over flow, cannot pop on an empty stack.");
80
+ }
81
+ function parserMatchError(node) {
82
+ return new MilkdownError(ErrorCode.parserMatchError, `Cannot match target parser for node: ${stringify(node)}.`);
83
+ }
84
+ function serializerMatchError(node) {
85
+ return new MilkdownError(ErrorCode.serializerMatchError, `Cannot match target serializer for node: ${stringify(node)}.`);
86
+ }
87
+ function getAtomFromSchemaFail(type, name) {
88
+ return new MilkdownError(ErrorCode.getAtomFromSchemaFail, `Cannot get ${type}: ${name} from schema.`);
89
+ }
90
+ function expectDomTypeError(node) {
91
+ return new MilkdownError(ErrorCode.expectDomTypeError, `Expect to be a dom, but get: ${stringify(node)}.`);
92
+ }
93
+ function callCommandBeforeEditorView() {
94
+ return new MilkdownError(ErrorCode.callCommandBeforeEditorView, "You're trying to call a command before editor view initialized, make sure to get commandManager from ctx after editor view has been initialized");
95
+ }
96
+ function missingRootElement() {
97
+ return new MilkdownError(ErrorCode.missingRootElement, "Missing root element, milkdown cannot find root element of the editor.");
98
+ }
99
+ function missingNodeInSchema(name) {
100
+ return new MilkdownError(ErrorCode.missingNodeInSchema, `Missing node in schema, milkdown cannot find "${name}" in schema.`);
101
+ }
102
+ function missingMarkInSchema(name) {
103
+ return new MilkdownError(ErrorCode.missingMarkInSchema, `Missing mark in schema, milkdown cannot find "${name}" in schema.`);
104
+ }
105
+ function ctxNotBind() {
106
+ return new MilkdownError(ErrorCode.ctxNotBind, "Context not bind, please make sure the plugin has been initialized.");
107
+ }
108
+ function missingYjsDoc() {
109
+ return new MilkdownError(ErrorCode.missingYjsDoc, "Missing yjs doc, please make sure you have bind one.");
110
+ }
111
+ //#endregion
112
+ export { callCommandBeforeEditorView, contextNotFound, createNodeInParserFail, ctxCallOutOfScope, ctxNotBind, docTypeError, expectDomTypeError, getAtomFromSchemaFail, missingMarkInSchema, missingNodeInSchema, missingRootElement, missingYjsDoc, parserMatchError, serializerMatchError, stackOverFlow, timerNotFound };
113
+
114
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":[],"sources":["../src/code.ts","../src/error.ts","../src/index.ts"],"sourcesContent":["export enum ErrorCode {\n docTypeError = 'docTypeError',\n contextNotFound = 'contextNotFound',\n timerNotFound = 'timerNotFound',\n ctxCallOutOfScope = 'ctxCallOutOfScope',\n createNodeInParserFail = 'createNodeInParserFail',\n stackOverFlow = 'stackOverFlow',\n parserMatchError = 'parserMatchError',\n serializerMatchError = 'serializerMatchError',\n getAtomFromSchemaFail = 'getAtomFromSchemaFail',\n expectDomTypeError = 'expectDomTypeError',\n callCommandBeforeEditorView = 'callCommandBeforeEditorView',\n missingRootElement = 'missingRootElement',\n missingNodeInSchema = 'missingNodeInSchema',\n missingMarkInSchema = 'missingMarkInSchema',\n\n // collab plugin\n ctxNotBind = 'ctxNotBind',\n missingYjsDoc = 'missingYjsDoc',\n}\n","import type { ErrorCode } from './code'\n\nexport class MilkdownError extends Error {\n public code: string\n constructor(code: ErrorCode, message: string) {\n super(message)\n this.name = 'MilkdownError'\n this.code = code\n }\n}\n","import { ErrorCode } from './code'\nimport { MilkdownError } from './error'\n\nconst functionReplacer = (_: string, value: unknown) =>\n typeof value === 'function' ? '[Function]' : value\n\nconst stringify = (x: unknown): string => JSON.stringify(x, functionReplacer)\n\nexport function docTypeError(type: unknown) {\n return new MilkdownError(\n ErrorCode.docTypeError,\n `Doc type error, unsupported type: ${stringify(type)}`\n )\n}\n\nexport function contextNotFound(name: string) {\n return new MilkdownError(\n ErrorCode.contextNotFound,\n `Context \"${name}\" not found, do you forget to inject it?`\n )\n}\n\nexport function timerNotFound(name: string) {\n return new MilkdownError(\n ErrorCode.timerNotFound,\n `Timer \"${name}\" not found, do you forget to record it?`\n )\n}\n\nexport function ctxCallOutOfScope() {\n return new MilkdownError(\n ErrorCode.ctxCallOutOfScope,\n 'Should not call a context out of the plugin.'\n )\n}\n\nexport function createNodeInParserFail(\n nodeType: object,\n attrs?: unknown,\n content?: unknown[]\n) {\n const nodeTypeName = 'name' in nodeType ? nodeType.name : nodeType\n const heading = `Cannot create node for ${nodeTypeName}`\n const serialize = (x: unknown): string => {\n if (x == null) return 'null'\n\n if (Array.isArray(x)) {\n return `[${x.map(serialize).join(', ')}]`\n }\n\n if (typeof x === 'object') {\n if ('toJSON' in x && typeof (x as any).toJSON === 'function') {\n return JSON.stringify((x as any).toJSON())\n }\n\n if ('spec' in x) {\n return JSON.stringify((x as any).spec)\n }\n\n return JSON.stringify(x)\n }\n\n if (\n typeof x === 'string' ||\n typeof x === 'number' ||\n typeof x === 'boolean'\n ) {\n return JSON.stringify(x)\n }\n\n if (typeof x === 'function') {\n return `[Function: ${(x as Function).name || 'anonymous'}]`\n }\n\n try {\n return String(x)\n } catch {\n return '[Unserializable]'\n }\n }\n\n const headingMessage = ['[Description]', heading] as const\n const attrsMessage = ['[Attributes]', attrs] as const\n const contentMessage = [\n '[Content]',\n (content ?? []).map((node) => {\n if (!node) return 'null'\n\n if (typeof node === 'object' && 'type' in node) {\n return `${node}`\n }\n\n return serialize(node)\n }),\n ] as const\n\n const messages = [headingMessage, attrsMessage, contentMessage].reduce(\n (acc, [title, value]) => {\n const message = `${title}: ${serialize(value)}.`\n return acc.concat(message)\n },\n [] as string[]\n )\n\n return new MilkdownError(\n ErrorCode.createNodeInParserFail,\n messages.join('\\n')\n )\n}\n\nexport function stackOverFlow() {\n return new MilkdownError(\n ErrorCode.stackOverFlow,\n 'Stack over flow, cannot pop on an empty stack.'\n )\n}\n\nexport function parserMatchError(node: unknown) {\n return new MilkdownError(\n ErrorCode.parserMatchError,\n `Cannot match target parser for node: ${stringify(node)}.`\n )\n}\n\nexport function serializerMatchError(node: unknown) {\n return new MilkdownError(\n ErrorCode.serializerMatchError,\n `Cannot match target serializer for node: ${stringify(node)}.`\n )\n}\n\nexport function getAtomFromSchemaFail(type: 'mark' | 'node', name: string) {\n return new MilkdownError(\n ErrorCode.getAtomFromSchemaFail,\n `Cannot get ${type}: ${name} from schema.`\n )\n}\n\nexport function expectDomTypeError(node: unknown) {\n return new MilkdownError(\n ErrorCode.expectDomTypeError,\n `Expect to be a dom, but get: ${stringify(node)}.`\n )\n}\n\nexport function callCommandBeforeEditorView() {\n return new MilkdownError(\n ErrorCode.callCommandBeforeEditorView,\n \"You're trying to call a command before editor view initialized, make sure to get commandManager from ctx after editor view has been initialized\"\n )\n}\n\nexport function missingRootElement() {\n return new MilkdownError(\n ErrorCode.missingRootElement,\n 'Missing root element, milkdown cannot find root element of the editor.'\n )\n}\n\nexport function missingNodeInSchema(name: string) {\n return new MilkdownError(\n ErrorCode.missingNodeInSchema,\n `Missing node in schema, milkdown cannot find \"${name}\" in schema.`\n )\n}\n\nexport function missingMarkInSchema(name: string) {\n return new MilkdownError(\n ErrorCode.missingMarkInSchema,\n `Missing mark in schema, milkdown cannot find \"${name}\" in schema.`\n )\n}\n\nexport function ctxNotBind() {\n return new MilkdownError(\n ErrorCode.ctxNotBind,\n 'Context not bind, please make sure the plugin has been initialized.'\n )\n}\n\nexport function missingYjsDoc() {\n return new MilkdownError(\n ErrorCode.missingYjsDoc,\n 'Missing yjs doc, please make sure you have bind one.'\n )\n}\n"],"mappings":"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"}
@@ -0,0 +1 @@
1
+ {"fileNames":["../../../node_modules/.pnpm/typescript@6.0.2/node_modules/typescript/lib/lib.es5.d.ts","../../../node_modules/.pnpm/typescript@6.0.2/node_modules/typescript/lib/lib.es2015.d.ts","../../../node_modules/.pnpm/typescript@6.0.2/node_modules/typescript/lib/lib.es2016.d.ts","../../../node_modules/.pnpm/typescript@6.0.2/node_modules/typescript/lib/lib.es2017.d.ts","../../../node_modules/.pnpm/typescript@6.0.2/node_modules/typescript/lib/lib.es2018.d.ts","../../../node_modules/.pnpm/typescript@6.0.2/node_modules/typescript/lib/lib.es2019.d.ts","../../../node_modules/.pnpm/typescript@6.0.2/node_modules/typescript/lib/lib.es2020.d.ts","../../../node_modules/.pnpm/typescript@6.0.2/node_modules/typescript/lib/lib.es2021.d.ts","../../../node_modules/.pnpm/typescript@6.0.2/node_modules/typescript/lib/lib.es2022.d.ts","../../../node_modules/.pnpm/typescript@6.0.2/node_modules/typescript/lib/lib.dom.d.ts","../../../node_modules/.pnpm/typescript@6.0.2/node_modules/typescript/lib/lib.es2015.core.d.ts","../../../node_modules/.pnpm/typescript@6.0.2/node_modules/typescript/lib/lib.es2015.collection.d.ts","../../../node_modules/.pnpm/typescript@6.0.2/node_modules/typescript/lib/lib.es2015.generator.d.ts","../../../node_modules/.pnpm/typescript@6.0.2/node_modules/typescript/lib/lib.es2015.iterable.d.ts","../../../node_modules/.pnpm/typescript@6.0.2/node_modules/typescript/lib/lib.es2015.promise.d.ts","../../../node_modules/.pnpm/typescript@6.0.2/node_modules/typescript/lib/lib.es2015.proxy.d.ts","../../../node_modules/.pnpm/typescript@6.0.2/node_modules/typescript/lib/lib.es2015.reflect.d.ts","../../../node_modules/.pnpm/typescript@6.0.2/node_modules/typescript/lib/lib.es2015.symbol.d.ts","../../../node_modules/.pnpm/typescript@6.0.2/node_modules/typescript/lib/lib.es2015.symbol.wellknown.d.ts","../../../node_modules/.pnpm/typescript@6.0.2/node_modules/typescript/lib/lib.es2016.array.include.d.ts","../../../node_modules/.pnpm/typescript@6.0.2/node_modules/typescript/lib/lib.es2016.intl.d.ts","../../../node_modules/.pnpm/typescript@6.0.2/node_modules/typescript/lib/lib.es2017.arraybuffer.d.ts","../../../node_modules/.pnpm/typescript@6.0.2/node_modules/typescript/lib/lib.es2017.date.d.ts","../../../node_modules/.pnpm/typescript@6.0.2/node_modules/typescript/lib/lib.es2017.object.d.ts","../../../node_modules/.pnpm/typescript@6.0.2/node_modules/typescript/lib/lib.es2017.sharedmemory.d.ts","../../../node_modules/.pnpm/typescript@6.0.2/node_modules/typescript/lib/lib.es2017.string.d.ts","../../../node_modules/.pnpm/typescript@6.0.2/node_modules/typescript/lib/lib.es2017.intl.d.ts","../../../node_modules/.pnpm/typescript@6.0.2/node_modules/typescript/lib/lib.es2017.typedarrays.d.ts","../../../node_modules/.pnpm/typescript@6.0.2/node_modules/typescript/lib/lib.es2018.asyncgenerator.d.ts","../../../node_modules/.pnpm/typescript@6.0.2/node_modules/typescript/lib/lib.es2018.asynciterable.d.ts","../../../node_modules/.pnpm/typescript@6.0.2/node_modules/typescript/lib/lib.es2018.intl.d.ts","../../../node_modules/.pnpm/typescript@6.0.2/node_modules/typescript/lib/lib.es2018.promise.d.ts","../../../node_modules/.pnpm/typescript@6.0.2/node_modules/typescript/lib/lib.es2018.regexp.d.ts","../../../node_modules/.pnpm/typescript@6.0.2/node_modules/typescript/lib/lib.es2019.array.d.ts","../../../node_modules/.pnpm/typescript@6.0.2/node_modules/typescript/lib/lib.es2019.object.d.ts","../../../node_modules/.pnpm/typescript@6.0.2/node_modules/typescript/lib/lib.es2019.string.d.ts","../../../node_modules/.pnpm/typescript@6.0.2/node_modules/typescript/lib/lib.es2019.symbol.d.ts","../../../node_modules/.pnpm/typescript@6.0.2/node_modules/typescript/lib/lib.es2019.intl.d.ts","../../../node_modules/.pnpm/typescript@6.0.2/node_modules/typescript/lib/lib.es2020.bigint.d.ts","../../../node_modules/.pnpm/typescript@6.0.2/node_modules/typescript/lib/lib.es2020.date.d.ts","../../../node_modules/.pnpm/typescript@6.0.2/node_modules/typescript/lib/lib.es2020.promise.d.ts","../../../node_modules/.pnpm/typescript@6.0.2/node_modules/typescript/lib/lib.es2020.sharedmemory.d.ts","../../../node_modules/.pnpm/typescript@6.0.2/node_modules/typescript/lib/lib.es2020.string.d.ts","../../../node_modules/.pnpm/typescript@6.0.2/node_modules/typescript/lib/lib.es2020.symbol.wellknown.d.ts","../../../node_modules/.pnpm/typescript@6.0.2/node_modules/typescript/lib/lib.es2020.intl.d.ts","../../../node_modules/.pnpm/typescript@6.0.2/node_modules/typescript/lib/lib.es2020.number.d.ts","../../../node_modules/.pnpm/typescript@6.0.2/node_modules/typescript/lib/lib.es2021.promise.d.ts","../../../node_modules/.pnpm/typescript@6.0.2/node_modules/typescript/lib/lib.es2021.string.d.ts","../../../node_modules/.pnpm/typescript@6.0.2/node_modules/typescript/lib/lib.es2021.weakref.d.ts","../../../node_modules/.pnpm/typescript@6.0.2/node_modules/typescript/lib/lib.es2021.intl.d.ts","../../../node_modules/.pnpm/typescript@6.0.2/node_modules/typescript/lib/lib.es2022.array.d.ts","../../../node_modules/.pnpm/typescript@6.0.2/node_modules/typescript/lib/lib.es2022.error.d.ts","../../../node_modules/.pnpm/typescript@6.0.2/node_modules/typescript/lib/lib.es2022.intl.d.ts","../../../node_modules/.pnpm/typescript@6.0.2/node_modules/typescript/lib/lib.es2022.object.d.ts","../../../node_modules/.pnpm/typescript@6.0.2/node_modules/typescript/lib/lib.es2022.string.d.ts","../../../node_modules/.pnpm/typescript@6.0.2/node_modules/typescript/lib/lib.es2022.regexp.d.ts","../../../node_modules/.pnpm/typescript@6.0.2/node_modules/typescript/lib/lib.decorators.d.ts","../../../node_modules/.pnpm/typescript@6.0.2/node_modules/typescript/lib/lib.decorators.legacy.d.ts","../src/code.ts","../src/error.ts","../src/index.ts"],"fileIdsList":[[59],[59,60]],"fileInfos":[{"version":"bcd24271a113971ba9eb71ff8cb01bc6b0f872a85c23fdbe5d93065b375933cd","affectsGlobalScope":true,"impliedFormat":1},{"version":"3f88bedbeb09c6f5a6645cb24c7c55f1aa22d19ae96c8e6959cbd8b85a707bc6","impliedFormat":1},{"version":"7fe93b39b810eadd916be8db880dd7f0f7012a5cc6ffb62de8f62a2117fa6f1f","impliedFormat":1},{"version":"bb0074cc08b84a2374af33d8bf044b80851ccc9e719a5e202eacf40db2c31600","impliedFormat":1},{"version":"1a7daebe4f45fb03d9ec53d60008fbf9ac45a697fdc89e4ce218bc94b94f94d6","impliedFormat":1},{"version":"f94b133a3cb14a288803be545ac2683e0d0ff6661bcd37e31aaaec54fc382aed","impliedFormat":1},{"version":"f59d0650799f8782fd74cf73c19223730c6d1b9198671b1c5b3a38e1188b5953","impliedFormat":1},{"version":"8a15b4607d9a499e2dbeed9ec0d3c0d7372c850b2d5f1fb259e8f6d41d468a84","impliedFormat":1},{"version":"26e0fe14baee4e127f4365d1ae0b276f400562e45e19e35fd2d4c296684715e6","impliedFormat":1},{"version":"d6b1eba8496bdd0eed6fc8a685768fe01b2da4a0388b5fe7df558290bffcf32f","affectsGlobalScope":true,"impliedFormat":1},{"version":"eadcffda2aa84802c73938e589b9e58248d74c59cb7fcbca6474e3435ac15504","affectsGlobalScope":true,"impliedFormat":1},{"version":"105ba8ff7ba746404fe1a2e189d1d3d2e0eb29a08c18dded791af02f29fb4711","affectsGlobalScope":true,"impliedFormat":1},{"version":"00343ca5b2e3d48fa5df1db6e32ea2a59afab09590274a6cccb1dbae82e60c7c","affectsGlobalScope":true,"impliedFormat":1},{"version":"ebd9f816d4002697cb2864bea1f0b70a103124e18a8cd9645eeccc09bdf80ab4","affectsGlobalScope":true,"impliedFormat":1},{"version":"2c1afac30a01772cd2a9a298a7ce7706b5892e447bb46bdbeef720f7b5da77ad","affectsGlobalScope":true,"impliedFormat":1},{"version":"7b0225f483e4fa685625ebe43dd584bb7973bbd84e66a6ba7bbe175ee1048b4f","affectsGlobalScope":true,"impliedFormat":1},{"version":"c0a4b8ac6ce74679c1da2b3795296f5896e31c38e888469a8e0f99dc3305de60","affectsGlobalScope":true,"impliedFormat":1},{"version":"3084a7b5f569088e0146533a00830e206565de65cae2239509168b11434cd84f","affectsGlobalScope":true,"impliedFormat":1},{"version":"c5079c53f0f141a0698faa903e76cb41cd664e3efb01cc17a5c46ec2eb0bef42","affectsGlobalScope":true,"impliedFormat":1},{"version":"32cafbc484dea6b0ab62cf8473182bbcb23020d70845b406f80b7526f38ae862","affectsGlobalScope":true,"impliedFormat":1},{"version":"fca4cdcb6d6c5ef18a869003d02c9f0fd95df8cfaf6eb431cd3376bc034cad36","affectsGlobalScope":true,"impliedFormat":1},{"version":"b93ec88115de9a9dc1b602291b85baf825c85666bf25985cc5f698073892b467","affectsGlobalScope":true,"impliedFormat":1},{"version":"f5c06dcc3fe849fcb297c247865a161f995cc29de7aa823afdd75aaaddc1419b","affectsGlobalScope":true,"impliedFormat":1},{"version":"b77e16112127a4b169ef0b8c3a4d730edf459c5f25fe52d5e436a6919206c4d7","affectsGlobalScope":true,"impliedFormat":1},{"version":"fbffd9337146eff822c7c00acbb78b01ea7ea23987f6c961eba689349e744f8c","affectsGlobalScope":true,"impliedFormat":1},{"version":"a995c0e49b721312f74fdfb89e4ba29bd9824c770bbb4021d74d2bf560e4c6bd","affectsGlobalScope":true,"impliedFormat":1},{"version":"c7b3542146734342e440a84b213384bfa188835537ddbda50d30766f0593aff9","affectsGlobalScope":true,"impliedFormat":1},{"version":"ce6180fa19b1cccd07ee7f7dbb9a367ac19c0ed160573e4686425060b6df7f57","affectsGlobalScope":true,"impliedFormat":1},{"version":"3f02e2476bccb9dbe21280d6090f0df17d2f66b74711489415a8aa4df73c9675","affectsGlobalScope":true,"impliedFormat":1},{"version":"45e3ab34c1c013c8ab2dc1ba4c80c780744b13b5676800ae2e3be27ae862c40c","affectsGlobalScope":true,"impliedFormat":1},{"version":"805c86f6cca8d7702a62a844856dbaa2a3fd2abef0536e65d48732441dde5b5b","affectsGlobalScope":true,"impliedFormat":1},{"version":"e42e397f1a5a77994f0185fd1466520691456c772d06bf843e5084ceb879a0ad","affectsGlobalScope":true,"impliedFormat":1},{"version":"f4c2b41f90c95b1c532ecc874bd3c111865793b23aebcc1c3cbbabcd5d76ffb0","affectsGlobalScope":true,"impliedFormat":1},{"version":"ab26191cfad5b66afa11b8bf935ef1cd88fabfcb28d30b2dfa6fad877d050332","affectsGlobalScope":true,"impliedFormat":1},{"version":"2088bc26531e38fb05eedac2951480db5309f6be3fa4a08d2221abb0f5b4200d","affectsGlobalScope":true,"impliedFormat":1},{"version":"cb9d366c425fea79716a8fb3af0d78e6b22ebbab3bd64d25063b42dc9f531c1e","affectsGlobalScope":true,"impliedFormat":1},{"version":"500934a8089c26d57ebdb688fc9757389bb6207a3c8f0674d68efa900d2abb34","affectsGlobalScope":true,"impliedFormat":1},{"version":"689da16f46e647cef0d64b0def88910e818a5877ca5379ede156ca3afb780ac3","affectsGlobalScope":true,"impliedFormat":1},{"version":"bc21cc8b6fee4f4c2440d08035b7ea3c06b3511314c8bab6bef7a92de58a2593","affectsGlobalScope":true,"impliedFormat":1},{"version":"7ca53d13d2957003abb47922a71866ba7cb2068f8d154877c596d63c359fed25","affectsGlobalScope":true,"impliedFormat":1},{"version":"54725f8c4df3d900cb4dac84b64689ce29548da0b4e9b7c2de61d41c79293611","affectsGlobalScope":true,"impliedFormat":1},{"version":"e5594bc3076ac29e6c1ebda77939bc4c8833de72f654b6e376862c0473199323","affectsGlobalScope":true,"impliedFormat":1},{"version":"2f3eb332c2d73e729f3364fcc0c2b375e72a121e8157d25a82d67a138c83a95c","affectsGlobalScope":true,"impliedFormat":1},{"version":"6f4427f9642ce8d500970e4e69d1397f64072ab73b97e476b4002a646ac743b1","affectsGlobalScope":true,"impliedFormat":1},{"version":"48915f327cd1dea4d7bd358d9dc7732f58f9e1626a29cc0c05c8c692419d9bb7","affectsGlobalScope":true,"impliedFormat":1},{"version":"b7bf9377723203b5a6a4b920164df22d56a43f593269ba6ae1fdc97774b68855","affectsGlobalScope":true,"impliedFormat":1},{"version":"db9709688f82c9e5f65a119c64d835f906efe5f559d08b11642d56eb85b79357","affectsGlobalScope":true,"impliedFormat":1},{"version":"4b25b8c874acd1a4cf8444c3617e037d444d19080ac9f634b405583fd10ce1f7","affectsGlobalScope":true,"impliedFormat":1},{"version":"37be57d7c90cf1f8112ee2636a068d8fd181289f82b744160ec56a7dc158a9f5","affectsGlobalScope":true,"impliedFormat":1},{"version":"a917a49ac94cd26b754ab84e113369a75d1a47a710661d7cd25e961cc797065f","affectsGlobalScope":true,"impliedFormat":1},{"version":"6d3261badeb7843d157ef3e6f5d1427d0eeb0af0cf9df84a62cfd29fd47ac86e","affectsGlobalScope":true,"impliedFormat":1},{"version":"195daca651dde22f2167ac0d0a05e215308119a3100f5e6268e8317d05a92526","affectsGlobalScope":true,"impliedFormat":1},{"version":"8b11e4285cd2bb164a4dc09248bdec69e9842517db4ca47c1ba913011e44ff2f","affectsGlobalScope":true,"impliedFormat":1},{"version":"0508571a52475e245b02bc50fa1394065a0a3d05277fbf5120c3784b85651799","affectsGlobalScope":true,"impliedFormat":1},{"version":"8f9af488f510c3015af3cc8c267a9e9d96c4dd38a1fdff0e11dc5a544711415b","affectsGlobalScope":true,"impliedFormat":1},{"version":"fc611fea8d30ea72c6bbfb599c9b4d393ce22e2f5bfef2172534781e7d138104","affectsGlobalScope":true,"impliedFormat":1},{"version":"1ce14b81c5cc821994aa8ec1d42b220dd41b27fcc06373bce3958af7421b77d4","affectsGlobalScope":true,"impliedFormat":1},{"version":"b3a048b3e9302ef9a34ef4ebb9aecfb28b66abb3bce577206a79fee559c230da","affectsGlobalScope":true,"impliedFormat":1},{"version":"c858c40c152f75cad2b0823350435d2fdc6e2937b091d9fc52f16f356ad90fbd","signature":"16976e5481a0abbe56fa82010ecd47c152e76d9c7ecb5977d68a1b2c4a785c91"},{"version":"080b30bcb35f812e7489260f12b560cf73f1974b471635e983c84eecfa1ccb60","signature":"749f3bd08d4fc5cc1c95e3c3b07dede327898a51cf6fa7f156456c0d46aae680"},{"version":"da260198e120507c26bf1adaeaeebf81d3c08eec386ce1e36c1e14512be62e9f","signature":"7797184093e9b9f6203d001829de6f91d142497913c67a8d0c2a5b25de005a68"}],"root":[[59,61]],"options":{"composite":true,"declaration":true,"declarationMap":true,"emitDeclarationOnly":true,"esModuleInterop":true,"module":99,"noEmitOnError":true,"noFallthroughCasesInSwitch":true,"noImplicitOverride":true,"noImplicitReturns":true,"noPropertyAccessFromIndexSignature":false,"noUncheckedIndexedAccess":true,"noUnusedLocals":true,"noUnusedParameters":true,"outDir":"./","rootDir":"../src","skipLibCheck":true,"sourceMap":true,"strict":true,"target":5,"tsBuildInfoFile":"./tsconfig.tsbuildinfo","useUnknownInCatchVariables":true,"verbatimModuleSyntax":true},"referencedMap":[[60,1],[61,2]],"latestChangedDtsFile":"./index.d.ts","version":"6.0.2"}
package/package.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "@jvs-milkdown/exception",
3
+ "version": "1.0.0",
4
+ "license": "MIT",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/Milkdown/milkdown.git",
8
+ "directory": "packages/exception"
9
+ },
10
+ "files": [
11
+ "lib",
12
+ "src"
13
+ ],
14
+ "type": "module",
15
+ "sideEffects": false,
16
+ "main": "./lib/index.js",
17
+ "exports": {
18
+ ".": {
19
+ "import": "./lib/index.js",
20
+ "types": "./lib/index.d.ts"
21
+ }
22
+ },
23
+ "scripts": {
24
+ "build": "vite build"
25
+ },
26
+ "types": "./lib/index.d.ts"
27
+ }
package/src/code.ts ADDED
@@ -0,0 +1,20 @@
1
+ export enum ErrorCode {
2
+ docTypeError = 'docTypeError',
3
+ contextNotFound = 'contextNotFound',
4
+ timerNotFound = 'timerNotFound',
5
+ ctxCallOutOfScope = 'ctxCallOutOfScope',
6
+ createNodeInParserFail = 'createNodeInParserFail',
7
+ stackOverFlow = 'stackOverFlow',
8
+ parserMatchError = 'parserMatchError',
9
+ serializerMatchError = 'serializerMatchError',
10
+ getAtomFromSchemaFail = 'getAtomFromSchemaFail',
11
+ expectDomTypeError = 'expectDomTypeError',
12
+ callCommandBeforeEditorView = 'callCommandBeforeEditorView',
13
+ missingRootElement = 'missingRootElement',
14
+ missingNodeInSchema = 'missingNodeInSchema',
15
+ missingMarkInSchema = 'missingMarkInSchema',
16
+
17
+ // collab plugin
18
+ ctxNotBind = 'ctxNotBind',
19
+ missingYjsDoc = 'missingYjsDoc',
20
+ }
package/src/error.ts ADDED
@@ -0,0 +1,10 @@
1
+ import type { ErrorCode } from './code'
2
+
3
+ export class MilkdownError extends Error {
4
+ public code: string
5
+ constructor(code: ErrorCode, message: string) {
6
+ super(message)
7
+ this.name = 'MilkdownError'
8
+ this.code = code
9
+ }
10
+ }
package/src/index.ts ADDED
@@ -0,0 +1,186 @@
1
+ import { ErrorCode } from './code'
2
+ import { MilkdownError } from './error'
3
+
4
+ const functionReplacer = (_: string, value: unknown) =>
5
+ typeof value === 'function' ? '[Function]' : value
6
+
7
+ const stringify = (x: unknown): string => JSON.stringify(x, functionReplacer)
8
+
9
+ export function docTypeError(type: unknown) {
10
+ return new MilkdownError(
11
+ ErrorCode.docTypeError,
12
+ `Doc type error, unsupported type: ${stringify(type)}`
13
+ )
14
+ }
15
+
16
+ export function contextNotFound(name: string) {
17
+ return new MilkdownError(
18
+ ErrorCode.contextNotFound,
19
+ `Context "${name}" not found, do you forget to inject it?`
20
+ )
21
+ }
22
+
23
+ export function timerNotFound(name: string) {
24
+ return new MilkdownError(
25
+ ErrorCode.timerNotFound,
26
+ `Timer "${name}" not found, do you forget to record it?`
27
+ )
28
+ }
29
+
30
+ export function ctxCallOutOfScope() {
31
+ return new MilkdownError(
32
+ ErrorCode.ctxCallOutOfScope,
33
+ 'Should not call a context out of the plugin.'
34
+ )
35
+ }
36
+
37
+ export function createNodeInParserFail(
38
+ nodeType: object,
39
+ attrs?: unknown,
40
+ content?: unknown[]
41
+ ) {
42
+ const nodeTypeName = 'name' in nodeType ? nodeType.name : nodeType
43
+ const heading = `Cannot create node for ${nodeTypeName}`
44
+ const serialize = (x: unknown): string => {
45
+ if (x == null) return 'null'
46
+
47
+ if (Array.isArray(x)) {
48
+ return `[${x.map(serialize).join(', ')}]`
49
+ }
50
+
51
+ if (typeof x === 'object') {
52
+ if ('toJSON' in x && typeof (x as any).toJSON === 'function') {
53
+ return JSON.stringify((x as any).toJSON())
54
+ }
55
+
56
+ if ('spec' in x) {
57
+ return JSON.stringify((x as any).spec)
58
+ }
59
+
60
+ return JSON.stringify(x)
61
+ }
62
+
63
+ if (
64
+ typeof x === 'string' ||
65
+ typeof x === 'number' ||
66
+ typeof x === 'boolean'
67
+ ) {
68
+ return JSON.stringify(x)
69
+ }
70
+
71
+ if (typeof x === 'function') {
72
+ return `[Function: ${(x as Function).name || 'anonymous'}]`
73
+ }
74
+
75
+ try {
76
+ return String(x)
77
+ } catch {
78
+ return '[Unserializable]'
79
+ }
80
+ }
81
+
82
+ const headingMessage = ['[Description]', heading] as const
83
+ const attrsMessage = ['[Attributes]', attrs] as const
84
+ const contentMessage = [
85
+ '[Content]',
86
+ (content ?? []).map((node) => {
87
+ if (!node) return 'null'
88
+
89
+ if (typeof node === 'object' && 'type' in node) {
90
+ return `${node}`
91
+ }
92
+
93
+ return serialize(node)
94
+ }),
95
+ ] as const
96
+
97
+ const messages = [headingMessage, attrsMessage, contentMessage].reduce(
98
+ (acc, [title, value]) => {
99
+ const message = `${title}: ${serialize(value)}.`
100
+ return acc.concat(message)
101
+ },
102
+ [] as string[]
103
+ )
104
+
105
+ return new MilkdownError(
106
+ ErrorCode.createNodeInParserFail,
107
+ messages.join('\n')
108
+ )
109
+ }
110
+
111
+ export function stackOverFlow() {
112
+ return new MilkdownError(
113
+ ErrorCode.stackOverFlow,
114
+ 'Stack over flow, cannot pop on an empty stack.'
115
+ )
116
+ }
117
+
118
+ export function parserMatchError(node: unknown) {
119
+ return new MilkdownError(
120
+ ErrorCode.parserMatchError,
121
+ `Cannot match target parser for node: ${stringify(node)}.`
122
+ )
123
+ }
124
+
125
+ export function serializerMatchError(node: unknown) {
126
+ return new MilkdownError(
127
+ ErrorCode.serializerMatchError,
128
+ `Cannot match target serializer for node: ${stringify(node)}.`
129
+ )
130
+ }
131
+
132
+ export function getAtomFromSchemaFail(type: 'mark' | 'node', name: string) {
133
+ return new MilkdownError(
134
+ ErrorCode.getAtomFromSchemaFail,
135
+ `Cannot get ${type}: ${name} from schema.`
136
+ )
137
+ }
138
+
139
+ export function expectDomTypeError(node: unknown) {
140
+ return new MilkdownError(
141
+ ErrorCode.expectDomTypeError,
142
+ `Expect to be a dom, but get: ${stringify(node)}.`
143
+ )
144
+ }
145
+
146
+ export function callCommandBeforeEditorView() {
147
+ return new MilkdownError(
148
+ ErrorCode.callCommandBeforeEditorView,
149
+ "You're trying to call a command before editor view initialized, make sure to get commandManager from ctx after editor view has been initialized"
150
+ )
151
+ }
152
+
153
+ export function missingRootElement() {
154
+ return new MilkdownError(
155
+ ErrorCode.missingRootElement,
156
+ 'Missing root element, milkdown cannot find root element of the editor.'
157
+ )
158
+ }
159
+
160
+ export function missingNodeInSchema(name: string) {
161
+ return new MilkdownError(
162
+ ErrorCode.missingNodeInSchema,
163
+ `Missing node in schema, milkdown cannot find "${name}" in schema.`
164
+ )
165
+ }
166
+
167
+ export function missingMarkInSchema(name: string) {
168
+ return new MilkdownError(
169
+ ErrorCode.missingMarkInSchema,
170
+ `Missing mark in schema, milkdown cannot find "${name}" in schema.`
171
+ )
172
+ }
173
+
174
+ export function ctxNotBind() {
175
+ return new MilkdownError(
176
+ ErrorCode.ctxNotBind,
177
+ 'Context not bind, please make sure the plugin has been initialized.'
178
+ )
179
+ }
180
+
181
+ export function missingYjsDoc() {
182
+ return new MilkdownError(
183
+ ErrorCode.missingYjsDoc,
184
+ 'Missing yjs doc, please make sure you have bind one.'
185
+ )
186
+ }