implement syntax: emoji, custom emoji

This commit is contained in:
marihachi 2020-03-01 01:12:32 +09:00
parent 36b2e27a05
commit c76e32070f
3 changed files with 37 additions and 8 deletions

View file

@ -41,4 +41,9 @@ npm run parse
``` ```
## License ## License
This software is released under the [MIT License](LICENSE). This software is released under the [MIT License](LICENSE) unless otherwise noted.
This software includes code of therd paty softwares below:
- twemoji-parser
Copyright (c) 2018 Twitter, Inc.
MIT Licence: https://github.com/twitter/twemoji-parser/blob/master/LICENSE.md

View file

@ -4,6 +4,8 @@
mergeText mergeText
} = require('./parser-utils'); } = require('./parser-utils');
const emojiRegex = require('./twemoji').default;
function applyParser(input, startRule) { function applyParser(input, startRule) {
let parseFunc = peg$parse; let parseFunc = peg$parse;
return parseFunc(input, startRule ? { startRule } : { }); return parseFunc(input, startRule ? { startRule } : { });
@ -14,7 +16,7 @@ rootParser
= ts:(block / inline)* { return mergeText(ts); } = ts:(block / inline)* { return mergeText(ts); }
plainParser plainParser
= ts:(text /*/ emoji*/)* { return mergeText(ts); } = ts:(customEmoji / textOrEmoji)* { return mergeText(ts); }
inlineParser inlineParser
= ts:(inline)* { return mergeText(ts); } = ts:(inline)* { return mergeText(ts); }
@ -43,12 +45,8 @@ inline
// / hashtag // / hashtag
// / url // / url
// / link // / link
// / emoji / customEmoji
/ text / textOrEmoji
text
= c:. { return createTree('text', { text: c }); }
// block: title // block: title
@ -276,6 +274,27 @@ mathInline
} }
// inline: custom emoji
customEmoji
= ":" name:$[a-z0-9_+-]+ ":"
{
return createTree('emoji', { name: name });
}
// inline: text or emoji
textOrEmoji
= c:((a:[\uD800-\uDBFF] b:[\uDC00-\uDFFF] { return a + b; }) / .)
{
if (emojiRegex.test(c)) {
return createTree('emoji', { emoji: c });
}
return createTree('text', { text: c });
}
// Core rules // Core rules
CHAR CHAR

5
src/parser/twemoji.ts Normal file

File diff suppressed because one or more lines are too long