Repository navigation
Expand file tree
/
Copy pathmarkdown.ts
More file actions
146 lines (134 loc) · 4.37 KB
/
Copy pathmarkdown.ts
File metadata and controls
146 lines (134 loc) · 4.37 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Selection-aware markdown transforms: each tool returns the new value plus the
// selection range to restore, so the textarea can re-select the right text.
export interface MdSelection {
/** Full text after the edit. */
text: string;
/** Selection start to restore. */
start: number;
/** Selection end to restore. */
end: number;
}
interface Slice {
before: string;
selected: string;
after: string;
}
const slice = (text: string, start: number, end: number): Slice => ({
before: text.slice(0, start),
selected: text.slice(start, end),
after: text.slice(end),
});
/** Wrap the selection in `token` on each side; placeholder if empty. */
function wrap(
text: string,
start: number,
end: number,
token: string,
placeholder: string,
): MdSelection {
const { before, selected, after } = slice(text, start, end);
const inner = selected || placeholder;
return {
text: `${before}${token}${inner}${token}${after}`,
start: before.length + token.length,
end: before.length + token.length + inner.length,
};
}
/** Prefix every line of the selection (or the current line) with `prefix`. */
function prefixLines(
text: string,
start: number,
end: number,
prefix: (i: number) => string,
placeholder: string,
): MdSelection {
const { before, selected, after } = slice(text, start, end);
const body = selected || placeholder;
const out = body
.split("\n")
.map((line, i) => `${prefix(i)}${line}`)
.join("\n");
return {
text: `${before}${out}${after}`,
start: before.length,
end: before.length + out.length,
};
}
export const md = {
bold: (t: string, s: number, e: number) => wrap(t, s, e, "**", "bold text"),
italic: (t: string, s: number, e: number) =>
wrap(t, s, e, "*", "italic text"),
strike: (t: string, s: number, e: number) =>
wrap(t, s, e, "~~", "struck text"),
/** Strip common inline markdown tokens from the selection. */
clear: (t: string, s: number, e: number): MdSelection => {
const { before, selected, after } = slice(t, s, e);
const cleaned = selected
.replace(/\*\*([^*]+)\*\*/g, "$1")
.replace(/~~([^~]+)~~/g, "$1")
.replace(/`([^`]+)`/g, "$1")
.replace(/\*([^*]+)\*/g, "$1")
.replace(/_([^_]+)_/g, "$1")
.replace(/^\s*>\s?/gm, "")
.replace(/^\s*[-*]\s+/gm, "")
.replace(/^\s*\d+\.\s+/gm, "");
return {
text: `${before}${cleaned}${after}`,
start: before.length,
end: before.length + cleaned.length,
};
},
/** `[selection](url)` — selects the `url` placeholder for quick typing. */
link: (t: string, s: number, e: number): MdSelection => {
const { before, selected, after } = slice(t, s, e);
const label = selected || "link text";
const urlPh = "url";
const head = `${before}[${label}](`;
return {
text: `${head}${urlPh})${after}`,
start: head.length,
end: head.length + urlPh.length,
};
},
ul: (t: string, s: number, e: number) =>
prefixLines(t, s, e, () => "- ", "list item"),
ol: (t: string, s: number, e: number) =>
prefixLines(t, s, e, (i) => `${i + 1}. `, "list item"),
quote: (t: string, s: number, e: number) =>
prefixLines(t, s, e, () => "> ", "quote"),
/** Inline code for single-line selections; fenced block otherwise. */
code: (t: string, s: number, e: number): MdSelection => {
const { before, selected, after } = slice(t, s, e);
const inner = selected || "code";
if (!inner.includes("\n")) {
const head = `${before}\``;
return {
text: `${head}${inner}\`${after}`,
start: head.length,
end: head.length + inner.length,
};
}
const head = `${before}\`\`\`\n`;
return {
text: `${head}${inner}\n\`\`\`${after}`,
start: head.length,
end: head.length + inner.length,
};
},
/** 3-row markdown table template; selects the first header cell. */
table: (t: string, s: number, e: number): MdSelection => {
const { before, after } = slice(t, s, e);
const lead = before && !before.endsWith("\n") ? "\n" : "";
const head = `${before}${lead}| `;
const cell = "Column 1";
const tail =
` | Column 2 | Column 3 |\n| --- | --- | --- |\n` +
`| Cell | Cell | Cell |\n| Cell | Cell | Cell |`;
return {
text: `${head}${cell}${tail}${after}`,
start: head.length,
end: head.length + cell.length,
};
},
};
export type MdTool = keyof typeof md;