Skip to content

Commit 2f114c3

Browse files
committed
Bugfix: marktext#152 emoji error
1 parent 3006f07 commit 2f114c3

3 files changed

Lines changed: 20 additions & 14 deletions

File tree

src/editor/index.css

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -396,8 +396,8 @@ span.ag-image-marked-text, span.ag-link-in-bracket, span.ag-link-in-bracket .ag-
396396
text-decoration: none;
397397
color: rgb(51, 51, 51);
398398
}
399-
.ag-warn {
400-
color: #000;
399+
span.ag-warn.ag-emoji-marked-text {
400+
color: rgba(242, 134, 94, .7);
401401
text-decoration: none;
402402
}
403403

src/editor/parser/parse.js

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { beginRules, inlineRules } from './rules'
22
import { isLengthEven, union } from '../utils'
33
import { punctuation } from '../config'
4+
// import { validEmoji } from '../emojis'
45

56
const CAN_NEST_RULES = ['strong', 'em', 'link', 'del', 'image', 'a_link'] // image can not nest but it has children
67

@@ -21,6 +22,20 @@ const getSrcAlt = text => {
2122
return { src, alt }
2223
}
2324

25+
const lowerPriority = (src, offset) => {
26+
let i
27+
for (i = 0; i < offset; i++) {
28+
const text = src.substring(i)
29+
for (const rule of Object.keys(validateRules)) {
30+
const to = validateRules[rule].exec(text)
31+
if (to && to[0].length > offset - i) {
32+
return false
33+
}
34+
}
35+
}
36+
return true
37+
}
38+
2439
const validateEmphasize = (src, offset, marker, pending) => {
2540
/**
2641
* Intraword emphasis is disallowed for _
@@ -62,17 +77,7 @@ const validateEmphasize = (src, offset, marker, pending) => {
6277
* and one that does not, the former always wins. Thus, for example, *[foo*](bar) is parsed
6378
* as *<a href="bar">foo*</a> rather than as <em>[foo</em>](bar).
6479
*/
65-
let i
66-
for (i = 0; i < offset; i++) {
67-
const text = src.substring(i)
68-
for (const rule of Object.keys(validateRules)) {
69-
const to = validateRules[rule].exec(text)
70-
if (to && to[0].length > offset - i) {
71-
return false
72-
}
73-
}
74-
}
75-
return true
80+
return lowerPriority(src, offset)
7681
}
7782

7883
const tokenizerFac = (src, beginRules, inlineRules, pos = 0, top) => {
@@ -180,6 +185,7 @@ const tokenizerFac = (src, beginRules, inlineRules, pos = 0, top) => {
180185
for (const rule of chunks) {
181186
const to = inlineRules[rule].exec(src)
182187
if (to && isLengthEven(to[3])) {
188+
if (rule === 'emoji' && !lowerPriority(src, to[0].length)) break
183189
inChunk = true
184190
pushPending()
185191
const range = {

src/editor/parser/rules.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export const inlineRules = {
1313
'inline_code': /^(`{1,3})([^`]+?|.{2,})\1/,
1414
'image': /^(\!\[)(.*?)(\\*)\]\((.*?)(\\*)\)/,
1515
'link': /^(\[)((?:\[[^\]]*\]|[^\[\]]|\](?=[^\[]*\]))*?)(\\*)\]\((.*?)(\\*)\)/, // can nest
16-
'emoji': /^(:)([^:\\]+?)\1/,
16+
'emoji': /^(:)([a-z_]+?)\1/,
1717
'del': /^(~{2})(?=\S)([\s\S]*?\S)(\\*)\1/, // can nest
1818
'auto_link': /^(https?:\/\/[^\s]+)(?=\s|$)/,
1919
'inline_math': /^(\$)([^\$]*?[^\$\\])(\\*)\1(?!\1)/,

0 commit comments

Comments
 (0)