-
Notifications
You must be signed in to change notification settings - Fork 126
feat: integrate markdown parsing into SuperDoc #796
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
caio-pizzol
merged 13 commits into
develop
from
feature/har-10088-superdoc-markdown-support
Aug 22, 2025
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
7efaeb5
feat: integrate markdown parsing into SuperDoc editor and update file…
caiopizzol 41b5311
refactor: simplify markdown conversion and enhance SuperDoc compatibi…
caiopizzol 86c4d74
fix: automatically insert blank docx if SuperEditor file is not docx …
harbournick 9d710b1
refactor: move markdown conversion logic to a dedicated helper module
caiopizzol f380f8e
refactor: update HTML and Markdown document creation logic with dedic…
caiopizzol 9c2894a
feat: add heading translation support for Word export in schema
caiopizzol e112a44
test: add unit and integration tests for markdown and heading export …
caiopizzol 8ec6781
Merge branch 'develop' into feature/har-10088-superdoc-markdown-support
caio-pizzol e6ddd14
refactor: clean up Editor class by removing unused CSS styles
caiopizzol d1823ff
refactor: streamline SuperEditor component structure and remove debug…
caio-pizzol fd6f655
refactor: consolidate document import helpers into a single module
caio-pizzol 8832dee
test: add integration tests for markdown to HTML conversion and docum…
caio-pizzol fef0c8b
refactor: enhance HTML and Markdown import helpers with TypeScript ch…
caio-pizzol File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| // @ts-check | ||
| import { DOMParser } from 'prosemirror-model'; | ||
|
|
||
| /** | ||
| * Create a document from HTML content | ||
| * @private | ||
| * @param {string} content - HTML content | ||
| * @returns {Object} Document node | ||
| */ | ||
| export function createDocFromHTML(content, schema) { | ||
| let parsedContent; | ||
| if (typeof content === 'string') { | ||
| const tempDiv = document.createElement('div'); | ||
| tempDiv.innerHTML = content; | ||
| parsedContent = tempDiv; | ||
| } else { | ||
| parsedContent = content; | ||
| } | ||
|
|
||
| return DOMParser.fromSchema(schema).parse(parsedContent); | ||
| } | ||
|
caio-pizzol marked this conversation as resolved.
|
||
32 changes: 32 additions & 0 deletions
32
packages/super-editor/src/core/helpers/importMarkdown.integration.test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import { describe, it, expect } from 'vitest'; | ||
| import { convertMarkdownToHTML } from './importMarkdown.js'; | ||
|
|
||
| describe('markdown to DOCX integration', () => { | ||
| it('converts complete markdown document with headings and lists', () => { | ||
| const markdown = `# Main Title | ||
|
|
||
| Text before list. | ||
|
|
||
| - Bullet item | ||
| - Another item | ||
|
|
||
| ## Section 2 | ||
|
|
||
| More text here. | ||
|
|
||
| 1. Numbered item | ||
| 2. Second item`; | ||
|
|
||
| const html = convertMarkdownToHTML(markdown); | ||
|
|
||
| // Verify all elements are converted | ||
| expect(html).toContain('<h1>Main Title</h1>'); | ||
| expect(html).toContain('<h2>Section 2</h2>'); | ||
| expect(html).toContain('<ul>'); | ||
| expect(html).toContain('<ol>'); | ||
|
|
||
| // Verify spacing is added between paragraphs and lists | ||
| expect(html).toContain('</p>\n<p> </p>\n<ul>'); | ||
| expect(html).toContain('</p>\n<p> </p>\n<ol>'); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| // @ts-check | ||
| import { marked } from 'marked'; | ||
| import { createDocFromHTML } from './importHtml.js'; | ||
|
|
||
| // Configure marked once | ||
| marked.use({ | ||
| breaks: false, // Use proper paragraphs, not <br> tags | ||
| gfm: true, // GitHub Flavored Markdown support | ||
| }); | ||
|
|
||
| /** | ||
| * Create a ProseMirror document from Markdown content | ||
| * @param {string} markdown - Markdown content | ||
| * @param {Object} schema - ProseMirror schema | ||
| * @returns {Object} Document node | ||
| */ | ||
| export function createDocFromMarkdown(markdown, schema) { | ||
| const html = convertMarkdownToHTML(markdown); | ||
| return createDocFromHTML(html, schema); | ||
| } | ||
|
|
||
| /** | ||
| * Convert Markdown to HTML with SuperDoc/DOCX compatibility | ||
| * @param {string} markdown - Markdown content | ||
| * @returns {string} HTML content | ||
| */ | ||
| export function convertMarkdownToHTML(markdown) { | ||
| let html = marked.parse(markdown, { async: false }); | ||
|
|
||
| // Add spacing between paragraphs and lists for proper DOCX rendering | ||
| return html | ||
| .replace(/<\/p>\n<ul>/g, '</p>\n<p> </p>\n<ul>') | ||
| .replace(/<\/p>\n<ol>/g, '</p>\n<p> </p>\n<ol>') | ||
| .replace(/<\/ul>\n<h/g, '</ul>\n<p> </p>\n<h') | ||
| .replace(/<\/ol>\n<h/g, '</ol>\n<p> </p>\n<h'); | ||
| } |
34 changes: 34 additions & 0 deletions
34
packages/super-editor/src/core/helpers/importMarkdown.test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import { describe, it, expect, vi } from 'vitest'; | ||
| import { createDocFromMarkdown, convertMarkdownToHTML } from './importMarkdown.js'; | ||
| import { createDocFromHTML } from './importHtml.js'; | ||
|
|
||
| vi.mock('../../core/helpers/importHtml.js', () => ({ | ||
| createDocFromHTML: vi.fn(), | ||
| })); | ||
|
|
||
| describe('markdown import', () => { | ||
| it('converts markdown to HTML with proper spacing', () => { | ||
| const markdown = `# Heading | ||
|
|
||
| Paragraph text | ||
|
|
||
| - List item`; | ||
|
|
||
| const html = convertMarkdownToHTML(markdown); | ||
|
|
||
| expect(html).toContain('<h1>Heading</h1>'); | ||
| expect(html).toContain('<p>Paragraph text</p>'); | ||
| expect(html).toContain('</p>\n<p> </p>\n<ul>'); // Spacing before list | ||
| }); | ||
|
|
||
| it('creates ProseMirror doc from markdown', () => { | ||
| const mockSchema = { nodes: {} }; | ||
| const mockDoc = { type: 'doc' }; | ||
| createDocFromHTML.mockReturnValue(mockDoc); | ||
|
|
||
| const result = createDocFromMarkdown('# Test', mockSchema); | ||
|
|
||
| expect(createDocFromHTML).toHaveBeenCalledWith(expect.stringContaining('<h1>Test</h1>'), mockSchema); | ||
| expect(result).toBe(mockDoc); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
packages/super-editor/src/tests/export/headingNodeExporter.test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import { describe, it, expect, vi, beforeEach } from 'vitest'; | ||
| import { exportSchemaToJson } from '../../core/super-converter/exporter.js'; | ||
|
|
||
| describe('heading node export', () => { | ||
| it('converts heading to paragraph with Word heading style', () => { | ||
| const mockParams = { | ||
| node: { | ||
| type: 'heading', | ||
| attrs: { level: 2 }, | ||
| content: [{ type: 'text', text: 'Test' }], | ||
| }, | ||
| editor: { | ||
| extensionService: { extensions: [] }, | ||
| }, | ||
| }; | ||
|
|
||
| const result = exportSchemaToJson(mockParams); | ||
|
|
||
| // Check it's a paragraph with Heading2 style | ||
| expect(result.name).toBe('w:p'); | ||
| const pPr = result.elements.find((el) => el?.name === 'w:pPr'); | ||
| const pStyle = pPr.elements.find((el) => el?.name === 'w:pStyle'); | ||
| expect(pStyle.attributes['w:val']).toBe('Heading2'); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.