@andesine/converters 0.1.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/CHANGELOG.md +9 -0
- package/LICENSE +21 -0
- package/README.md +366 -0
- package/dist/anchors.d.ts +18 -0
- package/dist/anchors.js +36 -0
- package/dist/anchors.js.map +1 -0
- package/dist/chunks/adapter-CeNgvE5W.js +177 -0
- package/dist/chunks/adapter-CeNgvE5W.js.map +1 -0
- package/dist/chunks/core-DC64ddB7.js +372 -0
- package/dist/chunks/core-DC64ddB7.js.map +1 -0
- package/dist/chunks/decode-B-xzTJFV.js +102 -0
- package/dist/chunks/decode-B-xzTJFV.js.map +1 -0
- package/dist/chunks/decode-BLP2Olia.d.ts +6 -0
- package/dist/chunks/stringify-DdPaV2y4.js +71 -0
- package/dist/chunks/stringify-DdPaV2y4.js.map +1 -0
- package/dist/chunks/types-SDFWRP3z.d.ts +185 -0
- package/dist/html.d.ts +49 -0
- package/dist/html.js +252 -0
- package/dist/html.js.map +1 -0
- package/dist/index.d.ts +115 -0
- package/dist/index.js +40 -0
- package/dist/index.js.map +1 -0
- package/dist/markdoc.d.ts +49 -0
- package/dist/markdoc.js +310 -0
- package/dist/markdoc.js.map +1 -0
- package/dist/markdown.d.ts +48 -0
- package/dist/markdown.js +49 -0
- package/dist/markdown.js.map +1 -0
- package/dist/mdx.d.ts +50 -0
- package/dist/mdx.js +110 -0
- package/dist/mdx.js.map +1 -0
- package/dist/text.d.ts +36 -0
- package/dist/text.js +69 -0
- package/dist/text.js.map +1 -0
- package/package.json +98 -0
package/dist/mdx.js
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { a as element, i as ConversionError, o as isJSONValue, t as createConverter } from "./chunks/core-DC64ddB7.js";
|
|
2
|
+
import { t as createMarkdownAdapter } from "./chunks/adapter-CeNgvE5W.js";
|
|
3
|
+
import remarkMDX from "remark-mdx";
|
|
4
|
+
import { parseExpressionAt } from "acorn";
|
|
5
|
+
//#region src/static-expression.ts
|
|
6
|
+
const staticExpression = (source) => {
|
|
7
|
+
const expression = parseExpressionAt(source, 0, { ecmaVersion: "latest" });
|
|
8
|
+
const read = (node) => {
|
|
9
|
+
if (node.type === "Literal" && !node.regex && !node.bigint) return node.value;
|
|
10
|
+
if (node.type === "ArrayExpression") return node.elements.map((item) => {
|
|
11
|
+
if (!item) throw new ConversionError("Array holes are not static JSON values");
|
|
12
|
+
return read(item);
|
|
13
|
+
});
|
|
14
|
+
if (node.type === "ObjectExpression") return Object.fromEntries(node.properties.map((property) => {
|
|
15
|
+
if (property.type !== "Property" || property.computed || property.method || property.shorthand || property.kind !== "init") throw new ConversionError("Only static object properties are supported");
|
|
16
|
+
const key = property.key;
|
|
17
|
+
return [key.type === "Identifier" ? key.name : String(key.value), read(property.value)];
|
|
18
|
+
}));
|
|
19
|
+
if (node.type === "UnaryExpression" && ["-", "+"].includes(node.operator || "")) {
|
|
20
|
+
const value = read(node.argument);
|
|
21
|
+
if (typeof value === "number") return node.operator === "-" ? -value : value;
|
|
22
|
+
}
|
|
23
|
+
throw new ConversionError(`Unsupported dynamic expression: ${node.type}`);
|
|
24
|
+
};
|
|
25
|
+
if (source.slice(expression.end).trim()) throw new ConversionError("Unexpected content after static expression");
|
|
26
|
+
return read(expression);
|
|
27
|
+
};
|
|
28
|
+
//#endregion
|
|
29
|
+
//#region src/mdx.ts
|
|
30
|
+
const markdown = createMarkdownAdapter(remarkMDX);
|
|
31
|
+
/** MDX adapter for createConverter(), including its parser and node/mark mappings. */
|
|
32
|
+
const mdxAdapter = {
|
|
33
|
+
...markdown,
|
|
34
|
+
elementName: (node) => node.type === "mdxJsxFlowElement" || node.type === "mdxJsxTextElement" ? node.name || void 0 : void 0,
|
|
35
|
+
async encode(node, context) {
|
|
36
|
+
if (node.type !== "element") return markdown.encode(node, context);
|
|
37
|
+
const name = String(node.attrs?.name || "Element");
|
|
38
|
+
const props = node.attrs?.props || {};
|
|
39
|
+
const children = await context.children(node);
|
|
40
|
+
if (!isJSONValue(props)) throw new ConversionError("MDX element properties must be static JSON values", context.path);
|
|
41
|
+
element(name, props, []);
|
|
42
|
+
return {
|
|
43
|
+
type: "mdxJsxFlowElement",
|
|
44
|
+
name,
|
|
45
|
+
attributes: Object.entries(props).map(([name, value]) => ({
|
|
46
|
+
type: "mdxJsxAttribute",
|
|
47
|
+
name,
|
|
48
|
+
value: typeof value === "string" ? value : {
|
|
49
|
+
type: "mdxJsxAttributeValueExpression",
|
|
50
|
+
value: JSON.stringify(value)
|
|
51
|
+
}
|
|
52
|
+
})),
|
|
53
|
+
children
|
|
54
|
+
};
|
|
55
|
+
},
|
|
56
|
+
async decode(node, context) {
|
|
57
|
+
if (node.type !== "mdxJsxFlowElement") return markdown.decode(node, context);
|
|
58
|
+
if (!node.name) throw new ConversionError("MDX fragments require a custom handler", context.path);
|
|
59
|
+
const props = Object.create(null);
|
|
60
|
+
for (const attribute of node.attributes) {
|
|
61
|
+
if (attribute.type !== "mdxJsxAttribute") throw new ConversionError("MDX spread attributes require a custom handler", context.path);
|
|
62
|
+
props[attribute.name] = attribute.value === null || attribute.value === void 0 ? true : typeof attribute.value === "string" ? attribute.value : staticExpression(attribute.value.value);
|
|
63
|
+
}
|
|
64
|
+
return element(node.name, { ...props }, await context.children(node.children));
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
/**
|
|
68
|
+
* Create a reusable async MDX converter with application-specific defaults.
|
|
69
|
+
* @param options - Default mappings and handlers; per-call maps merge by key and callbacks may be async.
|
|
70
|
+
* @returns String and AST import/export methods. Defaults use an H1 title, unwrap fragments, and omit properties.
|
|
71
|
+
* Custom elements use static JSON properties; executable expressions require custom handlers.
|
|
72
|
+
*/
|
|
73
|
+
const createMDXConverter = (options) => createConverter(mdxAdapter, options);
|
|
74
|
+
const converter = createMDXConverter();
|
|
75
|
+
/**
|
|
76
|
+
* Export ProseMirror JSON as MDX.
|
|
77
|
+
* @param document - Entry or fragment document. Fragment wrappers are removed by default.
|
|
78
|
+
* @param options - Title/property/fragment mappings and async handlers; imageURL resolves images.
|
|
79
|
+
* @returns Serialized MDX. Omitted metadata and unwrapped fragment boundaries are not recoverable.
|
|
80
|
+
* @throws ConversionError for unsupported content, unresolved images, or invalid mappings; handler errors pass through.
|
|
81
|
+
*/
|
|
82
|
+
const toMDX = converter.encode;
|
|
83
|
+
/**
|
|
84
|
+
* Import MDX as ProseMirror JSON without saving content or uploading assets.
|
|
85
|
+
* @param source - Source text to parse. Custom elements use static JSON properties; executable expressions require custom handlers.
|
|
86
|
+
* @param options - Import mappings and async handlers. imageAssetID must resolve image URLs.
|
|
87
|
+
* @returns An entry document; use document: "fragment" to omit the title and retain the first H1 as body content.
|
|
88
|
+
* @throws ConversionError for unsupported content/mappings; parser and handler errors pass through.
|
|
89
|
+
*/
|
|
90
|
+
const fromMDX = converter.decode;
|
|
91
|
+
/**
|
|
92
|
+
* Export ProseMirror JSON as a MDX AST with separate frontmatter.
|
|
93
|
+
* @param document - Source entry or fragment document.
|
|
94
|
+
* @param options - Export mappings and async handlers, including imageURL for images.
|
|
95
|
+
* @returns The format tree and metadata without string serialization.
|
|
96
|
+
* @throws The same conversion errors as toMDX(), including unsupported frontmatter mappings.
|
|
97
|
+
*/
|
|
98
|
+
const toMDXAST = converter.toAST;
|
|
99
|
+
/**
|
|
100
|
+
* Import a parsed MDX AST and its frontmatter as ProseMirror JSON.
|
|
101
|
+
* @param input - Tree and frontmatter object; use an empty object when metadata is absent.
|
|
102
|
+
* @param options - Import mappings and async handlers, including imageAssetID for images.
|
|
103
|
+
* @returns An entry document by default, or a fragment document when requested.
|
|
104
|
+
* @throws The same conversion errors as fromMDX(), excluding string-parser errors.
|
|
105
|
+
*/
|
|
106
|
+
const fromMDXAST = converter.fromAST;
|
|
107
|
+
//#endregion
|
|
108
|
+
export { createMDXConverter, fromMDX, fromMDXAST, mdxAdapter, toMDX, toMDXAST };
|
|
109
|
+
|
|
110
|
+
//# sourceMappingURL=mdx.js.map
|
package/dist/mdx.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mdx.js","names":[],"sources":["../src/static-expression.ts","../src/mdx.ts"],"sourcesContent":["import { parseExpressionAt } from \"acorn\";\nimport { ConversionError } from \"./utils\";\n\ninterface Expression {\n type: string;\n end: number;\n value?: unknown;\n name?: string;\n regex?: unknown;\n bigint?: unknown;\n elements?: Array<Expression | null>;\n properties?: Expression[];\n key?: Expression;\n computed?: boolean;\n method?: boolean;\n shorthand?: boolean;\n kind?: string;\n operator?: string;\n argument?: Expression;\n}\n\nconst staticExpression = (source: string): unknown => {\n const expression = parseExpressionAt(source, 0, { ecmaVersion: \"latest\" }) as Expression;\n const read = (node: Expression): unknown => {\n if (node.type === \"Literal\" && !node.regex && !node.bigint) return node.value;\n if (node.type === \"ArrayExpression\")\n return node.elements!.map((item) => {\n if (!item) throw new ConversionError(\"Array holes are not static JSON values\");\n\n return read(item);\n });\n if (node.type === \"ObjectExpression\")\n return Object.fromEntries(\n node.properties!.map((property) => {\n if (\n property.type !== \"Property\" ||\n property.computed ||\n property.method ||\n property.shorthand ||\n property.kind !== \"init\"\n ) {\n throw new ConversionError(\"Only static object properties are supported\");\n }\n const key = property.key!;\n\n return [\n key.type === \"Identifier\" ? key.name : String(key.value),\n read(property.value as Expression)\n ];\n })\n );\n if (node.type === \"UnaryExpression\" && [\"-\", \"+\"].includes(node.operator || \"\")) {\n const value = read(node.argument!);\n\n if (typeof value === \"number\") return node.operator === \"-\" ? -value : value;\n }\n throw new ConversionError(`Unsupported dynamic expression: ${node.type}`);\n };\n\n if (source.slice(expression.end).trim())\n throw new ConversionError(\"Unexpected content after static expression\");\n\n return read(expression);\n};\n\nexport { staticExpression };\n","import remarkMDX from \"remark-mdx\";\nimport type { MdxJsxFlowElement } from \"mdast-util-mdx-jsx\";\nimport { createConverter } from \"./core\";\nimport { createMarkdownAdapter } from \"./markdown/adapter\";\nimport { element, isJSONValue, ConversionError } from \"./utils\";\nimport { staticExpression } from \"./static-expression\";\nimport type { MarkdownNode } from \"./markdown/decode\";\nimport type { ConverterOptions, FormatAdapter } from \"./types\";\n\ntype MDXNode = MarkdownNode | MdxJsxFlowElement;\ntype MDXOptions = ConverterOptions<MDXNode>;\n\nconst markdown = createMarkdownAdapter(remarkMDX);\n/** MDX adapter for createConverter(), including its parser and node/mark mappings. */\nconst mdxAdapter: FormatAdapter<MDXNode> = {\n ...markdown,\n elementName: (node) =>\n node.type === \"mdxJsxFlowElement\" || node.type === \"mdxJsxTextElement\"\n ? node.name || undefined\n : undefined,\n async encode(node, context) {\n if (node.type !== \"element\") return markdown.encode(node, context);\n\n const name = String(node.attrs?.name || \"Element\");\n const props = (node.attrs?.props || {}) as Record<string, unknown>;\n const children = await context.children(node);\n\n if (!isJSONValue(props))\n throw new ConversionError(\"MDX element properties must be static JSON values\", context.path);\n\n // Validate names and build a source form using the same static element rules.\n element(name, props, []);\n return {\n type: \"mdxJsxFlowElement\",\n name,\n attributes: Object.entries(props).map(([name, value]) => ({\n type: \"mdxJsxAttribute\",\n name,\n value:\n typeof value === \"string\"\n ? value\n : { type: \"mdxJsxAttributeValueExpression\", value: JSON.stringify(value) }\n })),\n children\n } as Extract<MarkdownNode, { type: \"mdxJsxFlowElement\" }>;\n },\n async decode(node, context) {\n if (node.type !== \"mdxJsxFlowElement\") return markdown.decode(node, context);\n if (!node.name)\n throw new ConversionError(\"MDX fragments require a custom handler\", context.path);\n\n const props: Record<string, unknown> = Object.create(null);\n\n for (const attribute of node.attributes) {\n if (attribute.type !== \"mdxJsxAttribute\")\n throw new ConversionError(\"MDX spread attributes require a custom handler\", context.path);\n\n props[attribute.name] =\n attribute.value === null || attribute.value === undefined\n ? true\n : typeof attribute.value === \"string\"\n ? attribute.value\n : staticExpression(attribute.value.value);\n }\n return element(node.name, { ...props }, await context.children(node.children));\n }\n};\n/**\n * Create a reusable async MDX converter with application-specific defaults.\n * @param options - Default mappings and handlers; per-call maps merge by key and callbacks may be async.\n * @returns String and AST import/export methods. Defaults use an H1 title, unwrap fragments, and omit properties.\n * Custom elements use static JSON properties; executable expressions require custom handlers.\n */\nconst createMDXConverter = (options?: MDXOptions) => createConverter(mdxAdapter, options);\nconst converter = createMDXConverter();\n/**\n * Export ProseMirror JSON as MDX.\n * @param document - Entry or fragment document. Fragment wrappers are removed by default.\n * @param options - Title/property/fragment mappings and async handlers; imageURL resolves images.\n * @returns Serialized MDX. Omitted metadata and unwrapped fragment boundaries are not recoverable.\n * @throws ConversionError for unsupported content, unresolved images, or invalid mappings; handler errors pass through.\n */\nconst toMDX = converter.encode;\n/**\n * Import MDX as ProseMirror JSON without saving content or uploading assets.\n * @param source - Source text to parse. Custom elements use static JSON properties; executable expressions require custom handlers.\n * @param options - Import mappings and async handlers. imageAssetID must resolve image URLs.\n * @returns An entry document; use document: \"fragment\" to omit the title and retain the first H1 as body content.\n * @throws ConversionError for unsupported content/mappings; parser and handler errors pass through.\n */\nconst fromMDX = converter.decode;\n/**\n * Export ProseMirror JSON as a MDX AST with separate frontmatter.\n * @param document - Source entry or fragment document.\n * @param options - Export mappings and async handlers, including imageURL for images.\n * @returns The format tree and metadata without string serialization.\n * @throws The same conversion errors as toMDX(), including unsupported frontmatter mappings.\n */\nconst toMDXAST = converter.toAST;\n/**\n * Import a parsed MDX AST and its frontmatter as ProseMirror JSON.\n * @param input - Tree and frontmatter object; use an empty object when metadata is absent.\n * @param options - Import mappings and async handlers, including imageAssetID for images.\n * @returns An entry document by default, or a fragment document when requested.\n * @throws The same conversion errors as fromMDX(), excluding string-parser errors.\n */\nconst fromMDXAST = converter.fromAST;\n\nexport { createMDXConverter, mdxAdapter, toMDX, fromMDX, toMDXAST, fromMDXAST };\nexport type { MDXOptions, MDXNode };\n"],"mappings":";;;;;AAqBA,MAAM,oBAAoB,WAA4B;CACpD,MAAM,aAAa,kBAAkB,QAAQ,GAAG,EAAE,aAAa,SAAS,CAAC;CACzE,MAAM,QAAQ,SAA8B;EAC1C,IAAI,KAAK,SAAS,aA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ExportConverter, f as ExportConverterOptions, i as ConvertedAST, p as ExportFormatAdapter, r as ContentNode } from "./chunks/types-SDFWRP3z.js";
|
|
2
|
+
//#region src/text.d.ts
|
|
3
|
+
interface TextNode {
|
|
4
|
+
type: "text" | "root";
|
|
5
|
+
value?: string;
|
|
6
|
+
children?: TextNode[];
|
|
7
|
+
}
|
|
8
|
+
type TextOptions = ExportConverterOptions<TextNode>;
|
|
9
|
+
/** Export-only plain-text adapter. Formatting is discarded; import methods are not provided. */
|
|
10
|
+
declare const textAdapter: ExportFormatAdapter<TextNode>;
|
|
11
|
+
/**
|
|
12
|
+
* Create an export-only async plain-text converter with reusable defaults.
|
|
13
|
+
* @param options - Default mappings and handlers, merged with per-call overrides.
|
|
14
|
+
* @returns encode() and toAST(). Import is not supported. Marks and fragment boundaries are lost;
|
|
15
|
+
* images use alt text or captions and do not require an imageURL callback. Frontmatter is unsupported.
|
|
16
|
+
*/
|
|
17
|
+
declare const createTextConverter: (options?: TextOptions) => ExportConverter<TextNode>;
|
|
18
|
+
/**
|
|
19
|
+
* Export a ProseMirror document as plain text.
|
|
20
|
+
* @param document - Entry or fragment document.
|
|
21
|
+
* @param options - Export mappings and async handlers. Properties are omitted by default.
|
|
22
|
+
* @returns Text with block/list separators and image alt text or captions; formatting is discarded.
|
|
23
|
+
* @throws ConversionError for unsupported nodes or frontmatter mappings; handler errors pass through.
|
|
24
|
+
*/
|
|
25
|
+
declare const toText: (document: ContentNode, options?: ExportConverterOptions<TextNode> | undefined) => Promise<string>;
|
|
26
|
+
/**
|
|
27
|
+
* Export a ProseMirror document as a plain-text tree.
|
|
28
|
+
* @param document - Entry or fragment document.
|
|
29
|
+
* @param options - Export mappings and async handlers, with the same limits as toText().
|
|
30
|
+
* @returns A text/root AST and an empty frontmatter object without string serialization.
|
|
31
|
+
* @throws The same conversion errors as toText(); frontmatter mappings remain unsupported.
|
|
32
|
+
*/
|
|
33
|
+
declare const toTextAST: (document: ContentNode, options?: ExportConverterOptions<TextNode> | undefined) => Promise<ConvertedAST<TextNode>>;
|
|
34
|
+
//#endregion
|
|
35
|
+
export { type TextNode, type TextOptions, createTextConverter, textAdapter, toText, toTextAST };
|
|
36
|
+
//# sourceMappingURL=text.d.ts.map
|
package/dist/text.js
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { c as textContent, i as ConversionError, n as createExportConverter } from "./chunks/core-DC64ddB7.js";
|
|
2
|
+
//#region src/text.ts
|
|
3
|
+
const getText = (node) => node.value ?? (node.children || []).map(getText).join("");
|
|
4
|
+
/** Export-only plain-text adapter. Formatting is discarded; import methods are not provided. */
|
|
5
|
+
const textAdapter = {
|
|
6
|
+
name: "Plain text",
|
|
7
|
+
root: (children) => ({
|
|
8
|
+
type: "root",
|
|
9
|
+
children
|
|
10
|
+
}),
|
|
11
|
+
stringify: (tree) => getText(tree).trimEnd(),
|
|
12
|
+
mark: (_mark, children) => children,
|
|
13
|
+
async encode(node, context) {
|
|
14
|
+
const children = await context.children(node);
|
|
15
|
+
const value = children.map(getText).join("");
|
|
16
|
+
const text = (value) => ({
|
|
17
|
+
type: "text",
|
|
18
|
+
value
|
|
19
|
+
});
|
|
20
|
+
switch (node.type) {
|
|
21
|
+
case "text": return text(node.text || "");
|
|
22
|
+
case "hardBreak": return text("\n");
|
|
23
|
+
case "horizontalRule": return text("\n\n");
|
|
24
|
+
case "paragraph":
|
|
25
|
+
case "heading":
|
|
26
|
+
case "blockquote": return text(`${value.trimEnd()}\n\n`);
|
|
27
|
+
case "codeBlock": return text(`${textContent(node)}\n\n`);
|
|
28
|
+
case "image": return text(`${String(node.attrs?.alt || node.attrs?.caption || "")}\n\n`);
|
|
29
|
+
case "listItem": return text(value.trimEnd());
|
|
30
|
+
case "taskItem": return text(`[${node.attrs?.checked ? "x" : " "}] ${value.trimEnd()}`);
|
|
31
|
+
case "bulletList":
|
|
32
|
+
case "orderedList":
|
|
33
|
+
case "taskList": return text(`${children.map((child, index) => `${node.type === "orderedList" ? `${Number(node.attrs?.start || 1) + index}.` : "-"} ${getText(child).replace(/\n/g, "\n ")}`).join("\n")}\n\n`);
|
|
34
|
+
case "table": return text(`${value.trimEnd()}\n\n`);
|
|
35
|
+
case "tableRow": return text(`${children.map((child) => getText(child).trimEnd()).join(" ")}\n`);
|
|
36
|
+
case "tableCell":
|
|
37
|
+
case "tableHeader": return text(value.trimEnd());
|
|
38
|
+
default: throw new ConversionError(`Unsupported plain-text node: ${node.type}`, context.path);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* Create an export-only async plain-text converter with reusable defaults.
|
|
44
|
+
* @param options - Default mappings and handlers, merged with per-call overrides.
|
|
45
|
+
* @returns encode() and toAST(). Import is not supported. Marks and fragment boundaries are lost;
|
|
46
|
+
* images use alt text or captions and do not require an imageURL callback. Frontmatter is unsupported.
|
|
47
|
+
*/
|
|
48
|
+
const createTextConverter = (options) => createExportConverter(textAdapter, options);
|
|
49
|
+
const converter = createTextConverter();
|
|
50
|
+
/**
|
|
51
|
+
* Export a ProseMirror document as plain text.
|
|
52
|
+
* @param document - Entry or fragment document.
|
|
53
|
+
* @param options - Export mappings and async handlers. Properties are omitted by default.
|
|
54
|
+
* @returns Text with block/list separators and image alt text or captions; formatting is discarded.
|
|
55
|
+
* @throws ConversionError for unsupported nodes or frontmatter mappings; handler errors pass through.
|
|
56
|
+
*/
|
|
57
|
+
const toText = converter.encode;
|
|
58
|
+
/**
|
|
59
|
+
* Export a ProseMirror document as a plain-text tree.
|
|
60
|
+
* @param document - Entry or fragment document.
|
|
61
|
+
* @param options - Export mappings and async handlers, with the same limits as toText().
|
|
62
|
+
* @returns A text/root AST and an empty frontmatter object without string serialization.
|
|
63
|
+
* @throws The same conversion errors as toText(); frontmatter mappings remain unsupported.
|
|
64
|
+
*/
|
|
65
|
+
const toTextAST = converter.toAST;
|
|
66
|
+
//#endregion
|
|
67
|
+
export { createTextConverter, textAdapter, toText, toTextAST };
|
|
68
|
+
|
|
69
|
+
//# sourceMappingURL=text.js.map
|
package/dist/text.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"text.js","names":[],"sources":["../src/text.ts"],"sourcesContent":["import { createExportConverter } from \"./core\";\nimport { ConversionError, textContent } from \"./utils\";\nimport type { ExportConverterOptions, ExportFormatAdapter } from \"./types\";\n\ninterface TextNode {\n type: \"text\" | \"root\";\n value?: string;\n children?: TextNode[];\n}\n\ntype TextOptions = ExportConverterOptions<TextNode>;\n\nconst getText = (node: TextNode): string =>\n node.value ?? (node.children || []).map(getText).join(\"\");\n/** Export-only plain-text adapter. Formatting is discarded; import methods are not provided. */\nconst textAdapter: ExportFormatAdapter<TextNode> = {\n name: \"Plain text\",\n root: (children) => ({ type: \"root\", children }),\n stringify: (tree) => getText(tree).trimEnd(),\n mark: (_mark, children) => children,\n async encode(node, context) {\n const children = await context.children(node);\n const value = children.map(getText).join(\"\");\n const text = (value: string): TextNode => ({ type: \"text\", value });\n\n switch (node.type) {\n case \"text\":\n return text(node.text || \"\");\n case \"hardBreak\":\n return text(\"\\n\");\n case \"horizontalRule\":\n return text(\"\\n\\n\");\n case \"paragraph\":\n case \"heading\":\n case \"blockquote\":\n return text(`${value.trimEnd()}\\n\\n`);\n case \"codeBlock\":\n return text(`${textContent(node)}\\n\\n`);\n case \"image\":\n return text(`${String(node.attrs?.alt || node.attrs?.caption || \"\")}\\n\\n`);\n case \"listItem\":\n return text(value.trimEnd());\n case \"taskItem\":\n return text(`[${node.attrs?.checked ? \"x\" : \" \"}] ${value.trimEnd()}`);\n case \"bulletList\":\n case \"orderedList\":\n case \"taskList\":\n return text(\n `${children.map((child, index) => `${node.type === \"orderedList\" ? `${Number(node.attrs?.start || 1) + index}.` : \"-\"} ${getText(child).replace(/\\n/g, \"\\n \")}`).join(\"\\n\")}\\n\\n`\n );\n case \"table\":\n return text(`${value.trimEnd()}\\n\\n`);\n case \"tableRow\":\n return text(`${children.map((child) => getText(child).trimEnd()).join(\"\\t\")}\\n`);\n case \"tableCell\":\n case \"tableHeader\":\n return text(value.trimEnd());\n default:\n throw new ConversionError(`Unsupported plain-text node: ${node.type}`, context.path);\n }\n }\n};\n/**\n * Create an export-only async plain-text converter with reusable defaults.\n * @param options - Default mappings and handlers, merged with per-call overrides.\n * @returns encode() and toAST(). Import is not supported. Marks and fragment boundaries are lost;\n * images use alt text or captions and do not require an imageURL callback. Frontmatter is unsupported.\n */\nconst createTextConverter = (options?: TextOptions) => createExportConverter(textAdapter, options);\nconst converter = createTextConverter();\n/**\n * Export a ProseMirror document as plain text.\n * @param document - Entry or fragment document.\n * @param options - Export mappings and async handlers. Properties are omitted by default.\n * @returns Text with block/list separators and image alt text or captions; formatting is discarded.\n * @throws ConversionError for unsupported nodes or frontmatter mappings; handler errors pass through.\n */\nconst toText = converter.encode;\n/**\n * Export a ProseMirror document as a plain-text tree.\n * @param document - Entry or fragment document.\n * @param options - Export mappings and async handlers, with the same limits as toText().\n * @returns A text/root AST and an empty frontmatter object without string serialization.\n * @throws The same conversion errors as toText(); frontmatter mappings remain unsupported.\n */\nconst toTextAST = converter.toAST;\n\nexport { createTextConverter, textAdapter, toText, toTextAST };\nexport type { TextNode, TextOptions };\n"],"mappings":";;AAYA,MAAM,WAAW,SACf,KAAK,UAAU,KAAK,YAAY,CAAC,EAAA,CAAG,IAAI,OAAO,CAAC,CAAC,KAAK,EAAE;;AAE1D,MAAM,cAA6C;CACjD,MAAM;CACN,OAAO,cAAc;EAAE,MAAM;EAAQ;CAAS;CAC9C,YAAY,SAAS,QAAQ,IAAI,CAAC,CAAC,QAAQ;CAC3C,OAAO,OAAO,aAAa;CAC3B,MAAM,OAAO,MAAM,SAAS;EAC1B,MAAM,WAAW,MAAM,QAAQ,SAAS,IAAI;EAC5C,MAAM,QAAQ,SAAS,IAAI,OAAO,CAAC,CAAC,KAAK,EAAE;EAC3C,MAAM,QAAQ,WAA6B;GAAE,MAAM;GAAQ;EAAM;EAEjE,QAAQ,KAAK,MAA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AU"}
|
package/package.json
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@andesine/converters",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "TypeScript content converters for Andesine",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"files": [
|
|
9
|
+
"dist",
|
|
10
|
+
"LICENSE",
|
|
11
|
+
"README.md",
|
|
12
|
+
"CHANGELOG.md"
|
|
13
|
+
],
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"import": "./dist/index.js"
|
|
18
|
+
},
|
|
19
|
+
"./markdown": {
|
|
20
|
+
"types": "./dist/markdown.d.ts",
|
|
21
|
+
"import": "./dist/markdown.js"
|
|
22
|
+
},
|
|
23
|
+
"./mdx": {
|
|
24
|
+
"types": "./dist/mdx.d.ts",
|
|
25
|
+
"import": "./dist/mdx.js"
|
|
26
|
+
},
|
|
27
|
+
"./markdoc": {
|
|
28
|
+
"types": "./dist/markdoc.d.ts",
|
|
29
|
+
"import": "./dist/markdoc.js"
|
|
30
|
+
},
|
|
31
|
+
"./html": {
|
|
32
|
+
"types": "./dist/html.d.ts",
|
|
33
|
+
"import": "./dist/html.js"
|
|
34
|
+
},
|
|
35
|
+
"./text": {
|
|
36
|
+
"types": "./dist/text.d.ts",
|
|
37
|
+
"import": "./dist/text.js"
|
|
38
|
+
},
|
|
39
|
+
"./anchors": {
|
|
40
|
+
"types": "./dist/anchors.d.ts",
|
|
41
|
+
"import": "./dist/anchors.js"
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
"publishConfig": {
|
|
45
|
+
"access": "public",
|
|
46
|
+
"registry": "https://registry.npmjs.org/"
|
|
47
|
+
},
|
|
48
|
+
"engines": {
|
|
49
|
+
"node": ">=22"
|
|
50
|
+
},
|
|
51
|
+
"devDependencies": {
|
|
52
|
+
"typescript": "6.0.3",
|
|
53
|
+
"@types/node": "^26.1.2",
|
|
54
|
+
"rolldown": "^1.2.2",
|
|
55
|
+
"publint": "0.3.24",
|
|
56
|
+
"rolldown-plugin-dts": "^0.28.6"
|
|
57
|
+
},
|
|
58
|
+
"dependencies": {
|
|
59
|
+
"@markdoc/markdoc": "0.5.10",
|
|
60
|
+
"unified": "11.0.5",
|
|
61
|
+
"remark-parse": "11.0.0",
|
|
62
|
+
"remark-stringify": "11.0.0",
|
|
63
|
+
"remark-gfm": "4.0.1",
|
|
64
|
+
"remark-mdx": "3.1.1",
|
|
65
|
+
"hast-util-from-html": "2.0.3",
|
|
66
|
+
"hast-util-to-html": "9.0.5",
|
|
67
|
+
"yaml": "2.9.0",
|
|
68
|
+
"acorn": "8.18.0",
|
|
69
|
+
"@types/mdast": "^4.0.4",
|
|
70
|
+
"@types/react": "^19.2.18",
|
|
71
|
+
"@types/hast": "^3.0.4",
|
|
72
|
+
"mdast-util-mdx-jsx": "3.2.0"
|
|
73
|
+
},
|
|
74
|
+
"main": "./dist/index.js",
|
|
75
|
+
"types": "./dist/index.d.ts",
|
|
76
|
+
"repository": {
|
|
77
|
+
"type": "git",
|
|
78
|
+
"url": "git+https://github.com/vriteio/andesine.git",
|
|
79
|
+
"directory": "packages/public/converters"
|
|
80
|
+
},
|
|
81
|
+
"homepage": "https://github.com/vriteio/andesine/tree/andesine/packages/public/converters#readme",
|
|
82
|
+
"bugs": {
|
|
83
|
+
"url": "https://github.com/vriteio/andesine/issues"
|
|
84
|
+
},
|
|
85
|
+
"keywords": [
|
|
86
|
+
"andesine",
|
|
87
|
+
"prosemirror",
|
|
88
|
+
"markdown",
|
|
89
|
+
"mdx",
|
|
90
|
+
"markdoc",
|
|
91
|
+
"html"
|
|
92
|
+
],
|
|
93
|
+
"scripts": {
|
|
94
|
+
"build": "rolldown -c rolldown.config.ts",
|
|
95
|
+
"typecheck": "tsc --noEmit",
|
|
96
|
+
"lint:package": "publint --strict"
|
|
97
|
+
}
|
|
98
|
+
}
|