Skip to content

add markdown link input rules for Rich editor #390

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 2 commits into from
Jul 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion ui/src/components/nodes/Rich.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import {
DropCursorExtension,
ImageExtension,
ItalicExtension,
LinkExtension,
LinkExtension as RemirrorLinkExtension,
PlaceholderExtension,
ShortcutHandlerProps,
SubExtension,
Expand Down Expand Up @@ -95,6 +95,10 @@ import {
ToggleStrikeButton,
} from "@remirror/react";
import { FloatingToolbar, useExtensionEvent } from "@remirror/react";

import { InputRule } from "@remirror/pm";
import { markInputRule } from "@remirror/core-utils";

import { TableExtension } from "@remirror/extension-react-tables";
import { GenIcon, IconBase } from "@remirror/react-components";
import "remirror/styles/all.css";
Expand All @@ -108,6 +112,26 @@ import {
} from "./extensions/mathExtension";
import { NewPodButtons, level2fontsize } from "./utils";

class LinkExtension extends RemirrorLinkExtension {
createInputRules(): InputRule[] {
return [
markInputRule({
regexp: /\[([^\]]+)\]\(([^)]+)\)/,
type: this.type,
getAttributes: (matches: string[]) => {
const [_, text, href] = matches;
return { text: text, href: href };
},
}),
markInputRule({
regexp: /(?:==|__)([^*_]+)(?:==|__)$/,
type: this.type,
ignoreWhitespace: true,
}),
];
}
}

function useLinkShortcut() {
const [linkShortcut, setLinkShortcut] = useState<
ShortcutHandlerProps | undefined
Expand Down