-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.ts
More file actions
51 lines (46 loc) · 1.47 KB
/
index.ts
File metadata and controls
51 lines (46 loc) · 1.47 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
interface MarkdownOptions {
value: string;
tags: Array<string>;
}
const textEditOptionValues: Array<MarkdownOptions> = [
{ value: "*", tags: ["<b>", "</b>"] },
{ value: "_", tags: ["<i>", "</i>"] },
{ value: "~", tags: ["<s>", "</s>"] },
{ value: "```", tags: ["<code>", "</code>"] },
];
const handleCodeMarkdown = (
message: string,
splitMessage: Array<string>,
option: MarkdownOptions
) => {
let position = 0;
let splitCodeIndices: number[] = [];
while ((position = message!.indexOf("```", position)) !== -1) {
splitCodeIndices.push(position);
position += 3;
}
splitCodeIndices.forEach((originalIndex, parsedItemIndex) => {
const tag = parsedItemIndex % 2 === 0 ? option.tags[0] : option.tags[1];
splitMessage[originalIndex] = tag;
splitMessage[originalIndex + 1] = "";
splitMessage[originalIndex + 2] = "";
});
};
export const handleGenericMarkdown = (message: string) => {
if (!message) return;
const splitMessage = message!.split("");
textEditOptionValues.forEach((option) => {
const matchIndices: number[] = [];
splitMessage.forEach((letter, index) => {
if (letter === option.value) {
matchIndices.push(index);
}
});
matchIndices.forEach((originalIndex, parsedItemIndex) => {
const tag = parsedItemIndex % 2 === 0 ? option.tags[0] : option.tags[1];
splitMessage[originalIndex] = tag;
});
handleCodeMarkdown(message, splitMessage, option);
});
return splitMessage.join("");
};