@gravity-ui/markdown-editor 14.3.1 → 14.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (112) hide show
  1. package/build/cjs/bundle/Editor.d.ts +2 -0
  2. package/build/cjs/bundle/Editor.js +9 -2
  3. package/build/cjs/bundle/MarkdownEditorView.js +1 -0
  4. package/build/cjs/bundle/SplitModeView.js +1 -0
  5. package/build/cjs/bundle/config/markup.d.ts +41 -17
  6. package/build/cjs/bundle/config/markup.js +413 -308
  7. package/build/cjs/bundle/config/wysiwyg.d.ts +29 -18
  8. package/build/cjs/bundle/config/wysiwyg.js +526 -310
  9. package/build/cjs/bundle/sticky/sticky.css +1 -1
  10. package/build/cjs/bundle/types.d.ts +24 -0
  11. package/build/cjs/bundle/useMarkdownEditor.js +6 -2
  12. package/build/cjs/bundle/wysiwyg-preset.d.ts +11 -2
  13. package/build/cjs/bundle/wysiwyg-preset.js +1 -0
  14. package/build/cjs/extensions/behavior/Clipboard/utils.d.ts +1 -0
  15. package/build/cjs/extensions/behavior/Clipboard/utils.js +1 -0
  16. package/build/cjs/extensions/markdown/CodeBlock/handle-paste.js +5 -17
  17. package/build/cjs/extensions/yfm/YfmCut/YfmCutSpecs/const.d.ts +4 -0
  18. package/build/cjs/extensions/yfm/YfmCut/YfmCutSpecs/const.js +6 -1
  19. package/build/cjs/extensions/yfm/YfmCut/YfmCutSpecs/index.d.ts +8 -1
  20. package/build/cjs/extensions/yfm/YfmCut/YfmCutSpecs/index.js +11 -5
  21. package/build/cjs/extensions/yfm/YfmCut/YfmCutSpecs/parser.js +5 -1
  22. package/build/cjs/extensions/yfm/YfmCut/YfmCutSpecs/schema.d.ts +1 -1
  23. package/build/cjs/extensions/yfm/YfmCut/YfmCutSpecs/schema.js +8 -3
  24. package/build/cjs/extensions/yfm/YfmCut/YfmCutSpecs/serializer.d.ts +3 -1
  25. package/build/cjs/extensions/yfm/YfmCut/YfmCutSpecs/serializer.js +42 -24
  26. package/build/cjs/extensions/yfm/YfmFile/YfmFileSpecs/const.d.ts +12 -0
  27. package/build/cjs/extensions/yfm/YfmFile/YfmFileSpecs/const.js +30 -11
  28. package/build/cjs/extensions/yfm/YfmFile/YfmFileSpecs/index.d.ts +8 -1
  29. package/build/cjs/extensions/yfm/YfmFile/YfmFileSpecs/index.js +46 -24
  30. package/build/cjs/extensions/yfm/YfmFile/index.d.ts +1 -0
  31. package/build/cjs/extensions/yfm/YfmFile/index.js +1 -0
  32. package/build/cjs/index.d.ts +1 -1
  33. package/build/cjs/index.js +2 -1
  34. package/build/cjs/markup/codemirror/create.d.ts +8 -5
  35. package/build/cjs/markup/codemirror/create.js +43 -5
  36. package/build/cjs/markup/codemirror/directive-facet.d.ts +3 -0
  37. package/build/cjs/markup/codemirror/directive-facet.js +8 -0
  38. package/build/cjs/markup/codemirror/html-to-markdown/converters.d.ts +111 -0
  39. package/build/cjs/markup/codemirror/html-to-markdown/converters.js +214 -0
  40. package/build/cjs/markup/codemirror/html-to-markdown/handlers.d.ts +104 -0
  41. package/build/cjs/markup/codemirror/html-to-markdown/handlers.js +233 -0
  42. package/build/cjs/markup/codemirror/html-to-markdown/helpers.d.ts +1 -0
  43. package/build/cjs/markup/codemirror/html-to-markdown/helpers.js +21 -0
  44. package/build/cjs/markup/codemirror/index.d.ts +1 -0
  45. package/build/cjs/markup/codemirror/index.js +3 -1
  46. package/build/cjs/markup/codemirror/yfm.d.ts +5 -0
  47. package/build/cjs/markup/codemirror/yfm.js +26 -4
  48. package/build/cjs/markup/commands/inline.js +18 -8
  49. package/build/cjs/markup/commands/yfm.js +10 -1
  50. package/build/cjs/utils/clipboard.d.ts +14 -0
  51. package/build/cjs/utils/clipboard.js +36 -1
  52. package/build/cjs/utils/directive.d.ts +28 -0
  53. package/build/cjs/utils/directive.js +55 -0
  54. package/build/cjs/utils/index.d.ts +1 -0
  55. package/build/cjs/version.js +1 -1
  56. package/build/esm/bundle/Editor.d.ts +2 -0
  57. package/build/esm/bundle/Editor.js +9 -2
  58. package/build/esm/bundle/MarkdownEditorView.js +1 -0
  59. package/build/esm/bundle/SplitModeView.js +1 -0
  60. package/build/esm/bundle/config/markup.d.ts +41 -17
  61. package/build/esm/bundle/config/markup.js +411 -307
  62. package/build/esm/bundle/config/wysiwyg.d.ts +29 -18
  63. package/build/esm/bundle/config/wysiwyg.js +499 -284
  64. package/build/esm/bundle/sticky/sticky.css +1 -1
  65. package/build/esm/bundle/types.d.ts +24 -0
  66. package/build/esm/bundle/useMarkdownEditor.js +6 -2
  67. package/build/esm/bundle/wysiwyg-preset.d.ts +11 -2
  68. package/build/esm/bundle/wysiwyg-preset.js +1 -0
  69. package/build/esm/extensions/behavior/Clipboard/utils.d.ts +1 -0
  70. package/build/esm/extensions/behavior/Clipboard/utils.js +1 -0
  71. package/build/esm/extensions/markdown/CodeBlock/handle-paste.js +2 -14
  72. package/build/esm/extensions/yfm/YfmCut/YfmCutSpecs/const.d.ts +4 -0
  73. package/build/esm/extensions/yfm/YfmCut/YfmCutSpecs/const.js +5 -0
  74. package/build/esm/extensions/yfm/YfmCut/YfmCutSpecs/index.d.ts +8 -1
  75. package/build/esm/extensions/yfm/YfmCut/YfmCutSpecs/index.js +8 -3
  76. package/build/esm/extensions/yfm/YfmCut/YfmCutSpecs/parser.js +6 -2
  77. package/build/esm/extensions/yfm/YfmCut/YfmCutSpecs/schema.d.ts +1 -1
  78. package/build/esm/extensions/yfm/YfmCut/YfmCutSpecs/schema.js +8 -3
  79. package/build/esm/extensions/yfm/YfmCut/YfmCutSpecs/serializer.d.ts +3 -1
  80. package/build/esm/extensions/yfm/YfmCut/YfmCutSpecs/serializer.js +41 -24
  81. package/build/esm/extensions/yfm/YfmFile/YfmFileSpecs/const.d.ts +12 -0
  82. package/build/esm/extensions/yfm/YfmFile/YfmFileSpecs/const.js +21 -2
  83. package/build/esm/extensions/yfm/YfmFile/YfmFileSpecs/index.d.ts +8 -1
  84. package/build/esm/extensions/yfm/YfmFile/YfmFileSpecs/index.js +32 -10
  85. package/build/esm/extensions/yfm/YfmFile/index.d.ts +1 -0
  86. package/build/esm/extensions/yfm/YfmFile/index.js +2 -1
  87. package/build/esm/index.d.ts +1 -1
  88. package/build/esm/index.js +1 -1
  89. package/build/esm/markup/codemirror/create.d.ts +8 -5
  90. package/build/esm/markup/codemirror/create.js +42 -4
  91. package/build/esm/markup/codemirror/directive-facet.d.ts +3 -0
  92. package/build/esm/markup/codemirror/directive-facet.js +5 -0
  93. package/build/esm/markup/codemirror/html-to-markdown/converters.d.ts +111 -0
  94. package/build/esm/markup/codemirror/html-to-markdown/converters.js +210 -0
  95. package/build/esm/markup/codemirror/html-to-markdown/handlers.d.ts +104 -0
  96. package/build/esm/markup/codemirror/html-to-markdown/handlers.js +215 -0
  97. package/build/esm/markup/codemirror/html-to-markdown/helpers.d.ts +1 -0
  98. package/build/esm/markup/codemirror/html-to-markdown/helpers.js +17 -0
  99. package/build/esm/markup/codemirror/index.d.ts +1 -0
  100. package/build/esm/markup/codemirror/index.js +1 -0
  101. package/build/esm/markup/codemirror/yfm.d.ts +5 -0
  102. package/build/esm/markup/codemirror/yfm.js +25 -3
  103. package/build/esm/markup/commands/inline.js +18 -8
  104. package/build/esm/markup/commands/yfm.js +9 -1
  105. package/build/esm/utils/clipboard.d.ts +14 -0
  106. package/build/esm/utils/clipboard.js +32 -0
  107. package/build/esm/utils/directive.d.ts +28 -0
  108. package/build/esm/utils/directive.js +51 -0
  109. package/build/esm/utils/index.d.ts +1 -0
  110. package/build/esm/version.js +1 -1
  111. package/build/styles.css +1 -1
  112. package/package.json +10 -6
@@ -3,18 +3,20 @@ import { defaultKeymap, history, historyKeymap, indentWithTab, insertNewlineKeep
3
3
  import { syntaxHighlighting } from '@codemirror/language';
4
4
  import { EditorView, keymap, placeholder } from '@codemirror/view';
5
5
  import { ActionName } from '../../bundle/config/action-names';
6
- import { DataTransferType } from '../../extensions/behavior/Clipboard/utils';
7
6
  import { logger } from '../../logger';
8
7
  import { Action as A, formatter as f } from '../../shortcuts';
8
+ import { DataTransferType, shouldSkipHtmlConversion } from '../../utils/clipboard';
9
9
  import { insertImages, insertLink, toH1, toH2, toH3, toH4, toH5, toH6, toggleBold, toggleItalic, toggleStrikethrough, toggleUnderline, wrapToCodeBlock, wrapToInlineCode, wrapToYfmCut, wrapToYfmNote, } from '../commands';
10
+ import { DirectiveSyntaxFacet } from './directive-facet';
10
11
  import { FileUploadHandlerFacet } from './files-upload-facet';
11
12
  import { gravityHighlightStyle, gravityTheme } from './gravity';
13
+ import { MarkdownConverter } from './html-to-markdown/converters';
12
14
  import { PairingCharactersExtension } from './pairing-chars';
13
15
  import { ReactRendererFacet } from './react-facet';
14
16
  import { SearchPanelPlugin } from './search-plugin/plugin';
15
17
  import { yfmLang } from './yfm';
16
18
  export function createCodemirror(params) {
17
- const { doc, reactRenderer, onCancel, onScroll, onSubmit, onChange, onDocChange, disabledExtensions = {}, keymaps = [], receiver, yfmLangOptions, extensions: extraExtensions, placeholder: placeholderContent, autocompletion: autocompletionConfig, parseInsertedUrlAsImage, } = params;
19
+ const { doc, reactRenderer, onCancel, onScroll, onSubmit, onChange, onDocChange, disabledExtensions = {}, keymaps = [], receiver, yfmLangOptions, extensions: extraExtensions, placeholder: placeholderContent, autocompletion: autocompletionConfig, parseHtmlOnPaste, parseInsertedUrlAsImage, directiveSyntax, } = params;
18
20
  const extensions = [gravityTheme, placeholder(placeholderContent)];
19
21
  if (!disabledExtensions.history) {
20
22
  extensions.push(history());
@@ -60,13 +62,49 @@ export function createCodemirror(params) {
60
62
  ...defaultKeymap,
61
63
  ...(disabledExtensions.history ? [] : historyKeymap),
62
64
  ...keymaps,
63
- ]), autocompletion(autocompletionConfig), yfmLang(yfmLangOptions), ReactRendererFacet.of(reactRenderer), PairingCharactersExtension, EditorView.lineWrapping, EditorView.contentAttributes.of({ spellcheck: 'true' }), EditorView.domEventHandlers({
65
+ ]), autocompletion(autocompletionConfig), yfmLang(yfmLangOptions), ReactRendererFacet.of(reactRenderer), DirectiveSyntaxFacet.of(directiveSyntax), PairingCharactersExtension, EditorView.lineWrapping, EditorView.contentAttributes.of({ spellcheck: 'true' }), EditorView.domEventHandlers({
64
66
  scroll(event) {
65
67
  onScroll(event);
66
68
  },
67
69
  paste(event, editor) {
68
70
  var _a;
69
- if (event.clipboardData && parseInsertedUrlAsImage) {
71
+ if (!event.clipboardData)
72
+ return;
73
+ // if clipboard contains YFM content - avoid any meddling with pasted content
74
+ // since text/yfm will contain valid markdown
75
+ const yfmContent = event.clipboardData.getData(DataTransferType.Yfm);
76
+ if (yfmContent) {
77
+ event.preventDefault();
78
+ editor.dispatch(editor.state.replaceSelection(yfmContent));
79
+ return;
80
+ }
81
+ // checking if a copy buffer content is suitable for convertion
82
+ const shouldSkipHtml = shouldSkipHtmlConversion(event.clipboardData);
83
+ // if we have text/html inside copy/paste buffer
84
+ const htmlContent = event.clipboardData.getData(DataTransferType.Html);
85
+ // if we pasting markdown from VsCode we need skip html transformation
86
+ if (htmlContent && parseHtmlOnPaste && !shouldSkipHtml) {
87
+ let parsedMarkdownMarkup;
88
+ try {
89
+ const parser = new DOMParser();
90
+ const htmlDoc = parser.parseFromString(htmlContent, 'text/html');
91
+ const converter = new MarkdownConverter();
92
+ parsedMarkdownMarkup = converter.processNode(htmlDoc.body).trim();
93
+ }
94
+ catch (e) {
95
+ // The code is pretty new and there might be random issues we haven't caught yet,
96
+ // especially with invalid HTML or weird DOM parsing errors.
97
+ // If something goes wrong, I just want to fall back to the "default pasting"
98
+ // rather than break the entire experience for the user.
99
+ logger.error(e);
100
+ }
101
+ if (parsedMarkdownMarkup !== undefined) {
102
+ event.preventDefault();
103
+ editor.dispatch(editor.state.replaceSelection(parsedMarkdownMarkup));
104
+ return;
105
+ }
106
+ }
107
+ if (parseInsertedUrlAsImage) {
70
108
  const { imageUrl, title } = parseInsertedUrlAsImage((_a = event.clipboardData.getData(DataTransferType.Text)) !== null && _a !== void 0 ? _a : '') || {};
71
109
  if (!imageUrl) {
72
110
  return;
@@ -0,0 +1,3 @@
1
+ import { Facet } from '@codemirror/state';
2
+ import type { DirectiveSyntaxContext } from '../../utils/directive';
3
+ export declare const DirectiveSyntaxFacet: Facet<DirectiveSyntaxContext, Omit<DirectiveSyntaxContext, "shouldSerializeToDirective">>;
@@ -0,0 +1,5 @@
1
+ import { Facet } from '@codemirror/state';
2
+ export const DirectiveSyntaxFacet = Facet.define({
3
+ combine: ([context]) => context,
4
+ static: true,
5
+ });
@@ -0,0 +1,111 @@
1
+ /**
2
+ * Interface defining methods for visiting different types of HTML nodes.
3
+ * Each method corresponds to a specific HTML element type and returns its Markdown representation.
4
+ */
5
+ export interface HTMLNodeVisitor {
6
+ /** Converts a text node to Markdown */
7
+ visitText(node: Text): string;
8
+ /** Converts an anchor element to Markdown link syntax */
9
+ visitLink(node: HTMLAnchorElement): string;
10
+ /** Converts a header element to Markdown heading syntax */
11
+ visitHeader(node: HTMLElement, level: number): string;
12
+ /** Converts a paragraph element to Markdown */
13
+ visitParagraph(node: HTMLElement): string;
14
+ /** Converts formatting elements (bold, italic, etc.) to Markdown */
15
+ visitFormatting(node: HTMLElement): string;
16
+ /** Converts code elements to Markdown inline code syntax */
17
+ visitCode(node: HTMLElement): string;
18
+ /** Handles generic HTML elements with no specific Markdown conversion */
19
+ visitGeneric(node: HTMLElement): string;
20
+ /** Converts an HTML div element to Markdown format, adding a single newline */
21
+ visitDiv(node: HTMLElement): string;
22
+ /** Converts an HTML br element to a newline in Markdown */
23
+ visitBr(): string;
24
+ /** Converts a table row element to Markdown */
25
+ visitTableRow(node: HTMLTableRowElement): string;
26
+ /** Converts an HTML table element to Markdown table format */
27
+ visitTable(node: HTMLTableElement): string;
28
+ /** Convert img tag to Markdown image format */
29
+ visitImage(node: HTMLImageElement): string;
30
+ }
31
+ /**
32
+ * Main converter class that implements the visitor interface to convert HTML to Markdown.
33
+ * Uses the Chain of Responsibility pattern for handling different node types.
34
+ */
35
+ export declare class MarkdownConverter implements HTMLNodeVisitor {
36
+ private handler;
37
+ constructor();
38
+ /**
39
+ * Converts a text node to Markdown, escaping special characters.
40
+ */
41
+ visitText(node: Text): string;
42
+ /**
43
+ * Converts an HTML anchor element to Markdown link syntax.
44
+ */
45
+ visitLink(node: HTMLAnchorElement): string;
46
+ /**
47
+ * Converts an HTML heading element to Markdown heading syntax.
48
+ */
49
+ visitHeader(node: HTMLElement, level: number): string;
50
+ /**
51
+ * Converts an HTML paragraph to Markdown format.
52
+ */
53
+ visitParagraph(node: HTMLElement): string;
54
+ /**
55
+ * Applies Markdown formatting (bold, italic, etc.) to text content.
56
+ */
57
+ visitFormatting(node: HTMLElement): string;
58
+ /**
59
+ * Converts HTML code elements to Markdown inline code syntax.
60
+ */
61
+ visitCode(node: HTMLElement): string;
62
+ /**
63
+ * Handles generic HTML elements by processing their children.
64
+ */
65
+ visitGeneric(node: HTMLElement): string;
66
+ /**
67
+ * Converts an HTML div element to Markdown format, adding a single newline.
68
+ */
69
+ visitDiv(node: HTMLElement): string;
70
+ /**
71
+ * Converts an HTML br element to a newline in Markdown.
72
+ */
73
+ visitBr(): string;
74
+ /**
75
+ * Converts an HTML table row element to Markdown table row format.
76
+ */
77
+ visitTableRow(node: HTMLTableRowElement): string;
78
+ /**
79
+ * Converts an HTML table element to Markdown table format.
80
+ */
81
+ visitTable(node: HTMLTableElement): string;
82
+ /**
83
+ * Converts img tag to Markdown image format
84
+ */
85
+ visitImage(node: HTMLImageElement): string;
86
+ /**
87
+ * Processes a single node using the handler chain.
88
+ */
89
+ processNode(node: Node): string;
90
+ /**
91
+ * Creates and links together handlers in a specific order implementing the Chain of Responsibility pattern.
92
+ * @returns The first handler in the chain
93
+ */
94
+ private setupHandlerChain;
95
+ /**
96
+ * Recursively collects and processes text content from a node and its children.
97
+ */
98
+ private collectTextContent;
99
+ /**
100
+ * Collects raw text content from code elements.
101
+ */
102
+ private collectCodeContent;
103
+ /**
104
+ * Processes all child nodes of a given node.
105
+ */
106
+ private processChildren;
107
+ /**
108
+ * Gets the first handler in the chain.
109
+ */
110
+ private getHandler;
111
+ }
@@ -0,0 +1,210 @@
1
+ import { BrHandler, CodeHandler, DivHandler, FormattingHandler, GenericHandler, HeaderHandler, ImageHandler, LinkHandler, OrderedListHandler, ParagraphHandler, TableHandler, TableRowHandler, TextNodeHandler, UnorderedListHandler, } from './handlers';
2
+ import { applyFormatting } from './helpers';
3
+ /**
4
+ * Main converter class that implements the visitor interface to convert HTML to Markdown.
5
+ * Uses the Chain of Responsibility pattern for handling different node types.
6
+ */
7
+ export class MarkdownConverter {
8
+ constructor() {
9
+ // Set up the chain of responsibility for handling different node types
10
+ this.handler = this.setupHandlerChain();
11
+ }
12
+ /**
13
+ * Converts a text node to Markdown, escaping special characters.
14
+ */
15
+ visitText(node) {
16
+ return (node.textContent || '').replace(/\n+/g, '').replace(/([<>])/g, '\\$1');
17
+ }
18
+ /**
19
+ * Converts an HTML anchor element to Markdown link syntax.
20
+ */
21
+ visitLink(node) {
22
+ var _a;
23
+ const linkText = this.collectTextContent(node);
24
+ const url = node.href || '';
25
+ // Handle links with formatted content vs plain text differently
26
+ const formattedText = node.childNodes.length === 1 && ((_a = node.firstChild) === null || _a === void 0 ? void 0 : _a.nodeType) === Node.TEXT_NODE
27
+ ? applyFormatting(linkText, node) // Plain text link
28
+ : Array.from(node.childNodes)
29
+ .map((child) => {
30
+ if (child.nodeType === Node.ELEMENT_NODE) {
31
+ return applyFormatting(child.textContent || '', child);
32
+ }
33
+ return child.textContent || '';
34
+ })
35
+ .join(''); // Apply formatting for each formatted child node
36
+ return `[${formattedText}](${url} "${linkText.replace(/"/g, '\\"')}")`;
37
+ }
38
+ /**
39
+ * Converts an HTML heading element to Markdown heading syntax.
40
+ */
41
+ visitHeader(node, level) {
42
+ const headerContent = this.collectTextContent(node);
43
+ return '#'.repeat(level) + ' ' + headerContent + '\n';
44
+ }
45
+ /**
46
+ * Converts an HTML paragraph to Markdown format.
47
+ */
48
+ visitParagraph(node) {
49
+ const content = this.processChildren(node);
50
+ return content.trim() + '\n\n';
51
+ }
52
+ /**
53
+ * Applies Markdown formatting (bold, italic, etc.) to text content.
54
+ */
55
+ visitFormatting(node) {
56
+ var _a;
57
+ if (node.childNodes.length === 1 && ((_a = node.firstChild) === null || _a === void 0 ? void 0 : _a.nodeType) === Node.TEXT_NODE) {
58
+ const text = this.collectTextContent(node);
59
+ return applyFormatting(text, node);
60
+ }
61
+ return applyFormatting(this.visitGeneric(node), node);
62
+ }
63
+ /**
64
+ * Converts HTML code elements to Markdown inline code syntax.
65
+ */
66
+ visitCode(node) {
67
+ const codeContent = this.collectCodeContent(node);
68
+ if (codeContent.includes('\n')) {
69
+ return '```\n' + codeContent + '\n```\n';
70
+ }
71
+ else if (codeContent.includes('`')) {
72
+ return '`` ' + codeContent + ' ``';
73
+ }
74
+ else {
75
+ return `\`${codeContent}\``;
76
+ }
77
+ }
78
+ /**
79
+ * Handles generic HTML elements by processing their children.
80
+ */
81
+ visitGeneric(node) {
82
+ return this.processChildren(node);
83
+ }
84
+ /**
85
+ * Converts an HTML div element to Markdown format, adding a single newline.
86
+ */
87
+ visitDiv(node) {
88
+ const content = this.processChildren(node);
89
+ return content + '\n'; // Add a single newline for <div>
90
+ }
91
+ /**
92
+ * Converts an HTML br element to a newline in Markdown.
93
+ */
94
+ visitBr() {
95
+ return '\n'; // Single newline for <br>
96
+ }
97
+ /**
98
+ * Converts an HTML table row element to Markdown table row format.
99
+ */
100
+ visitTableRow(node) {
101
+ const cells = Array.from(node.children).map((cell) => {
102
+ return this.visitGeneric(cell).trim() || '';
103
+ });
104
+ return '||\n' + cells.join('\n|\n') + '\n||';
105
+ }
106
+ /**
107
+ * Converts an HTML table element to Markdown table format.
108
+ */
109
+ visitTable(node) {
110
+ const rows = [];
111
+ const tableRows = Array.from(node.querySelectorAll('tr'));
112
+ tableRows.forEach((row) => {
113
+ rows.push(this.visitTableRow(row));
114
+ });
115
+ return '\n\n#|\n' + rows.join('\n') + '\n|#\n\n';
116
+ }
117
+ /**
118
+ * Converts img tag to Markdown image format
119
+ */
120
+ visitImage(node) {
121
+ const imgElement = node;
122
+ const altText = imgElement.alt || '';
123
+ const src = imgElement.src || '';
124
+ return `![${altText}](${src})`;
125
+ }
126
+ /**
127
+ * Processes a single node using the handler chain.
128
+ */
129
+ processNode(node) {
130
+ const result = this.getHandler().handle(node, this);
131
+ return result;
132
+ }
133
+ /**
134
+ * Creates and links together handlers in a specific order implementing the Chain of Responsibility pattern.
135
+ * @returns The first handler in the chain
136
+ */
137
+ setupHandlerChain() {
138
+ // Create handlers for each type of node
139
+ const textHandler = new TextNodeHandler();
140
+ const linkHandler = new LinkHandler();
141
+ const headerHandler = new HeaderHandler();
142
+ const paragraphHandler = new ParagraphHandler();
143
+ const formattingHandler = new FormattingHandler();
144
+ const codeHandler = new CodeHandler();
145
+ const genericHandler = new GenericHandler();
146
+ const orderedListHandler = new OrderedListHandler();
147
+ const unorderedListHandler = new UnorderedListHandler();
148
+ const divHandler = new DivHandler();
149
+ const brHandler = new BrHandler();
150
+ const tableRowHandler = new TableRowHandler();
151
+ const tableHandler = new TableHandler();
152
+ const imageHandler = new ImageHandler(); // New handler for <img>
153
+ // Chain handlers together in priority order
154
+ textHandler
155
+ .setNext(linkHandler)
156
+ .setNext(headerHandler)
157
+ .setNext(paragraphHandler)
158
+ .setNext(divHandler)
159
+ .setNext(brHandler)
160
+ .setNext(orderedListHandler)
161
+ .setNext(unorderedListHandler)
162
+ .setNext(formattingHandler)
163
+ .setNext(codeHandler)
164
+ .setNext(imageHandler) // Add image handler
165
+ .setNext(tableHandler)
166
+ .setNext(tableRowHandler)
167
+ .setNext(genericHandler);
168
+ return textHandler;
169
+ }
170
+ /**
171
+ * Recursively collects and processes text content from a node and its children.
172
+ */
173
+ collectTextContent(node) {
174
+ // handle seo elements (hide it's content)
175
+ if (node.className === 'visually-hidden') {
176
+ return '';
177
+ }
178
+ if (node.nodeType === Node.TEXT_NODE) {
179
+ return this.visitText(node);
180
+ }
181
+ return Array.from(node.childNodes)
182
+ .map((child) => this.collectTextContent(child))
183
+ .join('');
184
+ }
185
+ /**
186
+ * Collects raw text content from code elements.
187
+ */
188
+ collectCodeContent(node) {
189
+ if (node.nodeType === Node.TEXT_NODE) {
190
+ return node.textContent || '';
191
+ }
192
+ return Array.from(node.childNodes)
193
+ .map((child) => this.collectCodeContent(child))
194
+ .join('');
195
+ }
196
+ /**
197
+ * Processes all child nodes of a given node.
198
+ */
199
+ processChildren(node) {
200
+ return Array.from(node.childNodes)
201
+ .map((child) => this.processNode(child))
202
+ .join('');
203
+ }
204
+ /**
205
+ * Gets the first handler in the chain.
206
+ */
207
+ getHandler() {
208
+ return this.handler;
209
+ }
210
+ }
@@ -0,0 +1,104 @@
1
+ import { HTMLNodeVisitor } from './converters';
2
+ /**
3
+ * Base handler class implementing the Chain of Responsibility pattern for HTML node processing.
4
+ * Each concrete handler decides whether it can process a node or should pass it to the next handler.
5
+ */
6
+ export declare abstract class NodeHandler {
7
+ protected next: NodeHandler | null;
8
+ /**
9
+ * Sets up the next handler in the chain
10
+ */
11
+ setNext(handler: NodeHandler): NodeHandler;
12
+ /**
13
+ * Process the given node or delegate to the next handler
14
+ */
15
+ abstract handle(node: Node, visitor: HTMLNodeVisitor): string;
16
+ /**
17
+ * Delegates processing to the next handler in the chain
18
+ */
19
+ protected handleNext(node: Node, visitor: HTMLNodeVisitor): string;
20
+ }
21
+ /**
22
+ * Handles text nodes, converting them to markdown text
23
+ */
24
+ export declare class TextNodeHandler extends NodeHandler {
25
+ handle(node: Node, visitor: HTMLNodeVisitor): string;
26
+ }
27
+ /**
28
+ * Handles anchor elements, converting them to markdown links
29
+ */
30
+ export declare class LinkHandler extends NodeHandler {
31
+ handle(node: Node, visitor: HTMLNodeVisitor): string;
32
+ }
33
+ /**
34
+ * Handles header elements (h1-h6), converting them to markdown headers
35
+ */
36
+ export declare class HeaderHandler extends NodeHandler {
37
+ handle(node: Node, visitor: HTMLNodeVisitor): string;
38
+ }
39
+ /**
40
+ * Handles paragraph elements, converting them to markdown paragraphs
41
+ */
42
+ export declare class ParagraphHandler extends NodeHandler {
43
+ handle(node: Node, visitor: HTMLNodeVisitor): string;
44
+ }
45
+ /**
46
+ * Handles text formatting elements (b, strong, i, em), converting them to markdown formatting
47
+ */
48
+ export declare class FormattingHandler extends NodeHandler {
49
+ handle(node: Node, visitor: HTMLNodeVisitor): string;
50
+ }
51
+ /**
52
+ * Handles code elements, converting them to markdown code blocks or inline code
53
+ */
54
+ export declare class CodeHandler extends NodeHandler {
55
+ handle(node: Node, visitor: HTMLNodeVisitor): string;
56
+ }
57
+ /**
58
+ * Fallback handler for any HTML elements not handled by other specific handlers
59
+ */
60
+ export declare class GenericHandler extends NodeHandler {
61
+ handle(node: Node, visitor: HTMLNodeVisitor): string;
62
+ }
63
+ /**
64
+ * Handles ordered list elements, converting them to markdown ordered lists
65
+ */
66
+ export declare class OrderedListHandler extends NodeHandler {
67
+ handle(node: Node, visitor: HTMLNodeVisitor): string;
68
+ }
69
+ /**
70
+ * Handles unordered list elements, converting them to markdown unordered lists
71
+ */
72
+ export declare class UnorderedListHandler extends NodeHandler {
73
+ handle(node: Node, visitor: HTMLNodeVisitor): string;
74
+ }
75
+ /**
76
+ * Handles div elements, converting them to markdown paragraphs
77
+ */
78
+ export declare class DivHandler extends NodeHandler {
79
+ handle(node: Node, visitor: HTMLNodeVisitor): string;
80
+ }
81
+ /**
82
+ * Handles br elements, converting them to markdown newlines
83
+ */
84
+ export declare class BrHandler extends NodeHandler {
85
+ handle(node: Node, visitor: HTMLNodeVisitor): string;
86
+ }
87
+ /**
88
+ * Handles table row elements, converting them to markdown table rows
89
+ */
90
+ export declare class TableRowHandler extends NodeHandler {
91
+ handle(node: Node, visitor: HTMLNodeVisitor): string;
92
+ }
93
+ /**
94
+ * Handles table elements, converting them to markdown tables
95
+ */
96
+ export declare class TableHandler extends NodeHandler {
97
+ handle(node: Node, visitor: HTMLNodeVisitor): string;
98
+ }
99
+ /**
100
+ * Handles image elements, converting them to markdown images
101
+ */
102
+ export declare class ImageHandler extends NodeHandler {
103
+ handle(node: Node, visitor: HTMLNodeVisitor): string;
104
+ }