Repository navigation
fix(slack): Convert canvas HTML to Markdown on read #2647
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
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1bdf5d3
fix(slack): Convert canvas HTML to Markdown on read
sentry-junior[bot] 1a211ff
fix(slack): Drop the canvas title heading on read
sentry-junior[bot] aed0bb2
fix(slack): Match escaped canvas titles when dropping the title heading
sentry-junior[bot] 06a6efd
fix(slack): Detect canvas HTML by a leading start tag
sentry-junior[bot] e8020f2
fix(slack): Keep canvas checklist state on read
sentry-junior[bot] 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
63 changes: 63 additions & 0 deletions
63
packages/junior/src/chat/slack/tool-support/canvas/html.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,63 @@ | ||
| import { NodeHtmlMarkdown } from "node-html-markdown"; | ||
|
|
||
| // Slack serves canvas downloads as Quip HTML, with links as `<lnk href>`. | ||
| const canvasHtmlConverter = new NodeHtmlMarkdown( | ||
| { | ||
| bulletMarker: "-", | ||
| codeBlockStyle: "fenced", | ||
| maxConsecutiveNewlines: 2, | ||
| }, | ||
| { | ||
| lnk: ({ node }) => ({ | ||
| prefix: "[", | ||
| postfix: `](${node.getAttribute("href") ?? ""})`, | ||
| }), | ||
| }, | ||
| ); | ||
|
|
||
| // Quip marks a checklist as `data-section-style="7"` and a done item with the | ||
| // `checked` class. Canvas writes take `- [ ]` and `- [x]`. | ||
| const listItemTranslator = canvasHtmlConverter.translators.get("li"); | ||
| canvasHtmlConverter.translators.set("li", (context) => { | ||
| const config = | ||
| typeof listItemTranslator === "function" | ||
| ? listItemTranslator(context) | ||
| : listItemTranslator; | ||
| const section = context.node.closest("[data-section-style]"); | ||
| if (section?.getAttribute("data-section-style") !== "7") { | ||
| return config; | ||
| } | ||
| const checkbox = context.node.classList.contains("checked") ? "[x] " : "[ ] "; | ||
| return { ...config, prefix: `${config?.prefix ?? ""}${checkbox}` }; | ||
| }); | ||
|
|
||
| // A leading HTML start tag, not a Markdown autolink such as `<https://...>`. | ||
| const leadingHtmlTag = /^\s*<[a-z][a-z0-9-]*[\s/>]/i; | ||
|
|
||
| /** | ||
| * Convert a downloaded canvas body to Markdown. Canvas writes take Markdown, | ||
| * so an HTML body passed back unchanged renders as escaped HTML text. | ||
| */ | ||
| export function canvasBodyToMarkdown(body: string, title?: string): string { | ||
| if (!leadingHtmlTag.test(body)) { | ||
| return body; | ||
| } | ||
| const markdown = canvasHtmlConverter.translate(body).trim(); | ||
| // The export starts with the canvas title as an `h1`. Slack keeps that | ||
| // heading across a body replace, so writing it back would show it twice. | ||
| // Convert the title the same way so Markdown escapes match. | ||
| const titleHeading = title | ||
| ? canvasHtmlConverter.translate(`<h1>${escapeHtml(title)}</h1>`).trim() | ||
| : undefined; | ||
| if (titleHeading && markdown.split("\n", 1)[0] === titleHeading) { | ||
| return markdown.slice(titleHeading.length).trimStart(); | ||
| } | ||
| return markdown; | ||
|
sentry-junior[bot] marked this conversation as resolved.
Resolved
|
||
| } | ||
|
|
||
| function escapeHtml(text: string): string { | ||
| return text | ||
| .replaceAll("&", "&") | ||
| .replaceAll("<", "<") | ||
| .replaceAll(">", ">"); | ||
| } | ||
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
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.