-
Notifications
You must be signed in to change notification settings - Fork 33
feat(code): paste rich text as Markdown in the composer #2472
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
+135
−4
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
39 changes: 39 additions & 0 deletions
39
apps/code/src/renderer/features/message-editor/utils/htmlToMarkdown.test.ts
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,39 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { htmlToMarkdown } from "./htmlToMarkdown"; | ||
|
|
||
| describe("htmlToMarkdown", () => { | ||
| it.each([ | ||
| [ | ||
| "headings, emphasis and links", | ||
| "<h1>Title</h1><p>Some <strong>bold</strong> and <em>italic</em> with a <a href='https://posthog.com'>link</a>.</p>", | ||
| "# Title\n\nSome **bold** and *italic* with a [link](https://posthog.com).", | ||
| ], | ||
| [ | ||
| "unordered lists", | ||
| "<ul><li>one</li><li>two</li></ul>", | ||
| "- one\n- two", | ||
| ], | ||
| [ | ||
| "tables via the gfm plugin", | ||
| "<table><thead><tr><th>a</th><th>b</th></tr></thead><tbody><tr><td>1</td><td>2</td></tr></tbody></table>", | ||
| "| a | b |\n| --- | --- |\n| 1 | 2 |", | ||
| ], | ||
| [ | ||
| "fenced code blocks", | ||
| "<pre><code>const x = 1;</code></pre>", | ||
| "```\nconst x = 1;\n```", | ||
| ], | ||
| ])("converts %s", (_, html, expected) => { | ||
| expect(htmlToMarkdown(html)).toBe(expected); | ||
| }); | ||
|
|
||
| it("returns null when there is no formatting beyond the plain-text fallback", () => { | ||
| const html = "<p>just text</p>"; | ||
| expect(htmlToMarkdown(html, "just text")).toBeNull(); | ||
| }); | ||
|
|
||
| it("returns null for empty html", () => { | ||
| expect(htmlToMarkdown("")).toBeNull(); | ||
| expect(htmlToMarkdown("<p></p>")).toBeNull(); | ||
| }); | ||
| }); | ||
35 changes: 35 additions & 0 deletions
35
apps/code/src/renderer/features/message-editor/utils/htmlToMarkdown.ts
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,35 @@ | ||
| import { gfm } from "@joplin/turndown-plugin-gfm"; | ||
| import TurndownService from "turndown"; | ||
|
|
||
| let turndown: TurndownService | null = null; | ||
|
|
||
| function getTurndown(): TurndownService { | ||
| if (turndown) return turndown; | ||
| turndown = new TurndownService({ | ||
| headingStyle: "atx", | ||
| codeBlockStyle: "fenced", | ||
| bulletListMarker: "-", | ||
| emDelimiter: "*", | ||
| }); | ||
| turndown.use(gfm); // tables, strikethrough, task lists | ||
| return turndown; | ||
| } | ||
|
|
||
| /** Convert clipboard HTML to Markdown. Returns null when it adds nothing over the plain-text fallback. */ | ||
| export function htmlToMarkdown( | ||
| html: string, | ||
| plainTextFallback?: string, | ||
| ): string | null { | ||
| const markdown = getTurndown().turndown(html).trim(); | ||
| if (!markdown) return null; | ||
|
|
||
| // No formatting beyond the plain text; defer to the default paste. | ||
| if ( | ||
| plainTextFallback !== undefined && | ||
| markdown === plainTextFallback.trim() | ||
| ) { | ||
| return null; | ||
| } | ||
|
|
||
| return markdown; | ||
| } |
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,9 @@ | ||
| declare module "@joplin/turndown-plugin-gfm" { | ||
| import type { Plugin } from "turndown"; | ||
|
|
||
| export const gfm: Plugin; | ||
| export const tables: Plugin; | ||
| export const strikethrough: Plugin; | ||
| export const taskListItems: Plugin; | ||
| export const highlightedCodeBlock: Plugin; | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.