From deeae7afa552a285d0890f126f06648cf2b10efb Mon Sep 17 00:00:00 2001 From: Jared Pereira Date: Mon, 12 Aug 2024 15:37:56 -0400 Subject: [PATCH] add markdown shortcuts for highlight and strikethrough --- components/Blocks/TextBlock/inputRules.ts | 33 +++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/components/Blocks/TextBlock/inputRules.ts b/components/Blocks/TextBlock/inputRules.ts index fe470735..8e63c89c 100644 --- a/components/Blocks/TextBlock/inputRules.ts +++ b/components/Blocks/TextBlock/inputRules.ts @@ -8,12 +8,45 @@ import { Replicache } from "replicache"; import { ReplicacheMutators } from "src/replicache"; import { BlockProps, focusBlock } from "components/Blocks"; import { schema } from "./schema"; +import { useUIState } from "src/useUIState"; export const inputrules = ( propsRef: MutableRefObject, repRef: MutableRefObject | null>, ) => inputRules({ rules: [ + new InputRule(/\~\~([^*]+)\~\~$/, (state, match, start, end) => { + const [fullMatch, content] = match; + const { tr } = state; + if (content) { + tr.replaceWith(start, end, state.schema.text(content)) + .addMark( + start, + start + content.length, + schema.marks.strikethrough.create(), + ) + .removeStoredMark(schema.marks.strikethrough); + return tr; + } + return null; + }), + new InputRule(/\=\=([^*]+)\=\=$/, (state, match, start, end) => { + const [fullMatch, content] = match; + const { tr } = state; + if (content) { + tr.replaceWith(start, end, state.schema.text(content)) + .addMark( + start, + start + content.length, + schema.marks.highlight.create({ + color: useUIState.getState().lastUsedHighlight || "1", + }), + ) + .removeStoredMark(schema.marks.highlight); + return tr; + } + return null; + }), new InputRule(/\*\*([^*]+)\*\*$/, (state, match, start, end) => { const [fullMatch, content] = match; const { tr } = state; -- knotmirror 0.0.1