@milkdown/plugin-emoji 7.2.2 → 7.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.es.js +6 -6
- package/lib/index.es.js.map +1 -1
- package/package.json +7 -7
- package/src/index.ts +2 -2
package/lib/index.es.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { expectDomTypeError as N } from "@milkdown/exception";
|
|
2
2
|
import { InputRule as v } from "@milkdown/prose/inputrules";
|
|
3
3
|
import { $ctx as R, $nodeAttr as T, $nodeSchema as C, $inputRule as A, $remark as y } from "@milkdown/utils";
|
|
4
|
-
import I from "node-emoji";
|
|
4
|
+
import { get as I } from "node-emoji";
|
|
5
5
|
import L from "remark-emoji";
|
|
6
6
|
import O from "twemoji";
|
|
7
7
|
import P from "emoji-regex";
|
|
@@ -14,10 +14,10 @@ function S(n, e) {
|
|
|
14
14
|
for (let o = 0, c = t.children.length; o < c; o++) {
|
|
15
15
|
const l = t.children[o];
|
|
16
16
|
if (l) {
|
|
17
|
-
const
|
|
18
|
-
if (
|
|
19
|
-
for (let
|
|
20
|
-
const d = j
|
|
17
|
+
const f = m(l, o, t);
|
|
18
|
+
if (f)
|
|
19
|
+
for (let j = 0, M = f.length; j < M; j++) {
|
|
20
|
+
const d = f[j];
|
|
21
21
|
d && s.push(d);
|
|
22
22
|
}
|
|
23
23
|
}
|
|
@@ -109,7 +109,7 @@ const k = A((n) => new v(/(:([^:\s]+):)$/, (e, m, t, i) => {
|
|
|
109
109
|
const r = m[0];
|
|
110
110
|
if (!r)
|
|
111
111
|
return null;
|
|
112
|
-
const s = I
|
|
112
|
+
const s = I(r);
|
|
113
113
|
if (!s || r.includes(s))
|
|
114
114
|
return null;
|
|
115
115
|
const o = w(s, n.get(u.key).twemojiOptions);
|
package/lib/index.es.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.es.js","sources":["../src/__internal__/parse.ts","../src/__internal__/remark-twemoji.ts","../src/__internal__/with-meta.ts","../src/index.ts"],"sourcesContent":["/* Copyright 2021, Milkdown by Mirone. */\nimport twemoji from 'twemoji'\n\nconst setAttr = (text: string) => ({ title: text })\n\nexport const parse = (emoji: string, twemojiOptions?: TwemojiOptions): string =>\n twemoji.parse(emoji, { attributes: setAttr, base: 'https://cdn.jsdelivr.net/gh/twitter/twemoji/assets/', ...twemojiOptions }) as unknown as string\n","/* Copyright 2021, Milkdown by Mirone. */\nimport type { RemarkPlugin } from '@milkdown/transformer'\nimport emojiRegex from 'emoji-regex'\nimport type { Literal, Node, Parent } from 'unist'\n\nimport { parse } from './parse'\n\nconst regex = emojiRegex()\n\nconst isParent = (node: Node): node is Parent => !!(node as Parent).children\nconst isLiteral = (node: Node): node is Literal => !!(node as Literal).value\n\nfunction flatMap(ast: Node, fn: (node: Node, index: number, parent: Node | null) => Node[]) {\n return transform(ast, 0, null)[0]\n\n function transform(node: Node, index: number, parent: Node | null) {\n if (isParent(node)) {\n const out = []\n for (let i = 0, n = node.children.length; i < n; i++) {\n const nthChild = node.children[i]\n if (nthChild) {\n const xs = transform(nthChild, i, node)\n if (xs) {\n for (let j = 0, m = xs.length; j < m; j++) {\n const item = xs[j]\n if (item)\n out.push(item)\n }\n }\n }\n }\n node.children = out\n }\n\n return fn(node, index, parent)\n }\n}\n\nexport const twemojiPlugin: (twemojiOptions?: TwemojiOptions) => RemarkPlugin = twemojiOptions => () => {\n function transformer(tree: Node) {\n flatMap(tree, (node) => {\n if (!isLiteral(node))\n return [node]\n\n const value = node.value as string\n const output: Literal<string>[] = []\n let match\n let str = value\n // eslint-disable-next-line no-cond-assign\n while ((match = regex.exec(str))) {\n const { index } = match\n const emoji = match[0]\n if (emoji) {\n if (index > 0)\n output.push({ ...node, value: str.slice(0, index) })\n\n output.push({ ...node, value: parse(emoji, twemojiOptions), type: 'emoji' })\n str = str.slice(index + emoji.length)\n }\n regex.lastIndex = 0\n }\n if (str.length)\n output.push({ ...node, value: str })\n\n return output\n })\n }\n return transformer\n}\n","/* Copyright 2021, Milkdown by Mirone. */\nimport type { Meta, MilkdownPlugin } from '@milkdown/ctx'\n\nexport const withMeta = <T extends MilkdownPlugin>(\n plugin: T,\n meta: Partial<Meta> & Pick<Meta, 'displayName'>,\n): T => {\n Object.assign(plugin, {\n meta: {\n package: '@milkdown/plugin-emoji',\n ...meta,\n },\n })\n\n return plugin\n}\n","/* Copyright 2021, Milkdown by Mirone. */\nimport type { MilkdownPlugin } from '@milkdown/ctx'\nimport type { RemarkPlugin } from '@milkdown/transformer'\nimport { expectDomTypeError } from '@milkdown/exception'\nimport { InputRule } from '@milkdown/prose/inputrules'\nimport { $ctx, $inputRule, $nodeAttr, $nodeSchema, $remark } from '@milkdown/utils'\nimport nodeEmoji from 'node-emoji'\nimport remarkEmoji from 'remark-emoji'\nimport type Twemoji from 'twemoji'\n\nimport { parse } from './__internal__/parse'\nimport { twemojiPlugin } from './__internal__/remark-twemoji'\nimport { withMeta } from './__internal__/with-meta'\n\ntype TwemojiOptions = Exclude<Parameters<typeof Twemoji.parse>[1], Function | undefined>\n\n/// @internal\nexport interface EmojiConfig {\n twemojiOptions?: TwemojiOptions\n}\n\n/// A slice that contains [options for twemoji](https://github.com/twitter/twemoji#object-as-parameter).\nexport const emojiConfig = $ctx<EmojiConfig, 'emojiConfig'>({}, 'emojiConfig')\nwithMeta(emojiConfig, {\n displayName: 'Ctx<emojiConfig>',\n})\n\n/// HTML attributes for emoji node.\nexport const emojiAttr = $nodeAttr('emoji', () => ({\n span: {},\n img: {},\n}))\nwithMeta(emojiAttr, {\n displayName: 'Attr<emoji>',\n})\n\n/// Schema for emoji node.\nexport const emojiSchema = $nodeSchema('emoji', ctx => ({\n group: 'inline',\n inline: true,\n attrs: {\n html: {\n default: '',\n },\n },\n parseDOM: [\n {\n tag: 'span[data-type=\"emoji\"]',\n getAttrs: (dom) => {\n if (!(dom instanceof HTMLElement))\n throw expectDomTypeError(dom)\n\n return { html: dom.innerHTML }\n },\n },\n ],\n toDOM: (node) => {\n const attrs = ctx.get(emojiAttr.key)(node)\n const tmp = document.createElement('span')\n tmp.innerHTML = node.attrs.html\n const dom = tmp.firstElementChild?.cloneNode()\n tmp.remove()\n if (dom && dom instanceof HTMLElement)\n Object.entries<string>(attrs.img).forEach(([key, value]) => dom.setAttribute(key, value))\n\n return ['span', { ...attrs.container, 'data-type': 'emoji' }, dom]\n },\n parseMarkdown: {\n match: ({ type }) => type === 'emoji',\n runner: (state, node, type) => {\n state.addNode(type, { html: node.value as string })\n },\n },\n toMarkdown: {\n match: node => node.type.name === 'emoji',\n runner: (state, node) => {\n const span = document.createElement('span')\n span.innerHTML = node.attrs.html\n const img = span.querySelector('img')\n const title = img?.title || img?.alt\n span.remove()\n state.addNode('text', undefined, title)\n },\n },\n}))\n\nwithMeta(emojiSchema.node, {\n displayName: 'NodeSchema<emoji>',\n})\nwithMeta(emojiSchema.ctx, {\n displayName: 'NodeSchemaCtx<emoji>',\n})\n\n/// Input rule for inserting emoji.\n/// For example, `:smile:` will be replaced with `😄`.\nexport const insertEmojiInputRule = $inputRule(ctx => new InputRule(/(:([^:\\s]+):)$/, (state, match, start, end) => {\n const content = match[0]\n if (!content)\n return null\n const got = nodeEmoji.get(content)\n if (!got || content.includes(got))\n return null\n\n const html = parse(got, ctx.get(emojiConfig.key).twemojiOptions)\n\n return state.tr\n .setMeta('emoji', true)\n .replaceRangeWith(start, end, emojiSchema.type().create({ html }))\n .scrollIntoView()\n}))\n\nwithMeta(insertEmojiInputRule, {\n displayName: 'InputRule<insertEmojiInputRule>',\n})\n\n/// This plugin wraps [remark-emoji](https://github.com/rhysd/remark-emoji).\nexport const remarkEmojiPlugin = $remark(() => remarkEmoji as RemarkPlugin)\n\nwithMeta(remarkEmojiPlugin, {\n displayName: 'Remark<remarkEmojiPlugin>',\n})\n\n/// This plugin is used for transforming emoji to twemoji.\nexport const remarkTwemojiPlugin = $remark(ctx => twemojiPlugin(ctx.get(emojiConfig.key).twemojiOptions))\n\nwithMeta(remarkTwemojiPlugin, {\n displayName: 'Remark<remarkTwemojiPlugin>',\n})\n\n/// All plugins exported by this package.\nexport const emoji: MilkdownPlugin[] = [\n emojiAttr,\n emojiConfig,\n remarkEmojiPlugin,\n remarkTwemojiPlugin,\n emojiSchema,\n insertEmojiInputRule,\n].flat()\n"],"names":["setAttr","text","parse","emoji","twemojiOptions","twemoji","regex","emojiRegex","isParent","node","isLiteral","flatMap","ast","fn","transform","index","parent","out","i","n","nthChild","xs","j","m","item","twemojiPlugin","transformer","tree","value","output","match","str","withMeta","plugin","meta","emojiConfig","$ctx","emojiAttr","$nodeAttr","emojiSchema","$nodeSchema","ctx","dom","expectDomTypeError","attrs","tmp","_a","key","type","state","span","img","title","insertEmojiInputRule","$inputRule","InputRule","start","end","content","got","nodeEmoji","html","remarkEmojiPlugin","$remark","remarkEmoji","remarkTwemojiPlugin"],"mappings":";;;;;;;AAGA,MAAMA,IAAU,CAACC,OAAkB,EAAE,OAAOA,EAAK,IAEpCC,IAAQ,CAACC,GAAeC,MACnCC,EAAQ,MAAMF,GAAO,EAAE,YAAYH,GAAS,MAAM,uDAAuD,GAAGI,GAAgB,GCCxHE,IAAQC,EAAW,GAEnBC,IAAW,CAACC,MAA+B,CAAC,CAAEA,EAAgB,UAC9DC,IAAY,CAACD,MAAgC,CAAC,CAAEA,EAAiB;AAEvE,SAASE,EAAQC,GAAWC,GAAgE;AAC1F,SAAOC,EAAUF,GAAK,GAAG,IAAI,EAAE,CAAC;AAEvB,WAAAE,EAAUL,GAAYM,GAAeC,GAAqB;AAC7D,QAAAR,EAASC,CAAI,GAAG;AAClB,YAAMQ,IAAM,CAAA;AACH,eAAAC,IAAI,GAAGC,IAAIV,EAAK,SAAS,QAAQS,IAAIC,GAAGD,KAAK;AAC9C,cAAAE,IAAWX,EAAK,SAASS,CAAC;AAChC,YAAIE,GAAU;AACZ,gBAAMC,IAAKP,EAAUM,GAAUF,GAAGT,CAAI;AACtC,cAAIY;A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,EACrCkC;AAAA,EACAF;AAAA,EACA2B;AAAA,EACAG;AAAA,EACA1B;AAAA,EACAc;AACF,EAAE,KAAK;"}
|
|
1
|
+
{"version":3,"file":"index.es.js","sources":["../src/__internal__/parse.ts","../src/__internal__/remark-twemoji.ts","../src/__internal__/with-meta.ts","../src/index.ts"],"sourcesContent":["/* Copyright 2021, Milkdown by Mirone. */\nimport twemoji from 'twemoji'\n\nconst setAttr = (text: string) => ({ title: text })\n\nexport const parse = (emoji: string, twemojiOptions?: TwemojiOptions): string =>\n twemoji.parse(emoji, { attributes: setAttr, base: 'https://cdn.jsdelivr.net/gh/twitter/twemoji/assets/', ...twemojiOptions }) as unknown as string\n","/* Copyright 2021, Milkdown by Mirone. */\nimport type { RemarkPlugin } from '@milkdown/transformer'\nimport emojiRegex from 'emoji-regex'\nimport type { Literal, Node, Parent } from 'unist'\n\nimport { parse } from './parse'\n\nconst regex = emojiRegex()\n\nconst isParent = (node: Node): node is Parent => !!(node as Parent).children\nconst isLiteral = (node: Node): node is Literal => !!(node as Literal).value\n\nfunction flatMap(ast: Node, fn: (node: Node, index: number, parent: Node | null) => Node[]) {\n return transform(ast, 0, null)[0]\n\n function transform(node: Node, index: number, parent: Node | null) {\n if (isParent(node)) {\n const out = []\n for (let i = 0, n = node.children.length; i < n; i++) {\n const nthChild = node.children[i]\n if (nthChild) {\n const xs = transform(nthChild, i, node)\n if (xs) {\n for (let j = 0, m = xs.length; j < m; j++) {\n const item = xs[j]\n if (item)\n out.push(item)\n }\n }\n }\n }\n node.children = out\n }\n\n return fn(node, index, parent)\n }\n}\n\nexport const twemojiPlugin: (twemojiOptions?: TwemojiOptions) => RemarkPlugin = twemojiOptions => () => {\n function transformer(tree: Node) {\n flatMap(tree, (node) => {\n if (!isLiteral(node))\n return [node]\n\n const value = node.value as string\n const output: Literal<string>[] = []\n let match\n let str = value\n // eslint-disable-next-line no-cond-assign\n while ((match = regex.exec(str))) {\n const { index } = match\n const emoji = match[0]\n if (emoji) {\n if (index > 0)\n output.push({ ...node, value: str.slice(0, index) })\n\n output.push({ ...node, value: parse(emoji, twemojiOptions), type: 'emoji' })\n str = str.slice(index + emoji.length)\n }\n regex.lastIndex = 0\n }\n if (str.length)\n output.push({ ...node, value: str })\n\n return output\n })\n }\n return transformer\n}\n","/* Copyright 2021, Milkdown by Mirone. */\nimport type { Meta, MilkdownPlugin } from '@milkdown/ctx'\n\nexport const withMeta = <T extends MilkdownPlugin>(\n plugin: T,\n meta: Partial<Meta> & Pick<Meta, 'displayName'>,\n): T => {\n Object.assign(plugin, {\n meta: {\n package: '@milkdown/plugin-emoji',\n ...meta,\n },\n })\n\n return plugin\n}\n","/* Copyright 2021, Milkdown by Mirone. */\nimport type { MilkdownPlugin } from '@milkdown/ctx'\nimport type { RemarkPlugin } from '@milkdown/transformer'\nimport { expectDomTypeError } from '@milkdown/exception'\nimport { InputRule } from '@milkdown/prose/inputrules'\nimport { $ctx, $inputRule, $nodeAttr, $nodeSchema, $remark } from '@milkdown/utils'\nimport { get } from 'node-emoji'\nimport remarkEmoji from 'remark-emoji'\nimport type Twemoji from 'twemoji'\n\nimport { parse } from './__internal__/parse'\nimport { twemojiPlugin } from './__internal__/remark-twemoji'\nimport { withMeta } from './__internal__/with-meta'\n\ntype TwemojiOptions = Exclude<Parameters<typeof Twemoji.parse>[1], Function | undefined>\n\n/// @internal\nexport interface EmojiConfig {\n twemojiOptions?: TwemojiOptions\n}\n\n/// A slice that contains [options for twemoji](https://github.com/twitter/twemoji#object-as-parameter).\nexport const emojiConfig = $ctx<EmojiConfig, 'emojiConfig'>({}, 'emojiConfig')\nwithMeta(emojiConfig, {\n displayName: 'Ctx<emojiConfig>',\n})\n\n/// HTML attributes for emoji node.\nexport const emojiAttr = $nodeAttr('emoji', () => ({\n span: {},\n img: {},\n}))\nwithMeta(emojiAttr, {\n displayName: 'Attr<emoji>',\n})\n\n/// Schema for emoji node.\nexport const emojiSchema = $nodeSchema('emoji', ctx => ({\n group: 'inline',\n inline: true,\n attrs: {\n html: {\n default: '',\n },\n },\n parseDOM: [\n {\n tag: 'span[data-type=\"emoji\"]',\n getAttrs: (dom) => {\n if (!(dom instanceof HTMLElement))\n throw expectDomTypeError(dom)\n\n return { html: dom.innerHTML }\n },\n },\n ],\n toDOM: (node) => {\n const attrs = ctx.get(emojiAttr.key)(node)\n const tmp = document.createElement('span')\n tmp.innerHTML = node.attrs.html\n const dom = tmp.firstElementChild?.cloneNode()\n tmp.remove()\n if (dom && dom instanceof HTMLElement)\n Object.entries<string>(attrs.img).forEach(([key, value]) => dom.setAttribute(key, value))\n\n return ['span', { ...attrs.container, 'data-type': 'emoji' }, dom]\n },\n parseMarkdown: {\n match: ({ type }) => type === 'emoji',\n runner: (state, node, type) => {\n state.addNode(type, { html: node.value as string })\n },\n },\n toMarkdown: {\n match: node => node.type.name === 'emoji',\n runner: (state, node) => {\n const span = document.createElement('span')\n span.innerHTML = node.attrs.html\n const img = span.querySelector('img')\n const title = img?.title || img?.alt\n span.remove()\n state.addNode('text', undefined, title)\n },\n },\n}))\n\nwithMeta(emojiSchema.node, {\n displayName: 'NodeSchema<emoji>',\n})\nwithMeta(emojiSchema.ctx, {\n displayName: 'NodeSchemaCtx<emoji>',\n})\n\n/// Input rule for inserting emoji.\n/// For example, `:smile:` will be replaced with `😄`.\nexport const insertEmojiInputRule = $inputRule(ctx => new InputRule(/(:([^:\\s]+):)$/, (state, match, start, end) => {\n const content = match[0]\n if (!content)\n return null\n const got = get(content)\n if (!got || content.includes(got))\n return null\n\n const html = parse(got, ctx.get(emojiConfig.key).twemojiOptions)\n\n return state.tr\n .setMeta('emoji', true)\n .replaceRangeWith(start, end, emojiSchema.type().create({ html }))\n .scrollIntoView()\n}))\n\nwithMeta(insertEmojiInputRule, {\n displayName: 'InputRule<insertEmojiInputRule>',\n})\n\n/// This plugin wraps [remark-emoji](https://github.com/rhysd/remark-emoji).\nexport const remarkEmojiPlugin = $remark(() => remarkEmoji as RemarkPlugin)\n\nwithMeta(remarkEmojiPlugin, {\n displayName: 'Remark<remarkEmojiPlugin>',\n})\n\n/// This plugin is used for transforming emoji to twemoji.\nexport const remarkTwemojiPlugin = $remark(ctx => twemojiPlugin(ctx.get(emojiConfig.key).twemojiOptions))\n\nwithMeta(remarkTwemojiPlugin, {\n displayName: 'Remark<remarkTwemojiPlugin>',\n})\n\n/// All plugins exported by this package.\nexport const emoji: MilkdownPlugin[] = [\n emojiAttr,\n emojiConfig,\n remarkEmojiPlugin,\n remarkTwemojiPlugin,\n emojiSchema,\n insertEmojiInputRule,\n].flat()\n"],"names":["setAttr","text","parse","emoji","twemojiOptions","twemoji","regex","emojiRegex","isParent","node","isLiteral","flatMap","ast","fn","transform","index","parent","out","i","n","nthChild","xs","m","item","twemojiPlugin","transformer","tree","value","output","match","str","withMeta","plugin","meta","emojiConfig","$ctx","emojiAttr","$nodeAttr","emojiSchema","$nodeSchema","ctx","dom","expectDomTypeError","attrs","tmp","_a","key","type","state","span","img","title","insertEmojiInputRule","$inputRule","InputRule","start","end","content","got","get","html","remarkEmojiPlugin","$remark","remarkEmoji","remarkTwemojiPlugin"],"mappings":";;;;;;;AAGA,MAAMA,IAAU,CAACC,OAAkB,EAAE,OAAOA,EAAK,IAEpCC,IAAQ,CAACC,GAAeC,MACnCC,EAAQ,MAAMF,GAAO,EAAE,YAAYH,GAAS,MAAM,uDAAuD,GAAGI,GAAgB,GCCxHE,IAAQC,EAAW,GAEnBC,IAAW,CAACC,MAA+B,CAAC,CAAEA,EAAgB,UAC9DC,IAAY,CAACD,MAAgC,CAAC,CAAEA,EAAiB;AAEvE,SAASE,EAAQC,GAAWC,GAAgE;AAC1F,SAAOC,EAAUF,GAAK,GAAG,IAAI,EAAE,CAAC;AAEvB,WAAAE,EAAUL,GAAYM,GAAeC,GAAqB;AAC7D,QAAAR,EAASC,CAAI,GAAG;AAClB,YAAMQ,IAAM,CAAA;AACH,eAAAC,IAAI,GAAGC,IAAIV,EAAK,SAAS,QAAQS,IAAIC,GAAGD,KAAK;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,EAAY,GAAG,EAAE,cAAc,CAAC;AAExGH,EAASiC,GAAqB;AAAA,EAC5B,aAAa;AACf,CAAC;AAGM,MAAM7D,IAA0B;AAAA,EACrCiC;AAAA,EACAF;AAAA,EACA2B;AAAA,EACAG;AAAA,EACA1B;AAAA,EACAc;AACF,EAAE,KAAK;"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@milkdown/plugin-emoji",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "7.2.
|
|
4
|
+
"version": "7.2.3",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
@@ -33,15 +33,15 @@
|
|
|
33
33
|
"tslib": "^2.5.0",
|
|
34
34
|
"twemoji": "^14.0.1",
|
|
35
35
|
"unist-util-visit": "^4.0.0",
|
|
36
|
-
"@milkdown/exception": "7.2.
|
|
37
|
-
"@milkdown/utils": "7.2.
|
|
36
|
+
"@milkdown/exception": "7.2.3",
|
|
37
|
+
"@milkdown/utils": "7.2.3"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
40
|
"@types/unist": "^2.0.6",
|
|
41
|
-
"@milkdown/core": "7.2.
|
|
42
|
-
"@milkdown/ctx": "7.2.
|
|
43
|
-
"@milkdown/prose": "7.2.
|
|
44
|
-
"@milkdown/transformer": "7.2.
|
|
41
|
+
"@milkdown/core": "7.2.3",
|
|
42
|
+
"@milkdown/ctx": "7.2.3",
|
|
43
|
+
"@milkdown/prose": "7.2.3",
|
|
44
|
+
"@milkdown/transformer": "7.2.3"
|
|
45
45
|
},
|
|
46
46
|
"nx": {
|
|
47
47
|
"targets": {
|
package/src/index.ts
CHANGED
|
@@ -4,7 +4,7 @@ import type { RemarkPlugin } from '@milkdown/transformer'
|
|
|
4
4
|
import { expectDomTypeError } from '@milkdown/exception'
|
|
5
5
|
import { InputRule } from '@milkdown/prose/inputrules'
|
|
6
6
|
import { $ctx, $inputRule, $nodeAttr, $nodeSchema, $remark } from '@milkdown/utils'
|
|
7
|
-
import
|
|
7
|
+
import { get } from 'node-emoji'
|
|
8
8
|
import remarkEmoji from 'remark-emoji'
|
|
9
9
|
import type Twemoji from 'twemoji'
|
|
10
10
|
|
|
@@ -97,7 +97,7 @@ export const insertEmojiInputRule = $inputRule(ctx => new InputRule(/(:([^:\s]+)
|
|
|
97
97
|
const content = match[0]
|
|
98
98
|
if (!content)
|
|
99
99
|
return null
|
|
100
|
-
const got =
|
|
100
|
+
const got = get(content)
|
|
101
101
|
if (!got || content.includes(got))
|
|
102
102
|
return null
|
|
103
103
|
|