-
Notifications
You must be signed in to change notification settings - Fork 152
feat: add GitHub-style alert blockquotes #174
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
Open
dirlligafu
wants to merge
3
commits into
erictli:main
Choose a base branch
from
dirlligafu:feat/github-alert-blockquotes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| import { Node, mergeAttributes, InputRule, type JSONContent, type MarkdownToken } from "@tiptap/core"; | ||
|
|
||
| export type AlertType = "NOTE" | "TIP" | "IMPORTANT" | "WARNING" | "CAUTION"; | ||
|
|
||
| const ALERT_TYPES: AlertType[] = ["NOTE", "TIP", "IMPORTANT", "WARNING", "CAUTION"]; | ||
|
|
||
|
|
||
| export const AlertBlockquote = Node.create({ | ||
| name: "alertBlockquote", | ||
| group: "block", | ||
| content: "block+", | ||
| defining: true, | ||
|
|
||
| addAttributes() { | ||
| return { | ||
| alertType: { | ||
| default: "NOTE", | ||
| parseHTML: (el) => (el.getAttribute("data-alert-type") as AlertType) || "NOTE", | ||
| renderHTML: (attrs) => ({ "data-alert-type": attrs.alertType }), | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
| }, | ||
| }; | ||
| }, | ||
|
|
||
| parseHTML() { | ||
| return [{ tag: "blockquote[data-alert-type]" }]; | ||
| }, | ||
|
|
||
| renderHTML({ node, HTMLAttributes }) { | ||
| const type = ((node.attrs.alertType as string) || "NOTE").toLowerCase(); | ||
| return [ | ||
| "blockquote", | ||
| mergeAttributes(HTMLAttributes, { class: `alert alert-${type}` }), | ||
| 0, | ||
| ]; | ||
| }, | ||
|
|
||
| addInputRules() { | ||
| const nodeType = this.type; | ||
| return ALERT_TYPES.map((alertType) => | ||
| new InputRule({ | ||
| find: new RegExp(`^\\[!${alertType}\\]\\s$`, "i"), | ||
| handler: ({ state, range, commands }) => { | ||
| const { $from } = state.selection; | ||
| let bqPos = -1; | ||
| for (let d = $from.depth; d >= 1; d--) { | ||
| if ($from.node(d).type.name === "blockquote") { | ||
| bqPos = $from.before(d); | ||
| break; | ||
| } | ||
| } | ||
| if (bqPos === -1) return; | ||
|
|
||
| commands.command(({ tr }) => { | ||
| tr.delete(range.from, range.to); | ||
| const bq = tr.doc.nodeAt(bqPos); | ||
| if (!bq) return true; | ||
| const alertNode = nodeType.create({ alertType }, bq.content); | ||
| tr.replaceWith(bqPos, bqPos + bq.nodeSize, alertNode); | ||
| return true; | ||
| }); | ||
| }, | ||
| }), | ||
| ); | ||
| }, | ||
|
|
||
| markdownTokenName: "alertBlockquote", | ||
|
|
||
| markdownTokenizer: { | ||
| name: "alertBlockquote", | ||
| level: "block" as const, | ||
| start: "> [!", | ||
| tokenize(src: string, _tokens: MarkdownToken[]) { | ||
| const match = src.match( | ||
| /^> \[!(NOTE|TIP|IMPORTANT|WARNING|CAUTION)\][ \t]*\r?\n?((?:>[ \t]?[^\n]*\r?\n?)*)/i, | ||
| ); | ||
| if (!match) return undefined; | ||
| const text = (match[2] || "").replace(/^>[ \t]?/gm, "").replace(/\s+$/, ""); | ||
| return { | ||
| type: "alertBlockquote", | ||
| raw: match[0], | ||
| alertType: match[1].toUpperCase(), | ||
| text, | ||
| }; | ||
| }, | ||
| }, | ||
|
|
||
| parseMarkdown(token: MarkdownToken, helpers) { | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| const t = token as any; | ||
| const alertType: string = t.alertType || "NOTE"; | ||
| const text: string = t.text || ""; | ||
| // Split on blank lines → multiple paragraphs; soft newlines → space | ||
| const blocks = text.split(/\n\n+/).map((p) => p.replace(/\n/g, " ").trim()).filter(Boolean); | ||
| const paraNodes = blocks.length > 0 | ||
| ? blocks.map((p) => helpers.createNode("paragraph", {}, [helpers.createTextNode(p)])) | ||
| : [helpers.createNode("paragraph", {}, [])]; | ||
| return helpers.createNode("alertBlockquote", { alertType }, paraNodes); | ||
| }, | ||
|
|
||
| renderMarkdown(node: JSONContent, helpers) { | ||
| const alertType = (node.attrs?.alertType as string) || "NOTE"; | ||
| const raw = node.content ? helpers.renderChildren(node.content) : ""; | ||
| const inner = raw.replace(/\s+$/, ""); | ||
| const lines = inner.split("\n").map((l) => (l ? `> ${l}` : ">")); | ||
| return `> [!${alertType}]\n${lines.join("\n")}`; | ||
| }, | ||
| }); | ||
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
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.