@mdream/js 2.0.0-beta.1 → 2.0.1

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 (40) hide show
  1. package/README.md +402 -126
  2. package/dist/_chunks/clean.mjs +526 -0
  3. package/dist/_chunks/const.mjs +3 -7
  4. package/dist/_chunks/extraction.d.mts +14 -0
  5. package/dist/_chunks/html.mjs +260 -0
  6. package/dist/_chunks/markdown-processor.mjs +1077 -828
  7. package/dist/_chunks/minimal.mjs +29 -0
  8. package/dist/_chunks/output-runner.mjs +54 -0
  9. package/dist/_chunks/parse.mjs +134 -1501
  10. package/dist/_chunks/plugin.mjs +14 -0
  11. package/dist/_chunks/src.mjs +291 -0
  12. package/dist/_chunks/tag-overrides.mjs +138 -0
  13. package/dist/_chunks/{plugins.mjs → tailwind.mjs} +145 -292
  14. package/dist/_chunks/text.mjs +583 -0
  15. package/dist/_chunks/types.d.mts +136 -126
  16. package/dist/_chunks/url.mjs +82 -0
  17. package/dist/_chunks/utils.mjs +659 -0
  18. package/dist/clean.d.mts +10 -0
  19. package/dist/clean.mjs +2 -0
  20. package/dist/cli.mjs +47 -8
  21. package/dist/html.d.mts +6 -0
  22. package/dist/html.mjs +2 -0
  23. package/dist/index.d.mts +5 -4
  24. package/dist/index.mjs +4 -58
  25. package/dist/parse.d.mts +15 -10
  26. package/dist/plugins.d.mts +13 -28
  27. package/dist/plugins.mjs +52 -2
  28. package/dist/preset/minimal.d.mts +14 -2
  29. package/dist/preset/minimal.mjs +1 -32
  30. package/dist/splitter.d.mts +12 -4
  31. package/dist/splitter.mjs +386 -113
  32. package/dist/text.d.mts +6 -0
  33. package/dist/text.mjs +2 -0
  34. package/package.json +34 -8
  35. package/dist/_chunks/index.d.mts +0 -9
  36. package/dist/_chunks/minimal.d.mts +0 -6
  37. package/dist/_chunks/resolve-plugins.mjs +0 -44
  38. package/dist/_chunks/stream.mjs +0 -37
  39. package/dist/core.d.mts +0 -15
  40. package/dist/core.mjs +0 -28
@@ -1,44 +0,0 @@
1
- import { extractionCollectorPlugin, filterPlugin, frontmatterPlugin, isolateMainPlugin, tailwindPlugin } from "./plugins.mjs";
2
- function resolveFrontmatterOpt(opt) {
3
- if (typeof opt === "function") return {
4
- config: {},
5
- callback: opt
6
- };
7
- if (typeof opt === "object") return {
8
- config: opt,
9
- callback: opt.onExtract
10
- };
11
- return { config: {} };
12
- }
13
- function resolvePlugins(options, hooks) {
14
- const plugins = [];
15
- let callExtractionHandlers;
16
- let getFrontmatter;
17
- let frontmatterCallback;
18
- const config = options.plugins;
19
- if (config) {
20
- if (config.frontmatter) {
21
- const fm = resolveFrontmatterOpt(config.frontmatter);
22
- const fmPlugin = frontmatterPlugin(fm.config);
23
- plugins.push(fmPlugin);
24
- getFrontmatter = fmPlugin.getFrontmatter;
25
- frontmatterCallback = fm.callback;
26
- }
27
- if (config.isolateMain) plugins.push(isolateMainPlugin());
28
- if (config.tailwind) plugins.push(tailwindPlugin());
29
- if (config.filter) plugins.push(filterPlugin(config.filter));
30
- if (config.extraction) {
31
- const collector = extractionCollectorPlugin(config.extraction);
32
- plugins.push(collector.plugin);
33
- callExtractionHandlers = collector.callHandlers;
34
- }
35
- }
36
- if (hooks) plugins.push(...hooks);
37
- return {
38
- plugins,
39
- callExtractionHandlers,
40
- getFrontmatter,
41
- frontmatterCallback
42
- };
43
- }
44
- export { resolvePlugins };
@@ -1,37 +0,0 @@
1
- import { finalizeParse, parseHtmlStream } from "./parse.mjs";
2
- import { createMarkdownProcessor, processPluginsForEvent } from "./markdown-processor.mjs";
3
- async function* streamHtmlToMarkdown(htmlStream, options = {}, resolvedPlugins = [], tagOverrideHandlers) {
4
- if (!htmlStream) throw new Error("Invalid HTML stream provided");
5
- const decoder = new TextDecoder();
6
- const reader = htmlStream.getReader();
7
- const processor = createMarkdownProcessor(options, resolvedPlugins, tagOverrideHandlers);
8
- const parseState = {
9
- depthMap: processor.state.depthMap,
10
- depth: 0,
11
- resolvedPlugins,
12
- tagOverrideHandlers,
13
- plainText: options.format === "text" || options.format === "html"
14
- };
15
- const handleEvent = resolvedPlugins.length ? (event) => processPluginsForEvent(event, resolvedPlugins, processor.state, processor.processEvent) : processor.processEvent;
16
- let remainingHtml = "";
17
- try {
18
- while (true) {
19
- const { done, value } = await reader.read();
20
- if (done) break;
21
- const decoded = typeof value === "string" ? decoder.decode() + value : decoder.decode(value, { stream: true });
22
- const htmlContent = `${remainingHtml}${decoded}`;
23
- remainingHtml = parseHtmlStream(htmlContent, parseState, handleEvent);
24
- const chunk = processor.getMarkdownChunk();
25
- if (chunk) yield chunk;
26
- }
27
- const decoderTail = decoder.decode();
28
- const finalHtml = remainingHtml + decoderTail;
29
- const leftover = finalHtml ? parseHtmlStream(finalHtml, parseState, handleEvent) : "";
30
- finalizeParse(leftover, parseState, handleEvent);
31
- const finalChunk = processor.getMarkdownChunk();
32
- if (finalChunk) yield finalChunk;
33
- } finally {
34
- reader.releaseLock();
35
- }
36
- }
37
- export { streamHtmlToMarkdown };
package/dist/core.d.mts DELETED
@@ -1,15 +0,0 @@
1
- import { BuiltinPlugins, MdreamOptions, TransformPlugin } from "./_chunks/types.mjs";
2
- /** Imperative plugin used by the tree-shakable converter. */
3
- export type Plugin = TransformPlugin;
4
- /**
5
- * Options for the tree-shakable conversion entry. Import plugin factories
6
- * explicitly and pass their results as an array, matching the pre-v1 JS API.
7
- * Declarative built-in plugin config and cleanup live on the package root.
8
- */
9
- export type CoreOptions = Omit<MdreamOptions, 'plugins' | 'clean'> & {
10
- plugins?: Plugin[] | Pick<BuiltinPlugins, 'tagOverrides'>;
11
- };
12
- /** Convert HTML without bundling the optional declarative built-in plugins. */
13
- export declare function htmlToMarkdown(html: string, options?: CoreOptions): string;
14
- /** Stream HTML conversion without bundling declarative built-in plugins. */
15
- export declare function streamHtmlToMarkdown(htmlStream: ReadableStream<Uint8Array | string> | null, options?: CoreOptions): AsyncIterable<string>;
package/dist/core.mjs DELETED
@@ -1,28 +0,0 @@
1
- import { buildTagOverrideHandlers } from "./_chunks/parse.mjs";
2
- import { createMarkdownProcessor } from "./_chunks/markdown-processor.mjs";
3
- import { streamHtmlToMarkdown as streamHtmlToMarkdown$1 } from "./_chunks/stream.mjs";
4
- function resolveHooks(options) {
5
- const plugins = Array.isArray(options.plugins) ? options.plugins : void 0;
6
- const hooks = options.hooks?.length ? options.hooks : void 0;
7
- if (!plugins?.length) return hooks || [];
8
- if (!hooks) return plugins;
9
- return [...plugins, ...hooks];
10
- }
11
- function resolveTagOverrides(options) {
12
- return !Array.isArray(options.plugins) ? options.plugins?.tagOverrides : void 0;
13
- }
14
- function htmlToMarkdown(html, options = {}) {
15
- const plugins = resolveHooks(options);
16
- const tagOverrides = resolveTagOverrides(options);
17
- const tagOverrideHandlers = tagOverrides ? buildTagOverrideHandlers(tagOverrides) : void 0;
18
- const processor = createMarkdownProcessor(options, plugins, tagOverrideHandlers);
19
- processor.processHtml(html);
20
- return processor.getMarkdown();
21
- }
22
- function streamHtmlToMarkdown(htmlStream, options = {}) {
23
- const plugins = resolveHooks(options);
24
- const tagOverrides = resolveTagOverrides(options);
25
- const tagOverrideHandlers = tagOverrides ? buildTagOverrideHandlers(tagOverrides) : void 0;
26
- return streamHtmlToMarkdown$1(htmlStream, options, plugins, tagOverrideHandlers);
27
- }
28
- export { htmlToMarkdown, streamHtmlToMarkdown };