-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): autoconvert html to md when pasting in markdown mode (#476)
- Loading branch information
Showing
36 changed files
with
1,481 additions
and
431 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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 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 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 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 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 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
42 changes: 42 additions & 0 deletions
42
src/markup/codemirror/html-to-markdown/__tests__/converter.test.ts
This file contains 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,42 @@ | ||
import fs from 'node:fs'; | ||
import path from 'node:path'; | ||
|
||
import {JSDOM} from 'jsdom'; | ||
|
||
import {MarkdownConverter} from '../converters'; | ||
|
||
describe('HTML to Markdown Converter', () => { | ||
const fixturesPath = path.join(__dirname, './fixtures'); | ||
const testCases = fs.readdirSync(fixturesPath); | ||
let converter: MarkdownConverter; | ||
|
||
beforeEach(() => { | ||
converter = new MarkdownConverter(); | ||
}); | ||
|
||
testCases.forEach((testCase) => { | ||
it(`should convert ${testCase} correctly`, () => { | ||
const inputPath = path.join(fixturesPath, testCase, 'input.html'); | ||
const outputPath = path.join(fixturesPath, testCase, 'output.md'); | ||
|
||
const inputHtml = fs.readFileSync(inputPath, 'utf-8'); | ||
const expectedOutput = fs.readFileSync(outputPath, 'utf-8').trim(); | ||
|
||
// Create a proper HTML document | ||
const dom = new JSDOM(` | ||
<!DOCTYPE html> | ||
<html> | ||
<body> | ||
${inputHtml} | ||
</body> | ||
</html> | ||
`); | ||
|
||
// Process the content inside body | ||
const result = converter.processNode(dom.window.document.body).trim(); | ||
|
||
// Compare the result with expected output | ||
expect(result).toBe(expectedOutput); | ||
}); | ||
}); | ||
}); |
2 changes: 2 additions & 0 deletions
2
src/markup/codemirror/html-to-markdown/__tests__/fixtures/basic-text/input.html
This file contains 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,2 @@ | ||
<p>This is a simple paragraph.</p> | ||
<p>This is another paragraph with some text.</p> |
3 changes: 3 additions & 0 deletions
3
src/markup/codemirror/html-to-markdown/__tests__/fixtures/basic-text/output.md
This file contains 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,3 @@ | ||
This is a simple paragraph. | ||
|
||
This is another paragraph with some text. |
24 changes: 24 additions & 0 deletions
24
src/markup/codemirror/html-to-markdown/__tests__/fixtures/code/input.html
This file contains 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,24 @@ | ||
<p>Inline code with escape <code>test`</code></p> | ||
|
||
<pre data-language="plaintext"><code class="hljs plaintext">import React from 'react'; | ||
import { useMarkdownEditor, MarkdownEditorView } from '@gravity-ui/markdown-editor'; | ||
import { toaster } from '@gravity-ui/uikit/toaster-singleton-react-18'; | ||
|
||
function Editor({ onSubmit }) { | ||
const editor = useMarkdownEditor({ allowHTML: false }); | ||
|
||
React.useEffect(() => { | ||
function submitHandler() { | ||
// Serialize current content to markdown markup | ||
const value = editor.getValue(); | ||
onSubmit(value); | ||
} | ||
|
||
editor.on('submit', submitHandler); | ||
return () => { | ||
editor.off('submit', submitHandler); | ||
}; | ||
}, [onSubmit]); | ||
|
||
return <MarkdownEditorView stickyToolbar autofocus toaster={toaster} editor={editor} />; | ||
}</code></pre> |
26 changes: 26 additions & 0 deletions
26
src/markup/codemirror/html-to-markdown/__tests__/fixtures/code/output.md
This file contains 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,26 @@ | ||
Inline code with escape `` test` `` | ||
|
||
``` | ||
import React from 'react'; | ||
import { useMarkdownEditor, MarkdownEditorView } from '@gravity-ui/markdown-editor'; | ||
import { toaster } from '@gravity-ui/uikit/toaster-singleton-react-18'; | ||
function Editor({ onSubmit }) { | ||
const editor = useMarkdownEditor({ allowHTML: false }); | ||
React.useEffect(() => { | ||
function submitHandler() { | ||
// Serialize current content to markdown markup | ||
const value = editor.getValue(); | ||
onSubmit(value); | ||
} | ||
editor.on('submit', submitHandler); | ||
return () => { | ||
editor.off('submit', submitHandler); | ||
}; | ||
}, [onSubmit]); | ||
return <MarkdownEditorView stickyToolbar autofocus toaster={toaster} editor={editor} />; | ||
} | ||
``` |
3 changes: 3 additions & 0 deletions
3
src/markup/codemirror/html-to-markdown/__tests__/fixtures/formatting/input.html
This file contains 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,3 @@ | ||
<p>This text has <strong>bold</strong> and <em>italic</em> formatting.</p> | ||
<p>This text uses <b>bold</b> and <i>italic</i> tags.</p> | ||
<p>This text uses <span style="font-weight: 600;">CSS bold</span> and <span style="font-style: italic;">CSS italic</span>.</p> |
5 changes: 5 additions & 0 deletions
5
src/markup/codemirror/html-to-markdown/__tests__/fixtures/formatting/output.md
This file contains 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,5 @@ | ||
This text has **bold** and *italic* formatting. | ||
|
||
This text uses **bold** and *italic* tags. | ||
|
||
This text uses **CSS bold** and *CSS italic*. |
1 change: 1 addition & 0 deletions
1
src/markup/codemirror/html-to-markdown/__tests__/fixtures/header-and-link/input.html
This file contains 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 @@ | ||
<h1>Main Title</h1><a href="https://example.com">Link</a> |
2 changes: 2 additions & 0 deletions
2
...markup/codemirror/html-to-markdown/__tests__/fixtures/header-and-link/output.md
This file contains 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,2 @@ | ||
# Main Title | ||
[Link](https://example.com/ "Link") |
6 changes: 6 additions & 0 deletions
6
src/markup/codemirror/html-to-markdown/__tests__/fixtures/headers/input.html
This file contains 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,6 @@ | ||
<h1>Main Title</h1> | ||
<p>Some content.</p> | ||
<h2>Subtitle</h2> | ||
<p>More content.</p> | ||
<h3>Section 3</h3> | ||
<h4>Section 4</h4> |
8 changes: 8 additions & 0 deletions
8
src/markup/codemirror/html-to-markdown/__tests__/fixtures/headers/output.md
This file contains 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,8 @@ | ||
# Main Title | ||
Some content. | ||
|
||
## Subtitle | ||
More content. | ||
|
||
### Section 3 | ||
#### Section 4 |
1 change: 1 addition & 0 deletions
1
src/markup/codemirror/html-to-markdown/__tests__/fixtures/image/input.html
This file contains 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 @@ | ||
<img src="https://github.com/user-attachments/assets/e2cdda03-fa1d-48fd-91e7-88d935f8bb9b" alt="nature"> |
1 change: 1 addition & 0 deletions
1
src/markup/codemirror/html-to-markdown/__tests__/fixtures/image/output.md
This file contains 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 @@ | ||
![nature](https://github.com/user-attachments/assets/e2cdda03-fa1d-48fd-91e7-88d935f8bb9b) |
3 changes: 3 additions & 0 deletions
3
src/markup/codemirror/html-to-markdown/__tests__/fixtures/links-with-formatting/input.html
This file contains 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,3 @@ | ||
<p>A link inside the <span style="font-style: italic;"><a href="https://example.com">span</a></span>.</p> | ||
|
||
<a href="https://en.wikipedia.org/wiki/Price_markdown" title="Price markdown" style="text-decoration: underline; color: var(--color-progressive,#36c); background: none rgb(255, 255, 255); border-radius: 2px; outline-color: var(--outline-color-progressive--focus,#36c); overflow-wrap: break-word; font-family: sans-serif; font-size: 16px; font-style: italic; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal;">Price markdown</a> |
3 changes: 3 additions & 0 deletions
3
.../codemirror/html-to-markdown/__tests__/fixtures/links-with-formatting/output.md
This file contains 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,3 @@ | ||
A link inside the *[span](https://example.com/ "span")*. | ||
|
||
[*Price markdown*](https://en.wikipedia.org/wiki/Price_markdown "Price markdown") |
2 changes: 2 additions & 0 deletions
2
src/markup/codemirror/html-to-markdown/__tests__/fixtures/links/input.html
This file contains 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,2 @@ | ||
<p>Here's a <a href="https://example.com">simple link</a>.</p> | ||
<p>This link has <a href="https://example.com"><b>bold text</b></a>.</p> |
3 changes: 3 additions & 0 deletions
3
src/markup/codemirror/html-to-markdown/__tests__/fixtures/links/output.md
This file contains 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,3 @@ | ||
Here's a [simple link](https://example.com/ "simple link"). | ||
|
||
This link has [**bold text**](https://example.com/ "bold text"). |
10 changes: 10 additions & 0 deletions
10
src/markup/codemirror/html-to-markdown/__tests__/fixtures/lists/input.html
This file contains 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,10 @@ | ||
<ol> | ||
<li>First item</li> | ||
<li>Second item</li> | ||
<li>Third item</li> | ||
</ol> | ||
<ul> | ||
<li>Item A</li> | ||
<li>Item B</li> | ||
<li>Item C</li> | ||
</ul> |
6 changes: 6 additions & 0 deletions
6
src/markup/codemirror/html-to-markdown/__tests__/fixtures/lists/output.md
This file contains 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,6 @@ | ||
1. First item | ||
2. Second item | ||
3. Third item | ||
- Item A | ||
- Item B | ||
- Item C |
Oops, something went wrong.