Skip to content

Commit 8f2be87

Browse files
committed
(UX) add bold/italic/strike/underline shortcuts
1 parent aa2676d commit 8f2be87

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

src/utils/formatString.js

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,28 @@ export default (text, doLinkify = false) => {
1717
})
1818
}
1919
} else {
20-
const firstChar = string.slice(0, 1)
21-
const lastChar = string.slice(-1)
20+
if (string.length > 2) {
21+
const formats = []
2222

23-
if (string.length > 2 && firstChar === '*' && lastChar === '*') {
24-
result = { bind: true, content: `<b>${string.slice(1, -1)}</b>` }
23+
string = testString(string, '*', 'b', formats)
24+
string = testString(string, '_', 'i', formats)
25+
26+
string = testString(string, '*', 'b', formats)
27+
string = testString(string, '~', 'del', formats)
28+
29+
string = testString(string, '*', 'b', formats)
30+
string = testString(string, '_', 'i', formats)
31+
string = testString(string, '|', 'ins', formats)
32+
33+
string = testString(string, '*', 'b', formats)
34+
string = testString(string, '_', 'i', formats)
35+
string = testString(string, '~', 'del', formats)
36+
37+
formats.map(format => {
38+
string = `<${format}>${string}</${format}>`
39+
})
40+
41+
result = { bind: formats.length, content: string }
2542
} else {
2643
result = { content: string }
2744
}
@@ -35,3 +52,12 @@ export default (text, doLinkify = false) => {
3552

3653
return formattedStrings
3754
}
55+
56+
function testString(string, sign, type, formats) {
57+
if (string.slice(0, 1) === sign && string.slice(-1) === sign) {
58+
string = string.slice(1, -1)
59+
formats.push(type)
60+
}
61+
62+
return string
63+
}

0 commit comments

Comments
 (0)