-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatch.diff
More file actions
34 lines (33 loc) · 1.22 KB
/
Copy pathpatch.diff
File metadata and controls
34 lines (33 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
--- src/HybridMarkdownEditor.tsx
+++ src/HybridMarkdownEditor.tsx
@@ -70,14 +70,23 @@
const cx = (...classes: Array<string | false | undefined>) =>
classes.filter(Boolean).join(" ");
+const H1_REGEX = /^#{1}\s/;
+const H2_REGEX = /^#{2}\s/;
+const H3_REGEX = /^#{3}\s/;
+const H4_REGEX = /^#{4}\s/;
+const LI_TASK_REGEX = /^\s*[-*]\s\[[ xX]\]\s/;
+const LI_UL_REGEX = /^\s*[-*]\s/;
+const LI_OL_REGEX = /^\s*\d+\.\s/;
+const BLOCKQUOTE_REGEX = /^\s*>\s/;
+
const getMarkdownType = (line: string): LineType => {
- if (/^#{1}\s/.test(line)) return "h1";
- if (/^#{2}\s/.test(line)) return "h2";
- if (/^#{3}\s/.test(line)) return "h3";
- if (/^#{4}\s/.test(line)) return "h4";
- if(/^\s*[-*]\s\[[ xX]\]\s/.test(line)) return "li";
- if(/^\s*[-*]\s/.test(line)) return "li";
- if(/^\s*\d+\.\s/.test(line)) return "li";
- if(/^\s*>\s/.test(line)) return "blockquote";
+ if (H1_REGEX.test(line)) return "h1";
+ if (H2_REGEX.test(line)) return "h2";
+ if (H3_REGEX.test(line)) return "h3";
+ if (H4_REGEX.test(line)) return "h4";
+ if(LI_TASK_REGEX.test(line)) return "li";
+ if(LI_UL_REGEX.test(line)) return "li";
+ if(LI_OL_REGEX.test(line)) return "li";
+ if(BLOCKQUOTE_REGEX.test(line)) return "blockquote";
return "p";
};