@svadmin/ai-elements 0.5.1 → 0.8.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/README.md +18 -53
- package/THIRD_PARTY_NOTICES.md +67 -6
- package/dist/ai.css +149 -2740
- package/dist/ai.theme.css +1 -2771
- package/dist/class-properties.generated.d.ts +1 -0
- package/dist/class-properties.generated.js +2 -0
- package/dist/classnames.d.ts +8 -0
- package/dist/classnames.js +26 -0
- package/dist/components/AICommandBar.svelte +6 -4
- package/dist/components/ChatDialog.svelte +102 -125
- package/dist/components/ChatDialog.svelte.d.ts +3 -9
- package/dist/components/Conversation.svelte +34 -10
- package/dist/components/ConversationContent.svelte +1 -1
- package/dist/components/ConversationScrollButton.svelte +0 -1
- package/dist/components/CopilotPanel.svelte +8 -6
- package/dist/components/Message.svelte +1 -1
- package/dist/components/MessageActions.svelte +1 -1
- package/dist/components/MessageContent.svelte +1 -1
- package/dist/components/MessageToolbar.svelte +1 -1
- package/dist/components/Response.svelte +32 -12
- package/dist/components/Response.svelte.d.ts +3 -3
- package/dist/components/Tool.svelte +13 -13
- package/dist/components/Tool.svelte.d.ts +1 -1
- package/dist/components/action/Action.svelte +3 -1
- package/dist/components/artifact/ArtifactAction.svelte +3 -1
- package/dist/components/attachments/Attachment.svelte +4 -2
- package/dist/components/attachments/Attachments.svelte +3 -1
- package/dist/components/attachments/types.js +3 -2
- package/dist/components/audio-player/AudioPlayer.svelte.d.ts +1 -1
- package/dist/components/canvas/Canvas.svelte +1 -1
- package/dist/components/checkpoint/index.d.ts +2 -2
- package/dist/components/code/index.d.ts +9 -1
- package/dist/components/code/index.js +8 -1
- package/dist/components/code-block/CodeBlockLanguageSelector.svelte.d.ts +1 -1
- package/dist/components/code-block/highlight.js +11 -10
- package/dist/components/commit/Commit.svelte +23 -2
- package/dist/components/commit/CommitActions.svelte +2 -11
- package/dist/components/commit/CommitCopyButton.svelte +1 -1
- package/dist/components/commit/CommitHeader.svelte +12 -2
- package/dist/components/commit/context.svelte.d.ts +6 -0
- package/dist/components/commit/context.svelte.js +14 -0
- package/dist/components/context/Context.svelte +6 -4
- package/dist/components/context/TokensWithCost.svelte +2 -1
- package/dist/components/edge/Animated.svelte +6 -6
- package/dist/components/edge/Temporary.svelte +6 -5
- package/dist/components/file-tree/FileTree.svelte +7 -5
- package/dist/components/jsx-preview/parser.js +10 -10
- package/dist/components/message/MessageAction.svelte +3 -1
- package/dist/components/mic-selector/MicSelector.svelte.d.ts +1 -1
- package/dist/components/model-selector/ModelSelectorDialog.svelte +3 -1
- package/dist/components/open-in-chat/OpenInChatGPT.svelte +3 -1
- package/dist/components/open-in-chat/OpenInClaude.svelte +3 -1
- package/dist/components/open-in-chat/OpenInCursor.svelte +3 -1
- package/dist/components/open-in-chat/OpenInGitHub.svelte +3 -1
- package/dist/components/open-in-chat/OpenInScira.svelte +3 -1
- package/dist/components/open-in-chat/OpenInT3.svelte +3 -1
- package/dist/components/open-in-chat/OpenInv0.svelte +3 -1
- package/dist/components/package-info/PackageInfo.svelte +3 -1
- package/dist/components/prompt-input/PromptInput.svelte +8 -18
- package/dist/components/prompt-input/PromptInputCommand.svelte +2 -1
- package/dist/components/prompt-input/PromptInputSelect.svelte.d.ts +1 -1
- package/dist/components/prompt-input/PromptInputSubmit.svelte +5 -18
- package/dist/components/prompt-input/PromptInputTextarea.svelte +6 -11
- package/dist/components/prompt-input/context.svelte.d.ts +1 -8
- package/dist/components/prompt-input/files.js +3 -2
- package/dist/components/question/Question.svelte +3 -1
- package/dist/components/schema-display/SchemaDisplay.svelte +4 -2
- package/dist/components/schema-display/SchemaDisplayParameter.svelte +14 -1
- package/dist/components/schema-display/SchemaDisplayProperty.svelte +16 -1
- package/dist/components/snippet/index.d.ts +3 -3
- package/dist/components/stack-trace/StackTrace.svelte +7 -5
- package/dist/components/suggestion/index.d.ts +1 -1
- package/dist/components/terminal/Terminal.svelte +4 -2
- package/dist/components/terminal/ansi.js +9 -6
- package/dist/components/test-results/Test.svelte +1 -1
- package/dist/components/test-results/TestResults.svelte +1 -1
- package/dist/components/test-results/context.svelte.d.ts +1 -1
- package/dist/components/tool/ToolContent.svelte +1 -1
- package/dist/components/tool/ToolHeader.svelte +5 -5
- package/dist/components/tool/ToolInput.svelte +2 -4
- package/dist/components/tool/ToolOutput.svelte +2 -4
- package/dist/components/tool/ToolStatusBadge.svelte +2 -3
- package/dist/components/voice-selector/VoiceSelector.svelte +2 -1
- package/dist/components/voice-selector/VoiceSelector.svelte.d.ts +2 -2
- package/dist/components/voice-selector/context.svelte.d.ts +2 -1
- package/dist/components/web-preview/WebPreview.svelte +4 -3
- package/dist/contracts.d.ts +2 -2
- package/dist/generated-components.d.ts +15 -1
- package/dist/generated-components.js +20 -1
- package/dist/internal/Tooltip.svelte +2 -1
- package/dist/parity-manifest.d.ts +0 -1
- package/dist/parity-manifest.js +0 -97
- package/dist/utilities.css +3172 -0
- package/dist/utils.d.ts +1 -1
- package/dist/utils.js +1 -1
- package/package.json +37 -22
- package/vendor/streamdown/AnimatedText.svelte +93 -0
- package/vendor/streamdown/AnimatedText.svelte.d.ts +6 -0
- package/vendor/streamdown/Block.svelte +96 -0
- package/vendor/streamdown/Block.svelte.d.ts +11 -0
- package/vendor/streamdown/Elements/Alert.svelte +69 -0
- package/vendor/streamdown/Elements/Alert.svelte.d.ts +10 -0
- package/vendor/streamdown/Elements/Citation.svelte +260 -0
- package/vendor/streamdown/Elements/Citation.svelte.d.ts +7 -0
- package/vendor/streamdown/Elements/Code.svelte +226 -0
- package/vendor/streamdown/Elements/Code.svelte.d.ts +9 -0
- package/vendor/streamdown/Elements/Element.svelte +574 -0
- package/vendor/streamdown/Elements/Element.svelte.d.ts +10 -0
- package/vendor/streamdown/Elements/FootnoteRef.svelte +30 -0
- package/vendor/streamdown/Elements/FootnoteRef.svelte.d.ts +7 -0
- package/vendor/streamdown/Elements/Footnotes.svelte +54 -0
- package/vendor/streamdown/Elements/Footnotes.svelte.d.ts +7 -0
- package/vendor/streamdown/Elements/Image.svelte +200 -0
- package/vendor/streamdown/Elements/Image.svelte.d.ts +10 -0
- package/vendor/streamdown/Elements/Link.svelte +220 -0
- package/vendor/streamdown/Elements/Link.svelte.d.ts +10 -0
- package/vendor/streamdown/Elements/LinkSafetyModal.svelte +121 -0
- package/vendor/streamdown/Elements/LinkSafetyModal.svelte.d.ts +4 -0
- package/vendor/streamdown/Elements/Math.svelte +100 -0
- package/vendor/streamdown/Elements/Math.svelte.d.ts +9 -0
- package/vendor/streamdown/Elements/Mermaid.svelte +455 -0
- package/vendor/streamdown/Elements/Mermaid.svelte.d.ts +9 -0
- package/vendor/streamdown/Elements/MermaidDownload.svelte +145 -0
- package/vendor/streamdown/Elements/MermaidDownload.svelte.d.ts +8 -0
- package/vendor/streamdown/Elements/Slot.svelte +23 -0
- package/vendor/streamdown/Elements/Slot.svelte.d.ts +10 -0
- package/vendor/streamdown/Elements/Table.svelte +195 -0
- package/vendor/streamdown/Elements/Table.svelte.d.ts +9 -0
- package/vendor/streamdown/Elements/TableDownload.svelte +221 -0
- package/vendor/streamdown/Elements/TableDownload.svelte.d.ts +8 -0
- package/vendor/streamdown/Elements/fallbacks/CodeFallback.svelte +41 -0
- package/vendor/streamdown/Elements/fallbacks/CodeFallback.svelte.d.ts +9 -0
- package/vendor/streamdown/Elements/fallbacks/MathFallback.svelte +35 -0
- package/vendor/streamdown/Elements/fallbacks/MathFallback.svelte.d.ts +8 -0
- package/vendor/streamdown/Elements/fallbacks/MermaidFallback.svelte +36 -0
- package/vendor/streamdown/Elements/fallbacks/MermaidFallback.svelte.d.ts +9 -0
- package/vendor/streamdown/Elements/fallbacks/index.d.ts +3 -0
- package/vendor/streamdown/Elements/fallbacks/index.js +3 -0
- package/vendor/streamdown/Elements/icons.d.ts +31 -0
- package/vendor/streamdown/Elements/icons.js +200 -0
- package/vendor/streamdown/Elements/index.d.ts +8 -0
- package/vendor/streamdown/Elements/index.js +8 -0
- package/vendor/streamdown/Elements/popover.svelte.d.ts +8 -0
- package/vendor/streamdown/Elements/popover.svelte.js +45 -0
- package/vendor/streamdown/Elements/stepperState.svelte.d.ts +36 -0
- package/vendor/streamdown/Elements/stepperState.svelte.js +92 -0
- package/vendor/streamdown/IconProvider.svelte +11 -0
- package/vendor/streamdown/IconProvider.svelte.d.ts +30 -0
- package/vendor/streamdown/LICENSE +202 -0
- package/vendor/streamdown/PluginProvider.svelte +11 -0
- package/vendor/streamdown/PluginProvider.svelte.d.ts +30 -0
- package/vendor/streamdown/README.svadmin.md +5 -0
- package/vendor/streamdown/Streamdown.svelte +447 -0
- package/vendor/streamdown/Streamdown.svelte.d.ts +25 -0
- package/vendor/streamdown/block/BlockTokens.svelte +45 -0
- package/vendor/streamdown/block/BlockTokens.svelte.d.ts +9 -0
- package/vendor/streamdown/block/html.d.ts +14 -0
- package/vendor/streamdown/block/html.js +27 -0
- package/vendor/streamdown/block/text.d.ts +10 -0
- package/vendor/streamdown/block/text.js +31 -0
- package/vendor/streamdown/context-key.d.ts +1 -0
- package/vendor/streamdown/context-key.js +1 -0
- package/vendor/streamdown/context.svelte.d.ts +24 -0
- package/vendor/streamdown/context.svelte.js +75 -0
- package/vendor/streamdown/contracts/plugins.d.ts +26 -0
- package/vendor/streamdown/contracts/plugins.js +1 -0
- package/vendor/streamdown/contracts/streamdown.d.ts +374 -0
- package/vendor/streamdown/contracts/streamdown.js +29 -0
- package/vendor/streamdown/detect-direction.d.ts +1 -0
- package/vendor/streamdown/detect-direction.js +1 -0
- package/vendor/streamdown/icon-context.d.ts +36 -0
- package/vendor/streamdown/icon-context.js +52 -0
- package/vendor/streamdown/incomplete-code.d.ts +6 -0
- package/vendor/streamdown/incomplete-code.js +23 -0
- package/vendor/streamdown/index.d.ts +22 -0
- package/vendor/streamdown/index.js +20 -0
- package/vendor/streamdown/markdown-parse-cache.d.ts +37 -0
- package/vendor/streamdown/markdown-parse-cache.js +109 -0
- package/vendor/streamdown/markdown.d.ts +21 -0
- package/vendor/streamdown/markdown.js +214 -0
- package/vendor/streamdown/marked/index.d.ts +44 -0
- package/vendor/streamdown/marked/index.js +228 -0
- package/vendor/streamdown/marked/marked-alert.d.ts +13 -0
- package/vendor/streamdown/marked/marked-alert.js +47 -0
- package/vendor/streamdown/marked/marked-align.d.ts +10 -0
- package/vendor/streamdown/marked/marked-align.js +45 -0
- package/vendor/streamdown/marked/marked-br.d.ts +6 -0
- package/vendor/streamdown/marked/marked-br.js +15 -0
- package/vendor/streamdown/marked/marked-citations.d.ts +8 -0
- package/vendor/streamdown/marked/marked-citations.js +53 -0
- package/vendor/streamdown/marked/marked-cjk.d.ts +2 -0
- package/vendor/streamdown/marked/marked-cjk.js +165 -0
- package/vendor/streamdown/marked/marked-dl.d.ts +23 -0
- package/vendor/streamdown/marked/marked-dl.js +44 -0
- package/vendor/streamdown/marked/marked-footnotes.d.ts +26 -0
- package/vendor/streamdown/marked/marked-footnotes.js +101 -0
- package/vendor/streamdown/marked/marked-hr.d.ts +6 -0
- package/vendor/streamdown/marked/marked-hr.js +18 -0
- package/vendor/streamdown/marked/marked-list.d.ts +31 -0
- package/vendor/streamdown/marked/marked-list.js +330 -0
- package/vendor/streamdown/marked/marked-math.d.ts +13 -0
- package/vendor/streamdown/marked/marked-math.js +129 -0
- package/vendor/streamdown/marked/marked-mdx.d.ts +12 -0
- package/vendor/streamdown/marked/marked-mdx.js +124 -0
- package/vendor/streamdown/marked/marked-subsup.d.ts +22 -0
- package/vendor/streamdown/marked/marked-subsup.js +40 -0
- package/vendor/streamdown/marked/marked-table.d.ts +80 -0
- package/vendor/streamdown/marked/marked-table.js +527 -0
- package/vendor/streamdown/plugin-context.d.ts +19 -0
- package/vendor/streamdown/plugin-context.js +40 -0
- package/vendor/streamdown/plugins/cjk-shared.d.ts +1 -0
- package/vendor/streamdown/plugins/cjk-shared.js +1 -0
- package/vendor/streamdown/plugins.d.ts +19 -0
- package/vendor/streamdown/plugins.js +107 -0
- package/vendor/streamdown/provenance.json +229 -0
- package/vendor/streamdown/remend.d.ts +2 -0
- package/vendor/streamdown/remend.js +1 -0
- package/vendor/streamdown/security/html.d.ts +19 -0
- package/vendor/streamdown/security/html.js +329 -0
- package/vendor/streamdown/security/preprocess-custom-tags.d.ts +1 -0
- package/vendor/streamdown/security/preprocess-custom-tags.js +20 -0
- package/vendor/streamdown/security/preprocess-literal-tag-content.d.ts +1 -0
- package/vendor/streamdown/security/preprocess-literal-tag-content.js +16 -0
- package/vendor/streamdown/security/types.d.ts +1 -0
- package/vendor/streamdown/security/types.js +1 -0
- package/vendor/streamdown/site/SiteHeader.svelte +94 -0
- package/vendor/streamdown/site/SiteHeader.svelte.d.ts +6 -0
- package/vendor/streamdown/site/home/HomeDemo.svelte +122 -0
- package/vendor/streamdown/site/home/HomeDemo.svelte.d.ts +3 -0
- package/vendor/streamdown/site/home/HomePage.svelte +293 -0
- package/vendor/streamdown/site/home/HomePage.svelte.d.ts +3 -0
- package/vendor/streamdown/site/home/content.d.ts +69 -0
- package/vendor/streamdown/site/home/content.js +144 -0
- package/vendor/streamdown/site/metadata.d.ts +4 -0
- package/vendor/streamdown/site/metadata.js +4 -0
- package/vendor/streamdown/site/navigation.d.ts +6 -0
- package/vendor/streamdown/site/navigation.js +22 -0
- package/vendor/streamdown/streamdown/config.d.ts +41 -0
- package/vendor/streamdown/streamdown/config.js +125 -0
- package/vendor/streamdown/streamdown/context.d.ts +12 -0
- package/vendor/streamdown/streamdown/context.js +2 -0
- package/vendor/streamdown/streamdown/incomplete-markdown.d.ts +5 -0
- package/vendor/streamdown/streamdown/incomplete-markdown.js +79 -0
- package/vendor/streamdown/streamdown/pipeline.d.ts +95 -0
- package/vendor/streamdown/streamdown/pipeline.js +138 -0
- package/vendor/streamdown/streamdown/runtime.d.ts +59 -0
- package/vendor/streamdown/streamdown/runtime.js +112 -0
- package/vendor/streamdown/streaming.d.ts +5 -0
- package/vendor/streamdown/streaming.js +5 -0
- package/vendor/streamdown/theme.d.ts +486 -0
- package/vendor/streamdown/theme.js +370 -0
- package/vendor/streamdown/translations.d.ts +43 -0
- package/vendor/streamdown/translations.js +54 -0
- package/vendor/streamdown/url-policy.d.ts +1 -0
- package/vendor/streamdown/url-policy.js +1 -0
- package/vendor/streamdown/utils/bind.d.ts +8 -0
- package/vendor/streamdown/utils/bind.js +26 -0
- package/vendor/streamdown/utils/bundledLanguages.d.ts +1 -0
- package/vendor/streamdown/utils/bundledLanguages.js +1 -0
- package/vendor/streamdown/utils/code-block.d.ts +8 -0
- package/vendor/streamdown/utils/code-block.js +38 -0
- package/vendor/streamdown/utils/copy.svelte.d.ts +7 -0
- package/vendor/streamdown/utils/copy.svelte.js +38 -0
- package/vendor/streamdown/utils/darkMode.svelte.d.ts +3 -0
- package/vendor/streamdown/utils/darkMode.svelte.js +49 -0
- package/vendor/streamdown/utils/detectDirection.d.ts +1 -0
- package/vendor/streamdown/utils/detectDirection.js +19 -0
- package/vendor/streamdown/utils/get.d.ts +1 -0
- package/vendor/streamdown/utils/get.js +25 -0
- package/vendor/streamdown/utils/hightlighter.svelte.d.ts +30 -0
- package/vendor/streamdown/utils/hightlighter.svelte.js +524 -0
- package/vendor/streamdown/utils/index.d.ts +3 -0
- package/vendor/streamdown/utils/index.js +3 -0
- package/vendor/streamdown/utils/mermaid.d.ts +13 -0
- package/vendor/streamdown/utils/mermaid.js +49 -0
- package/vendor/streamdown/utils/panzoom.svelte.d.ts +35 -0
- package/vendor/streamdown/utils/panzoom.svelte.js +556 -0
- package/vendor/streamdown/utils/parse-incomplete-markdown.d.ts +2 -0
- package/vendor/streamdown/utils/parse-incomplete-markdown.js +2 -0
- package/vendor/streamdown/utils/runtime-env.d.ts +1 -0
- package/vendor/streamdown/utils/runtime-env.js +13 -0
- package/vendor/streamdown/utils/save.d.ts +1 -0
- package/vendor/streamdown/utils/save.js +14 -0
- package/vendor/streamdown/utils/scroll-lock.d.ts +2 -0
- package/vendor/streamdown/utils/scroll-lock.js +35 -0
- package/vendor/streamdown/utils/table.d.ts +8 -0
- package/vendor/streamdown/utils/table.js +176 -0
- package/vendor/streamdown/utils/url.d.ts +22 -0
- package/vendor/streamdown/utils/url.js +98 -0
- package/vendor/streamdown/utils/useClickOutside.svelte.d.ts +8 -0
- package/vendor/streamdown/utils/useClickOutside.svelte.js +41 -0
- package/vendor/streamdown/utils/useKeyDown.svelte.d.ts +5 -0
- package/vendor/streamdown/utils/useKeyDown.svelte.js +23 -0
- package/UPSTREAM_PARITY.md +0 -69
- package/dist/ai-theme.css +0 -28
- package/dist/components/code/code-variants.d.ts +0 -25
- package/dist/components/code/code-variants.js +0 -20
- package/dist/components/conversation/scroll.d.ts +0 -4
- package/dist/components/conversation/scroll.js +0 -85
- package/dist/components/message/content-polish-html.d.ts +0 -5
- package/dist/components/message/content-polish-html.js +0 -75
- package/dist/components/tool/content-polish-scroll-region.d.ts +0 -3
- package/dist/components/tool/content-polish-scroll-region.js +0 -22
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export { default as Streamdown } from './Streamdown.svelte';
|
|
2
|
+
export { STREAMDOWN_CONTEXT_KEY } from './context-key.js';
|
|
3
|
+
export { StreamdownContext, useStreamdown } from './context.svelte.js';
|
|
4
|
+
export { default as IconProvider } from './IconProvider.svelte';
|
|
5
|
+
export { default as PluginProvider } from './PluginProvider.svelte';
|
|
6
|
+
export { detectTextDirection } from './detect-direction.js';
|
|
7
|
+
export { hasIncompleteCodeFence, hasTable, useIsCodeFenceIncomplete } from './incomplete-code.js';
|
|
8
|
+
export { defaultIcons, IconContext, useIcons } from './icon-context.js';
|
|
9
|
+
export { defaultUrlTransform } from './markdown.js';
|
|
10
|
+
export { lex, parseBlocks } from './marked/index.js';
|
|
11
|
+
export { PluginContext, useCjkPlugin, useCodePlugin, useCustomRenderer, useMathPlugin, useMermaidPlugin, usePlugins } from './plugin-context.js';
|
|
12
|
+
export { cjk, code, createCjkPlugin, createCodePlugin, createMathPlugin, createMermaidPlugin, findCustomRenderer, getThemeName, math, mermaid } from './plugins.js';
|
|
13
|
+
export { normalizeHtmlIndentation } from './security/html.js';
|
|
14
|
+
export { cn, theme, shadcnTheme, mergeTheme } from './theme.js';
|
|
15
|
+
export { parseIncompleteMarkdown, IncompleteMarkdownParser } from './remend.js';
|
|
16
|
+
export { bundledLanguagesInfo, createLanguageSet } from './utils/bundledLanguages.js';
|
|
17
|
+
export { isPathRelativeUrl, parseUrl, transformUrl } from './url-policy.js';
|
|
18
|
+
export { save } from './utils/index.js';
|
|
19
|
+
export { defaultTranslations, mergeTranslations } from './translations.js';
|
|
20
|
+
export { extractTableDataFromElement, tableDataToCSV, tableDataToMarkdown, tableDataToTSV } from './utils/table.js';
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { lexWithFootnotes, lexWithoutFootnotes, parseBlocksWithFootnotes, parseBlocksWithoutFootnotes, type Extension, type FootnoteState, type StreamdownToken } from './marked/index.js';
|
|
2
|
+
export type MarkdownBlockParseResult = {
|
|
3
|
+
tokens: StreamdownToken[];
|
|
4
|
+
footnotes: FootnoteState;
|
|
5
|
+
};
|
|
6
|
+
export type MarkdownBlockCacheScope = 'stable' | 'transient';
|
|
7
|
+
export type MarkdownDocumentParseResult = {
|
|
8
|
+
blocks: string[];
|
|
9
|
+
footnotes: FootnoteState;
|
|
10
|
+
};
|
|
11
|
+
export type MarkdownParseOperations = {
|
|
12
|
+
lexWithFootnotes: typeof lexWithFootnotes;
|
|
13
|
+
lexWithoutFootnotes: typeof lexWithoutFootnotes;
|
|
14
|
+
parseBlocksWithFootnotes: typeof parseBlocksWithFootnotes;
|
|
15
|
+
parseBlocksWithoutFootnotes: typeof parseBlocksWithoutFootnotes;
|
|
16
|
+
};
|
|
17
|
+
type MarkdownBlockParseRequest = {
|
|
18
|
+
markdown: string;
|
|
19
|
+
extensions?: Extension[];
|
|
20
|
+
resolveFootnotes: boolean;
|
|
21
|
+
cacheScope?: MarkdownBlockCacheScope;
|
|
22
|
+
};
|
|
23
|
+
type MarkdownDocumentParseRequest = MarkdownBlockParseRequest & {
|
|
24
|
+
splitBlocksFn?: ((markdown: string) => string[]) | null;
|
|
25
|
+
blockCacheScope?: MarkdownBlockCacheScope;
|
|
26
|
+
};
|
|
27
|
+
declare class MarkdownParseCache {
|
|
28
|
+
private readonly operations;
|
|
29
|
+
private readonly cachesByExtensions;
|
|
30
|
+
private readonly cacheWithoutExtensions;
|
|
31
|
+
constructor(operations: MarkdownParseOperations);
|
|
32
|
+
parseBlock(request: MarkdownBlockParseRequest): MarkdownBlockParseResult;
|
|
33
|
+
parseDocument(request: MarkdownDocumentParseRequest): MarkdownDocumentParseResult;
|
|
34
|
+
private getScopedCache;
|
|
35
|
+
}
|
|
36
|
+
export declare const createMarkdownParseCache: (operations?: MarkdownParseOperations) => MarkdownParseCache;
|
|
37
|
+
export {};
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { lexWithFootnotes, lexWithoutFootnotes, parseBlocksWithFootnotes, parseBlocksWithoutFootnotes } from './marked/index.js';
|
|
2
|
+
const defaultMarkdownParseOperations = {
|
|
3
|
+
lexWithFootnotes,
|
|
4
|
+
lexWithoutFootnotes,
|
|
5
|
+
parseBlocksWithFootnotes,
|
|
6
|
+
parseBlocksWithoutFootnotes
|
|
7
|
+
};
|
|
8
|
+
const createEmptyFootnoteState = () => ({
|
|
9
|
+
refs: new Map(),
|
|
10
|
+
footnotes: new Map()
|
|
11
|
+
});
|
|
12
|
+
class ScopedMarkdownParseCache {
|
|
13
|
+
operations;
|
|
14
|
+
extensions;
|
|
15
|
+
blockParsesWithFootnotes = new Map();
|
|
16
|
+
blockParsesWithoutFootnotes = new Map();
|
|
17
|
+
lastDocumentParse = null;
|
|
18
|
+
constructor(operations, extensions) {
|
|
19
|
+
this.operations = operations;
|
|
20
|
+
this.extensions = extensions;
|
|
21
|
+
}
|
|
22
|
+
computeBlockParse(markdown, resolveFootnotes) {
|
|
23
|
+
return resolveFootnotes
|
|
24
|
+
? this.operations.lexWithFootnotes(markdown, this.extensions)
|
|
25
|
+
: {
|
|
26
|
+
tokens: this.operations.lexWithoutFootnotes(markdown, this.extensions),
|
|
27
|
+
footnotes: createEmptyFootnoteState()
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
parseBlock({ markdown, resolveFootnotes, cacheScope = 'stable' }) {
|
|
31
|
+
if (cacheScope === 'transient') {
|
|
32
|
+
return this.computeBlockParse(markdown, resolveFootnotes);
|
|
33
|
+
}
|
|
34
|
+
const cache = resolveFootnotes
|
|
35
|
+
? this.blockParsesWithFootnotes
|
|
36
|
+
: this.blockParsesWithoutFootnotes;
|
|
37
|
+
const cached = cache.get(markdown);
|
|
38
|
+
if (cached) {
|
|
39
|
+
return cached;
|
|
40
|
+
}
|
|
41
|
+
const result = this.computeBlockParse(markdown, resolveFootnotes);
|
|
42
|
+
cache.set(markdown, result);
|
|
43
|
+
return result;
|
|
44
|
+
}
|
|
45
|
+
parseDocument({ markdown, resolveFootnotes, splitBlocksFn = null, blockCacheScope = 'stable' }) {
|
|
46
|
+
if (this.lastDocumentParse?.markdown === markdown &&
|
|
47
|
+
this.lastDocumentParse.resolveFootnotes === resolveFootnotes &&
|
|
48
|
+
this.lastDocumentParse.splitBlocksFn === splitBlocksFn) {
|
|
49
|
+
return this.lastDocumentParse.result;
|
|
50
|
+
}
|
|
51
|
+
let result;
|
|
52
|
+
if (splitBlocksFn) {
|
|
53
|
+
result = {
|
|
54
|
+
blocks: splitBlocksFn(markdown),
|
|
55
|
+
footnotes: resolveFootnotes
|
|
56
|
+
? this.parseBlock({
|
|
57
|
+
markdown,
|
|
58
|
+
resolveFootnotes,
|
|
59
|
+
cacheScope: blockCacheScope
|
|
60
|
+
}).footnotes
|
|
61
|
+
: createEmptyFootnoteState()
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
else if (resolveFootnotes) {
|
|
65
|
+
result = this.operations.parseBlocksWithFootnotes(markdown, this.extensions);
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
result = {
|
|
69
|
+
blocks: this.operations.parseBlocksWithoutFootnotes(markdown, this.extensions),
|
|
70
|
+
footnotes: createEmptyFootnoteState()
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
this.lastDocumentParse = {
|
|
74
|
+
markdown,
|
|
75
|
+
resolveFootnotes,
|
|
76
|
+
splitBlocksFn,
|
|
77
|
+
result
|
|
78
|
+
};
|
|
79
|
+
return result;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
class MarkdownParseCache {
|
|
83
|
+
operations;
|
|
84
|
+
cachesByExtensions = new WeakMap();
|
|
85
|
+
cacheWithoutExtensions;
|
|
86
|
+
constructor(operations) {
|
|
87
|
+
this.operations = operations;
|
|
88
|
+
this.cacheWithoutExtensions = new ScopedMarkdownParseCache(operations, []);
|
|
89
|
+
}
|
|
90
|
+
parseBlock(request) {
|
|
91
|
+
return this.getScopedCache(request.extensions).parseBlock(request);
|
|
92
|
+
}
|
|
93
|
+
parseDocument(request) {
|
|
94
|
+
return this.getScopedCache(request.extensions).parseDocument(request);
|
|
95
|
+
}
|
|
96
|
+
getScopedCache(extensions = []) {
|
|
97
|
+
if (extensions.length === 0) {
|
|
98
|
+
return this.cacheWithoutExtensions;
|
|
99
|
+
}
|
|
100
|
+
const cached = this.cachesByExtensions.get(extensions);
|
|
101
|
+
if (cached) {
|
|
102
|
+
return cached;
|
|
103
|
+
}
|
|
104
|
+
const scopedCache = new ScopedMarkdownParseCache(this.operations, extensions);
|
|
105
|
+
this.cachesByExtensions.set(extensions, scopedCache);
|
|
106
|
+
return scopedCache;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
export const createMarkdownParseCache = (operations = defaultMarkdownParseOperations) => new MarkdownParseCache(operations);
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { StreamdownToken } from './marked/index.js';
|
|
2
|
+
export type MarkdownElement = {
|
|
3
|
+
tagName: string;
|
|
4
|
+
properties: Record<string, unknown>;
|
|
5
|
+
};
|
|
6
|
+
export type MarkdownParent = MarkdownElement & {
|
|
7
|
+
children?: readonly MarkdownElement[];
|
|
8
|
+
};
|
|
9
|
+
export type AllowElement = (element: Readonly<MarkdownElement>, index: number, parent: Readonly<MarkdownParent> | undefined) => boolean | null | undefined;
|
|
10
|
+
export type UrlTransform = (url: string, key: string, node: Readonly<MarkdownElement>) => string | null | undefined;
|
|
11
|
+
export declare const defaultUrlTransform: UrlTransform;
|
|
12
|
+
export type MarkdownFilteringOptions = {
|
|
13
|
+
allowElement?: AllowElement;
|
|
14
|
+
allowedElements?: readonly string[];
|
|
15
|
+
disallowedElements?: readonly string[];
|
|
16
|
+
skipHtml?: boolean;
|
|
17
|
+
unwrapDisallowed?: boolean;
|
|
18
|
+
};
|
|
19
|
+
export declare const createMarkdownElement: (tagName: string, properties?: Record<string, unknown>, children?: readonly MarkdownElement[]) => MarkdownParent;
|
|
20
|
+
export declare const applyMarkdownUrlTransform: (url: string, key: string, node: Readonly<MarkdownElement>, urlTransform?: UrlTransform) => string | null | undefined;
|
|
21
|
+
export declare const filterMarkdownTokens: (tokens: StreamdownToken[], options: MarkdownFilteringOptions) => StreamdownToken[];
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
export const defaultUrlTransform = (value) => value;
|
|
2
|
+
const normalizeElementSet = (elements) => {
|
|
3
|
+
if (!elements || elements.length === 0) {
|
|
4
|
+
return undefined;
|
|
5
|
+
}
|
|
6
|
+
return new Set(elements
|
|
7
|
+
.filter((element) => typeof element === 'string' && element.length > 0)
|
|
8
|
+
.map((element) => element.toLowerCase()));
|
|
9
|
+
};
|
|
10
|
+
export const createMarkdownElement = (tagName, properties = {}, children) => ({
|
|
11
|
+
tagName: tagName.toLowerCase(),
|
|
12
|
+
properties,
|
|
13
|
+
...(children && children.length > 0 ? { children } : {})
|
|
14
|
+
});
|
|
15
|
+
export const applyMarkdownUrlTransform = (url, key, node, urlTransform) => {
|
|
16
|
+
const transform = urlTransform ?? defaultUrlTransform;
|
|
17
|
+
return transform(url, key, node);
|
|
18
|
+
};
|
|
19
|
+
const parseMarkdownFilteringOptions = (options) => ({
|
|
20
|
+
allowElement: options.allowElement,
|
|
21
|
+
allowedElements: normalizeElementSet(options.allowedElements),
|
|
22
|
+
disallowedElements: normalizeElementSet(options.disallowedElements),
|
|
23
|
+
skipHtml: options.skipHtml === true,
|
|
24
|
+
unwrapDisallowed: options.unwrapDisallowed === true
|
|
25
|
+
});
|
|
26
|
+
const getHtmlTokenTagName = (token) => {
|
|
27
|
+
const match = token.raw.match(/^\s*<([A-Za-z][\w:-]*)(?=[\s/>])/);
|
|
28
|
+
if (!match) {
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
return match[1].toLowerCase();
|
|
32
|
+
};
|
|
33
|
+
const getTokenTagName = (token) => {
|
|
34
|
+
switch (token.type) {
|
|
35
|
+
case 'heading':
|
|
36
|
+
return `h${token.depth}`;
|
|
37
|
+
case 'paragraph':
|
|
38
|
+
return 'p';
|
|
39
|
+
case 'blockquote':
|
|
40
|
+
return 'blockquote';
|
|
41
|
+
case 'code':
|
|
42
|
+
return 'pre';
|
|
43
|
+
case 'codespan':
|
|
44
|
+
return 'code';
|
|
45
|
+
case 'list':
|
|
46
|
+
return token.ordered ? 'ol' : 'ul';
|
|
47
|
+
case 'list_item':
|
|
48
|
+
return 'li';
|
|
49
|
+
case 'table':
|
|
50
|
+
return 'table';
|
|
51
|
+
case 'thead':
|
|
52
|
+
return 'thead';
|
|
53
|
+
case 'tbody':
|
|
54
|
+
return 'tbody';
|
|
55
|
+
case 'tfoot':
|
|
56
|
+
return 'tfoot';
|
|
57
|
+
case 'tr':
|
|
58
|
+
return 'tr';
|
|
59
|
+
case 'td':
|
|
60
|
+
return 'td';
|
|
61
|
+
case 'th':
|
|
62
|
+
return 'th';
|
|
63
|
+
case 'image':
|
|
64
|
+
return 'img';
|
|
65
|
+
case 'link':
|
|
66
|
+
return 'a';
|
|
67
|
+
case 'strong':
|
|
68
|
+
return 'strong';
|
|
69
|
+
case 'em':
|
|
70
|
+
return 'em';
|
|
71
|
+
case 'del':
|
|
72
|
+
return 'del';
|
|
73
|
+
case 'hr':
|
|
74
|
+
return 'hr';
|
|
75
|
+
case 'br':
|
|
76
|
+
return 'br';
|
|
77
|
+
case 'sub':
|
|
78
|
+
return 'sub';
|
|
79
|
+
case 'sup':
|
|
80
|
+
return 'sup';
|
|
81
|
+
case 'descriptionList':
|
|
82
|
+
return 'dl';
|
|
83
|
+
case 'descriptionTerm':
|
|
84
|
+
return 'dt';
|
|
85
|
+
case 'descriptionDetail':
|
|
86
|
+
return 'dd';
|
|
87
|
+
case 'html':
|
|
88
|
+
return getHtmlTokenTagName(token);
|
|
89
|
+
default:
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
const getTokenProperties = (token) => {
|
|
94
|
+
switch (token.type) {
|
|
95
|
+
case 'heading':
|
|
96
|
+
return { depth: token.depth };
|
|
97
|
+
case 'paragraph':
|
|
98
|
+
return {
|
|
99
|
+
text: token.text
|
|
100
|
+
};
|
|
101
|
+
case 'code':
|
|
102
|
+
return {
|
|
103
|
+
lang: token.lang ?? undefined,
|
|
104
|
+
text: token.text
|
|
105
|
+
};
|
|
106
|
+
case 'list':
|
|
107
|
+
return {
|
|
108
|
+
listType: token.listType ?? undefined,
|
|
109
|
+
loose: token.loose,
|
|
110
|
+
ordered: token.ordered,
|
|
111
|
+
start: token.start ?? undefined
|
|
112
|
+
};
|
|
113
|
+
case 'list_item':
|
|
114
|
+
return {
|
|
115
|
+
checked: token.checked,
|
|
116
|
+
loose: token.loose,
|
|
117
|
+
task: token.task,
|
|
118
|
+
text: token.text,
|
|
119
|
+
value: token.value
|
|
120
|
+
};
|
|
121
|
+
case 'link':
|
|
122
|
+
return {
|
|
123
|
+
href: token.href,
|
|
124
|
+
title: token.title ?? undefined
|
|
125
|
+
};
|
|
126
|
+
case 'image':
|
|
127
|
+
return {
|
|
128
|
+
alt: token.text,
|
|
129
|
+
src: token.href,
|
|
130
|
+
title: token.title ?? undefined
|
|
131
|
+
};
|
|
132
|
+
default:
|
|
133
|
+
return {};
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
const shouldRemoveElement = (element, index, parent, options) => {
|
|
137
|
+
let remove = false;
|
|
138
|
+
if (options.allowedElements) {
|
|
139
|
+
remove = !options.allowedElements.has(element.tagName);
|
|
140
|
+
}
|
|
141
|
+
else if (options.disallowedElements) {
|
|
142
|
+
remove = options.disallowedElements.has(element.tagName);
|
|
143
|
+
}
|
|
144
|
+
if (!remove && options.allowElement) {
|
|
145
|
+
remove = !options.allowElement(element, index, parent);
|
|
146
|
+
}
|
|
147
|
+
return remove;
|
|
148
|
+
};
|
|
149
|
+
const filterTokens = (tokens, options, parent) => {
|
|
150
|
+
const prepared = [];
|
|
151
|
+
for (let index = 0; index < tokens.length; index += 1) {
|
|
152
|
+
const token = tokens[index];
|
|
153
|
+
if (!token) {
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
if (options.skipHtml && token.type === 'html') {
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
const tagName = getTokenTagName(token);
|
|
160
|
+
const elementProperties = getTokenProperties(token);
|
|
161
|
+
const currentElement = tagName ? createMarkdownElement(tagName, elementProperties) : undefined;
|
|
162
|
+
const hasChildTokens = Array.isArray(token.tokens);
|
|
163
|
+
const filteredChildren = hasChildTokens
|
|
164
|
+
? filterTokens(token.tokens, options, currentElement ?? parent)
|
|
165
|
+
: undefined;
|
|
166
|
+
const nextToken = hasChildTokens
|
|
167
|
+
? { ...token, tokens: filteredChildren?.tokens ?? [] }
|
|
168
|
+
: token;
|
|
169
|
+
const nextElement = tagName
|
|
170
|
+
? createMarkdownElement(tagName, getTokenProperties(nextToken), filteredChildren?.elements)
|
|
171
|
+
: undefined;
|
|
172
|
+
prepared.push({
|
|
173
|
+
element: nextElement,
|
|
174
|
+
filteredChildren,
|
|
175
|
+
token: nextToken
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
const siblingElements = prepared.flatMap((entry) => (entry.element ? [entry.element] : []));
|
|
179
|
+
const resolvedParent = parent
|
|
180
|
+
? createMarkdownElement(parent.tagName, parent.properties, siblingElements)
|
|
181
|
+
: undefined;
|
|
182
|
+
const filtered = {
|
|
183
|
+
elements: [],
|
|
184
|
+
tokens: []
|
|
185
|
+
};
|
|
186
|
+
for (let index = 0; index < prepared.length; index += 1) {
|
|
187
|
+
const entry = prepared[index];
|
|
188
|
+
if (!entry) {
|
|
189
|
+
continue;
|
|
190
|
+
}
|
|
191
|
+
if (entry.element && shouldRemoveElement(entry.element, index, resolvedParent, options)) {
|
|
192
|
+
if (options.unwrapDisallowed && entry.filteredChildren) {
|
|
193
|
+
filtered.elements.push(...entry.filteredChildren.elements);
|
|
194
|
+
filtered.tokens.push(...entry.filteredChildren.tokens);
|
|
195
|
+
}
|
|
196
|
+
continue;
|
|
197
|
+
}
|
|
198
|
+
if (entry.element) {
|
|
199
|
+
filtered.elements.push(entry.element);
|
|
200
|
+
}
|
|
201
|
+
filtered.tokens.push(entry.token);
|
|
202
|
+
}
|
|
203
|
+
return filtered;
|
|
204
|
+
};
|
|
205
|
+
export const filterMarkdownTokens = (tokens, options) => {
|
|
206
|
+
const parsedOptions = parseMarkdownFilteringOptions(options);
|
|
207
|
+
if (!parsedOptions.allowElement &&
|
|
208
|
+
!parsedOptions.allowedElements &&
|
|
209
|
+
!parsedOptions.disallowedElements &&
|
|
210
|
+
!parsedOptions.skipHtml) {
|
|
211
|
+
return tokens;
|
|
212
|
+
}
|
|
213
|
+
return filterTokens(tokens, parsedOptions).tokens;
|
|
214
|
+
};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { type MarkedToken, type Token, type TokenizerStartFunction, type TokenizerThis, type Tokens, type TokensList } from 'marked';
|
|
2
|
+
import { type AlertToken } from './marked-alert.js';
|
|
3
|
+
import { type Footnote, type FootnoteRef, type FootnoteToken } from './marked-footnotes.js';
|
|
4
|
+
import { type MathToken } from './marked-math.js';
|
|
5
|
+
import { type SubSupToken } from './marked-subsup.js';
|
|
6
|
+
import { type ListItemToken, type ListToken } from './marked-list.js';
|
|
7
|
+
import { type BrToken } from './marked-br.js';
|
|
8
|
+
import { type HrToken } from './marked-hr.js';
|
|
9
|
+
import { type TableToken, type THead, type TBody, type TFoot, type THeadRow, type TRow, type TH, type TD } from './marked-table.js';
|
|
10
|
+
import { type DescriptionDetailToken, type DescriptionListToken, type DescriptionTermToken, type DescriptionToken } from './marked-dl.js';
|
|
11
|
+
import { type AlignToken } from './marked-align.js';
|
|
12
|
+
import { type CitationToken } from './marked-citations.js';
|
|
13
|
+
import { type MdxToken } from './marked-mdx.js';
|
|
14
|
+
export type GenericToken = {
|
|
15
|
+
type: string;
|
|
16
|
+
raw: string;
|
|
17
|
+
tokens?: Token[];
|
|
18
|
+
} & Record<string, any>;
|
|
19
|
+
export type Extension = {
|
|
20
|
+
name: string;
|
|
21
|
+
level: 'block' | 'inline';
|
|
22
|
+
tokenizer: (this: TokenizerThis, src: string, tokens: Token[] | TokensList) => GenericToken | undefined;
|
|
23
|
+
start?: TokenizerStartFunction;
|
|
24
|
+
applyInBlockParsing?: boolean;
|
|
25
|
+
};
|
|
26
|
+
export type StreamdownToken = Exclude<MarkedToken, Tokens.List | Tokens.ListItem | Tokens.Table> | ListToken | ListItemToken | MathToken | AlertToken | FootnoteToken | SubSupToken | BrToken | HrToken | TableToken | THead | TBody | TFoot | THeadRow | TRow | TH | TD | DescriptionListToken | DescriptionToken | DescriptionDetailToken | DescriptionTermToken | AlignToken | CitationToken | MdxToken;
|
|
27
|
+
export type FootnoteState = {
|
|
28
|
+
refs: Map<string, FootnoteRef>;
|
|
29
|
+
footnotes: Map<string, Footnote>;
|
|
30
|
+
};
|
|
31
|
+
export type { TableToken, THead, TBody, TFoot, THeadRow, TRow, TH, TD } from './marked-table.js';
|
|
32
|
+
export declare const lexWithFootnotes: (markdown: string, extensions?: Extension[]) => {
|
|
33
|
+
tokens: StreamdownToken[];
|
|
34
|
+
footnotes: FootnoteState;
|
|
35
|
+
};
|
|
36
|
+
export declare const lexWithoutFootnotes: (markdown: string, extensions?: Extension[]) => StreamdownToken[];
|
|
37
|
+
export declare const lex: (markdown: string, extensions?: Extension[]) => StreamdownToken[];
|
|
38
|
+
export declare const parseBlocksWithFootnotes: (markdown: string, extensions?: Extension[]) => {
|
|
39
|
+
blocks: string[];
|
|
40
|
+
footnotes: FootnoteState;
|
|
41
|
+
};
|
|
42
|
+
export declare const parseBlocksWithoutFootnotes: (markdown: string, extensions?: Extension[]) => string[];
|
|
43
|
+
export declare const parseBlocks: (markdown: string, extensions?: Extension[]) => string[];
|
|
44
|
+
export type { MathToken, AlertToken, FootnoteToken, SubSupToken, BrToken, HrToken, AlignToken, CitationToken, MdxToken };
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
import { Lexer } from 'marked';
|
|
2
|
+
import { markedAlert } from './marked-alert.js';
|
|
3
|
+
import { markedFootnote } from './marked-footnotes.js';
|
|
4
|
+
import { markedMath } from './marked-math.js';
|
|
5
|
+
import { markedSub, markedSup } from './marked-subsup.js';
|
|
6
|
+
import { markedList } from './marked-list.js';
|
|
7
|
+
import { markedBr } from './marked-br.js';
|
|
8
|
+
import { markedHr } from './marked-hr.js';
|
|
9
|
+
import { markedTable } from './marked-table.js';
|
|
10
|
+
import { markedDl } from './marked-dl.js';
|
|
11
|
+
import { markedAlign } from './marked-align.js';
|
|
12
|
+
import { markedCitations } from './marked-citations.js';
|
|
13
|
+
import { markedMdx } from './marked-mdx.js';
|
|
14
|
+
import { markedCjk } from './marked-cjk.js';
|
|
15
|
+
const footnoteReferencePattern = /\[\^[\w-]{1,200}\](?!:)/;
|
|
16
|
+
const footnoteDefinitionPattern = /\[\^[\w-]{1,200}\]:/;
|
|
17
|
+
const openingTagPattern = /<(\w+)[\s>]/;
|
|
18
|
+
const voidElements = new Set([
|
|
19
|
+
'area',
|
|
20
|
+
'base',
|
|
21
|
+
'br',
|
|
22
|
+
'col',
|
|
23
|
+
'embed',
|
|
24
|
+
'hr',
|
|
25
|
+
'img',
|
|
26
|
+
'input',
|
|
27
|
+
'link',
|
|
28
|
+
'meta',
|
|
29
|
+
'param',
|
|
30
|
+
'source',
|
|
31
|
+
'track',
|
|
32
|
+
'wbr'
|
|
33
|
+
]);
|
|
34
|
+
const openTagPatternCache = new Map();
|
|
35
|
+
const closeTagPatternCache = new Map();
|
|
36
|
+
const getOpenTagPattern = (tagName) => {
|
|
37
|
+
const normalizedTag = tagName.toLowerCase();
|
|
38
|
+
const cached = openTagPatternCache.get(normalizedTag);
|
|
39
|
+
if (cached) {
|
|
40
|
+
return cached;
|
|
41
|
+
}
|
|
42
|
+
const pattern = new RegExp(`<${normalizedTag}(?=[\\s>/])[^>]*>`, 'gi');
|
|
43
|
+
openTagPatternCache.set(normalizedTag, pattern);
|
|
44
|
+
return pattern;
|
|
45
|
+
};
|
|
46
|
+
const getCloseTagPattern = (tagName) => {
|
|
47
|
+
const normalizedTag = tagName.toLowerCase();
|
|
48
|
+
const cached = closeTagPatternCache.get(normalizedTag);
|
|
49
|
+
if (cached) {
|
|
50
|
+
return cached;
|
|
51
|
+
}
|
|
52
|
+
const pattern = new RegExp(`</${normalizedTag}(?=[\\s>])[^>]*>`, 'gi');
|
|
53
|
+
closeTagPatternCache.set(normalizedTag, pattern);
|
|
54
|
+
return pattern;
|
|
55
|
+
};
|
|
56
|
+
const countNonSelfClosingOpenTags = (block, tagName) => {
|
|
57
|
+
if (voidElements.has(tagName.toLowerCase())) {
|
|
58
|
+
return 0;
|
|
59
|
+
}
|
|
60
|
+
const matches = block.match(getOpenTagPattern(tagName));
|
|
61
|
+
if (!matches) {
|
|
62
|
+
return 0;
|
|
63
|
+
}
|
|
64
|
+
let count = 0;
|
|
65
|
+
for (const match of matches) {
|
|
66
|
+
if (!match.trimEnd().endsWith('/>')) {
|
|
67
|
+
count += 1;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return count;
|
|
71
|
+
};
|
|
72
|
+
const countClosingTags = (block, tagName) => {
|
|
73
|
+
const matches = block.match(getCloseTagPattern(tagName));
|
|
74
|
+
return matches ? matches.length : 0;
|
|
75
|
+
};
|
|
76
|
+
const countDoubleDollars = (value) => {
|
|
77
|
+
let count = 0;
|
|
78
|
+
for (let i = 0; i < value.length - 1; i += 1) {
|
|
79
|
+
if (value[i] === '$' && value[i + 1] === '$') {
|
|
80
|
+
count += 1;
|
|
81
|
+
i += 1;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return count;
|
|
85
|
+
};
|
|
86
|
+
const parseExtensions = (...extensions) => {
|
|
87
|
+
const options = {
|
|
88
|
+
gfm: true,
|
|
89
|
+
extensions: {
|
|
90
|
+
block: [],
|
|
91
|
+
inline: [],
|
|
92
|
+
childTokens: {},
|
|
93
|
+
renderers: {},
|
|
94
|
+
startBlock: [],
|
|
95
|
+
startInline: []
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
extensions.forEach(({ level, name, tokenizer, ...rest }) => {
|
|
99
|
+
if ('start' in rest && rest.start) {
|
|
100
|
+
if (level === 'block') {
|
|
101
|
+
options.extensions.startBlock.push(rest.start);
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
options.extensions.startInline.push(rest.start);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
if (tokenizer) {
|
|
108
|
+
if (level === 'block') {
|
|
109
|
+
options.extensions.block.push(tokenizer);
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
options.extensions.inline.push(tokenizer);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
return options;
|
|
117
|
+
};
|
|
118
|
+
const cloneFootnoteState = (state) => ({
|
|
119
|
+
refs: new Map(state?.refs ?? []),
|
|
120
|
+
footnotes: new Map(state?.footnotes ?? [])
|
|
121
|
+
});
|
|
122
|
+
const filterOutFootnoteExtensions = (extensions) => extensions.filter(({ name }) => name !== 'footnote' && name !== 'footnoteRef');
|
|
123
|
+
const buildInlineLexerOptions = (extensions, includeFootnotes) => {
|
|
124
|
+
const resolvedExtensions = includeFootnotes ? extensions : filterOutFootnoteExtensions(extensions);
|
|
125
|
+
const hasCustomMathExtension = resolvedExtensions.some(({ name }) => name === 'math');
|
|
126
|
+
return parseExtensions(markedHr, markedTable, ...(includeFootnotes ? markedFootnote({ preferContext: false }) : []), markedAlert, ...(hasCustomMathExtension ? [] : markedMath), ...markedCjk, markedSub, markedSup, markedList, markedBr, markedDl, markedAlign, markedCitations, markedMdx, ...resolvedExtensions);
|
|
127
|
+
};
|
|
128
|
+
const buildBlockLexerOptions = (extensions, includeFootnotes) => parseExtensions(markedHr, ...(includeFootnotes ? markedFootnote({ preferContext: false }) : []), markedDl, markedTable, markedAlign, markedMdx, ...(includeFootnotes ? extensions : filterOutFootnoteExtensions(extensions)).filter(({ level, applyInBlockParsing }) => level === 'block' && applyInBlockParsing));
|
|
129
|
+
const defaultBlockLexerOptionsWithoutFootnotes = { gfm: true };
|
|
130
|
+
const mergeBlockTokens = (rawBlocks) => {
|
|
131
|
+
const mergedBlocks = [];
|
|
132
|
+
const htmlStack = [];
|
|
133
|
+
let previousTokenWasCode = false;
|
|
134
|
+
for (const block of rawBlocks) {
|
|
135
|
+
const currentBlock = block.raw;
|
|
136
|
+
const mergedBlocksLen = mergedBlocks.length;
|
|
137
|
+
if (htmlStack.length > 0) {
|
|
138
|
+
mergedBlocks[mergedBlocksLen - 1] += currentBlock;
|
|
139
|
+
const trackedTag = htmlStack[htmlStack.length - 1];
|
|
140
|
+
const newOpenTags = countNonSelfClosingOpenTags(currentBlock, trackedTag);
|
|
141
|
+
const newCloseTags = countClosingTags(currentBlock, trackedTag);
|
|
142
|
+
for (let index = 0; index < newOpenTags; index += 1) {
|
|
143
|
+
htmlStack.push(trackedTag);
|
|
144
|
+
}
|
|
145
|
+
for (let index = 0; index < newCloseTags; index += 1) {
|
|
146
|
+
if (htmlStack.length > 0 && htmlStack[htmlStack.length - 1] === trackedTag) {
|
|
147
|
+
htmlStack.pop();
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
continue;
|
|
151
|
+
}
|
|
152
|
+
if (block.type === 'html' && block.block) {
|
|
153
|
+
const openingTagMatch = currentBlock.match(openingTagPattern);
|
|
154
|
+
if (openingTagMatch) {
|
|
155
|
+
const tagName = openingTagMatch[1];
|
|
156
|
+
const openTags = countNonSelfClosingOpenTags(currentBlock, tagName);
|
|
157
|
+
const closeTags = countClosingTags(currentBlock, tagName);
|
|
158
|
+
if (openTags > closeTags) {
|
|
159
|
+
htmlStack.push(tagName);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
if (mergedBlocksLen > 0 && !previousTokenWasCode) {
|
|
164
|
+
const previousBlock = mergedBlocks[mergedBlocksLen - 1];
|
|
165
|
+
if (countDoubleDollars(previousBlock) % 2 === 1) {
|
|
166
|
+
mergedBlocks[mergedBlocksLen - 1] = previousBlock + currentBlock;
|
|
167
|
+
continue;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
mergedBlocks.push(currentBlock);
|
|
171
|
+
if (block.type !== 'space') {
|
|
172
|
+
previousTokenWasCode = block.type === 'code';
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
return mergedBlocks;
|
|
176
|
+
};
|
|
177
|
+
const parseDefaultBlocksWithoutFootnotes = (markdown) => mergeBlockTokens(Lexer.lex(markdown, defaultBlockLexerOptionsWithoutFootnotes).filter((block) => block.type !== 'space'));
|
|
178
|
+
export const lexWithFootnotes = (markdown, extensions = []) => {
|
|
179
|
+
const lexer = new Lexer(buildInlineLexerOptions(extensions, true));
|
|
180
|
+
return {
|
|
181
|
+
tokens: lexer
|
|
182
|
+
.lex(markdown)
|
|
183
|
+
.filter((token) => token.type !== 'space' && token.type !== 'footnote'),
|
|
184
|
+
footnotes: cloneFootnoteState(lexer.footnotes)
|
|
185
|
+
};
|
|
186
|
+
};
|
|
187
|
+
export const lexWithoutFootnotes = (markdown, extensions = []) => {
|
|
188
|
+
const lexer = new Lexer(buildInlineLexerOptions(extensions, false));
|
|
189
|
+
return lexer
|
|
190
|
+
.lex(markdown)
|
|
191
|
+
.filter((token) => token.type !== 'space' && token.type !== 'footnote');
|
|
192
|
+
};
|
|
193
|
+
export const lex = (markdown, extensions = []) => {
|
|
194
|
+
return lexWithFootnotes(markdown, extensions).tokens;
|
|
195
|
+
};
|
|
196
|
+
export const parseBlocksWithFootnotes = (markdown, extensions = []) => {
|
|
197
|
+
if (footnoteReferencePattern.test(markdown) || footnoteDefinitionPattern.test(markdown)) {
|
|
198
|
+
return {
|
|
199
|
+
blocks: [markdown],
|
|
200
|
+
footnotes: lexWithFootnotes(markdown, extensions).footnotes
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
if (extensions.length === 0) {
|
|
204
|
+
return {
|
|
205
|
+
blocks: parseDefaultBlocksWithoutFootnotes(markdown),
|
|
206
|
+
footnotes: cloneFootnoteState()
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
const blockLexer = new Lexer(buildBlockLexerOptions(extensions, true));
|
|
210
|
+
const rawBlocks = blockLexer
|
|
211
|
+
.blockTokens(markdown, [])
|
|
212
|
+
.filter((block) => block.type !== 'space' && block.type !== 'footnote');
|
|
213
|
+
return {
|
|
214
|
+
blocks: mergeBlockTokens(rawBlocks),
|
|
215
|
+
footnotes: cloneFootnoteState(blockLexer.footnotes)
|
|
216
|
+
};
|
|
217
|
+
};
|
|
218
|
+
export const parseBlocksWithoutFootnotes = (markdown, extensions = []) => {
|
|
219
|
+
if (extensions.length === 0) {
|
|
220
|
+
return parseDefaultBlocksWithoutFootnotes(markdown);
|
|
221
|
+
}
|
|
222
|
+
const blockLexer = new Lexer(buildBlockLexerOptions(extensions, false));
|
|
223
|
+
const rawBlocks = blockLexer.blockTokens(markdown, []).filter((block) => block.type !== 'space');
|
|
224
|
+
return mergeBlockTokens(rawBlocks);
|
|
225
|
+
};
|
|
226
|
+
export const parseBlocks = (markdown, extensions = []) => {
|
|
227
|
+
return parseBlocksWithFootnotes(markdown, extensions).blocks;
|
|
228
|
+
};
|