@milkdown/plugin-emoji 7.2.4 → 7.3.1
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/__internal__/remark-twemoji.d.ts +2 -2
- package/lib/__internal__/remark-twemoji.d.ts.map +1 -1
- package/lib/index.d.ts +3 -9
- package/lib/index.d.ts.map +1 -1
- package/lib/index.es.js +90 -90
- package/lib/index.es.js.map +1 -1
- package/package.json +10 -11
- package/src/__internal__/remark-twemoji.ts +5 -6
- package/src/index.ts +26 -32
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
export declare const twemojiPlugin:
|
|
1
|
+
import type { RemarkPluginRaw } from '@milkdown/transformer';
|
|
2
|
+
export declare const twemojiPlugin: RemarkPluginRaw<TwemojiOptions>;
|
|
3
3
|
//# sourceMappingURL=remark-twemoji.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"remark-twemoji.d.ts","sourceRoot":"","sources":["../../src/__internal__/remark-twemoji.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"remark-twemoji.d.ts","sourceRoot":"","sources":["../../src/__internal__/remark-twemoji.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAQ,eAAe,EAAE,MAAM,uBAAuB,CAAA;AAoClE,eAAO,MAAM,aAAa,EAAE,eAAe,CAAC,cAAc,CA8BzD,CAAA"}
|
package/lib/index.d.ts
CHANGED
|
@@ -1,15 +1,9 @@
|
|
|
1
1
|
import type { MilkdownPlugin } from '@milkdown/ctx';
|
|
2
|
-
import type
|
|
3
|
-
type TwemojiOptions = Exclude<Parameters<typeof Twemoji.parse>[1], Function | undefined>;
|
|
4
|
-
export interface EmojiConfig {
|
|
5
|
-
twemojiOptions?: TwemojiOptions;
|
|
6
|
-
}
|
|
7
|
-
export declare const emojiConfig: import("@milkdown/utils").$Ctx<EmojiConfig, "emojiConfig">;
|
|
2
|
+
import type { RemarkEmojiOptions } from 'remark-emoji';
|
|
8
3
|
export declare const emojiAttr: import("@milkdown/utils").$NodeAttr;
|
|
9
4
|
export declare const emojiSchema: import("@milkdown/utils").$NodeSchema<"emoji">;
|
|
5
|
+
export declare const remarkEmojiPlugin: import("@milkdown/utils").$Remark<"remarkEmoji", RemarkEmojiOptions>;
|
|
6
|
+
export declare const remarkTwemojiPlugin: import("@milkdown/utils").$Remark<"remarkTwemoji", TwemojiOptions>;
|
|
10
7
|
export declare const insertEmojiInputRule: import("@milkdown/utils").$InputRule;
|
|
11
|
-
export declare const remarkEmojiPlugin: import("@milkdown/utils").$Remark;
|
|
12
|
-
export declare const remarkTwemojiPlugin: import("@milkdown/utils").$Remark;
|
|
13
8
|
export declare const emoji: MilkdownPlugin[];
|
|
14
|
-
export {};
|
|
15
9
|
//# sourceMappingURL=index.d.ts.map
|
package/lib/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,eAAe,CAAA;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,eAAe,CAAA;AAMnD,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAA;AAQtD,eAAO,MAAM,SAAS,qCAGnB,CAAA;AAMH,eAAO,MAAM,WAAW,gDA+CrB,CAAA;AAUH,eAAO,MAAM,iBAAiB,sEAAmF,CAAA;AAWjH,eAAO,MAAM,mBAAmB,oEAAgD,CAAA;AAYhF,eAAO,MAAM,oBAAoB,sCAc9B,CAAA;AAOH,eAAO,MAAM,KAAK,EAAE,cAAc,EAM1B,CAAA"}
|
package/lib/index.es.js
CHANGED
|
@@ -1,64 +1,60 @@
|
|
|
1
|
-
import { expectDomTypeError as
|
|
1
|
+
import { expectDomTypeError as M } from "@milkdown/exception";
|
|
2
2
|
import { InputRule as v } from "@milkdown/prose/inputrules";
|
|
3
|
-
import { $
|
|
4
|
-
import { get as
|
|
5
|
-
import
|
|
6
|
-
import
|
|
7
|
-
import
|
|
8
|
-
const
|
|
9
|
-
function S(n,
|
|
3
|
+
import { $nodeAttr as R, $nodeSchema as x, $remark as y, $inputRule as T } from "@milkdown/utils";
|
|
4
|
+
import { get as P } from "node-emoji";
|
|
5
|
+
import A from "remark-emoji";
|
|
6
|
+
import I from "twemoji";
|
|
7
|
+
import L from "emoji-regex";
|
|
8
|
+
const b = (n) => ({ title: n }), w = (n, t) => I.parse(n, { attributes: b, base: "https://cdn.jsdelivr.net/gh/twitter/twemoji/assets/", ...t }), k = L(), C = (n) => !!n.children, H = (n) => !!n.value;
|
|
9
|
+
function S(n, t) {
|
|
10
10
|
return m(n, 0, null)[0];
|
|
11
|
-
function m(
|
|
12
|
-
if (
|
|
13
|
-
const
|
|
14
|
-
for (let o = 0, c =
|
|
15
|
-
const l =
|
|
11
|
+
function m(e, r, i) {
|
|
12
|
+
if (C(e)) {
|
|
13
|
+
const a = [];
|
|
14
|
+
for (let o = 0, c = e.children.length; o < c; o++) {
|
|
15
|
+
const l = e.children[o];
|
|
16
16
|
if (l) {
|
|
17
|
-
const f = m(l, o,
|
|
17
|
+
const f = m(l, o, e);
|
|
18
18
|
if (f)
|
|
19
|
-
for (let
|
|
20
|
-
const
|
|
21
|
-
|
|
19
|
+
for (let h = 0, N = f.length; h < N; h++) {
|
|
20
|
+
const g = f[h];
|
|
21
|
+
g && a.push(g);
|
|
22
22
|
}
|
|
23
23
|
}
|
|
24
24
|
}
|
|
25
|
-
|
|
25
|
+
e.children = a;
|
|
26
26
|
}
|
|
27
|
-
return e
|
|
27
|
+
return t(e, r, i);
|
|
28
28
|
}
|
|
29
29
|
}
|
|
30
|
-
const
|
|
31
|
-
function
|
|
32
|
-
S(m, (
|
|
33
|
-
if (!H(
|
|
34
|
-
return [
|
|
35
|
-
const
|
|
36
|
-
let
|
|
37
|
-
for (;
|
|
38
|
-
const { index: c } =
|
|
39
|
-
l && (c > 0 &&
|
|
30
|
+
const $ = (n) => {
|
|
31
|
+
function t(m) {
|
|
32
|
+
S(m, (e) => {
|
|
33
|
+
if (!H(e))
|
|
34
|
+
return [e];
|
|
35
|
+
const r = e.value, i = [];
|
|
36
|
+
let a, o = r;
|
|
37
|
+
for (; a = k.exec(o); ) {
|
|
38
|
+
const { index: c } = a, l = a[0];
|
|
39
|
+
l && (c > 0 && i.push({ ...e, value: o.slice(0, c) }), i.push({ ...e, value: w(l, n), type: "emoji" }), o = o.slice(c + l.length)), k.lastIndex = 0;
|
|
40
40
|
}
|
|
41
|
-
return o.length &&
|
|
41
|
+
return o.length && i.push({ ...e, value: o }), i;
|
|
42
42
|
});
|
|
43
43
|
}
|
|
44
|
-
return
|
|
45
|
-
},
|
|
44
|
+
return t;
|
|
45
|
+
}, s = (n, t) => (Object.assign(n, {
|
|
46
46
|
meta: {
|
|
47
47
|
package: "@milkdown/plugin-emoji",
|
|
48
|
-
...
|
|
48
|
+
...t
|
|
49
49
|
}
|
|
50
|
-
}), n),
|
|
51
|
-
a(u, {
|
|
52
|
-
displayName: "Ctx<emojiConfig>"
|
|
53
|
-
});
|
|
54
|
-
const h = T("emoji", () => ({
|
|
50
|
+
}), n), j = R("emoji", () => ({
|
|
55
51
|
span: {},
|
|
56
52
|
img: {}
|
|
57
53
|
}));
|
|
58
|
-
|
|
54
|
+
s(j, {
|
|
59
55
|
displayName: "Attr<emoji>"
|
|
60
56
|
});
|
|
61
|
-
const
|
|
57
|
+
const u = x("emoji", (n) => ({
|
|
62
58
|
group: "inline",
|
|
63
59
|
inline: !0,
|
|
64
60
|
attrs: {
|
|
@@ -69,78 +65,82 @@ const p = C("emoji", (n) => ({
|
|
|
69
65
|
parseDOM: [
|
|
70
66
|
{
|
|
71
67
|
tag: 'span[data-type="emoji"]',
|
|
72
|
-
getAttrs: (
|
|
73
|
-
if (!(
|
|
74
|
-
throw
|
|
75
|
-
return { html:
|
|
68
|
+
getAttrs: (t) => {
|
|
69
|
+
if (!(t instanceof HTMLElement))
|
|
70
|
+
throw M(t);
|
|
71
|
+
return { html: t.innerHTML };
|
|
76
72
|
}
|
|
77
73
|
}
|
|
78
74
|
],
|
|
79
|
-
toDOM: (
|
|
80
|
-
var
|
|
81
|
-
const m = n.get(
|
|
82
|
-
|
|
83
|
-
const
|
|
84
|
-
return
|
|
75
|
+
toDOM: (t) => {
|
|
76
|
+
var i;
|
|
77
|
+
const m = n.get(j.key)(t), e = document.createElement("span");
|
|
78
|
+
e.innerHTML = t.attrs.html;
|
|
79
|
+
const r = (i = e.firstElementChild) == null ? void 0 : i.cloneNode();
|
|
80
|
+
return e.remove(), r && r instanceof HTMLElement && Object.entries(m.img).forEach(([a, o]) => r.setAttribute(a, o)), ["span", { ...m.container, "data-type": "emoji" }, r];
|
|
85
81
|
},
|
|
86
82
|
parseMarkdown: {
|
|
87
|
-
match: ({ type:
|
|
88
|
-
runner: (
|
|
89
|
-
|
|
83
|
+
match: ({ type: t }) => t === "emoji",
|
|
84
|
+
runner: (t, m, e) => {
|
|
85
|
+
t.addNode(e, { html: m.value });
|
|
90
86
|
}
|
|
91
87
|
},
|
|
92
88
|
toMarkdown: {
|
|
93
|
-
match: (
|
|
94
|
-
runner: (
|
|
95
|
-
const
|
|
96
|
-
|
|
97
|
-
const
|
|
98
|
-
|
|
89
|
+
match: (t) => t.type.name === "emoji",
|
|
90
|
+
runner: (t, m) => {
|
|
91
|
+
const e = document.createElement("span");
|
|
92
|
+
e.innerHTML = m.attrs.html;
|
|
93
|
+
const r = e.querySelector("img"), i = (r == null ? void 0 : r.title) || (r == null ? void 0 : r.alt);
|
|
94
|
+
e.remove(), t.addNode("text", void 0, i);
|
|
99
95
|
}
|
|
100
96
|
}
|
|
101
97
|
}));
|
|
102
|
-
|
|
98
|
+
s(u.node, {
|
|
103
99
|
displayName: "NodeSchema<emoji>"
|
|
104
100
|
});
|
|
105
|
-
|
|
101
|
+
s(u.ctx, {
|
|
106
102
|
displayName: "NodeSchemaCtx<emoji>"
|
|
107
103
|
});
|
|
108
|
-
const
|
|
109
|
-
|
|
110
|
-
|
|
104
|
+
const d = y("remarkEmoji", () => A);
|
|
105
|
+
s(d.plugin, {
|
|
106
|
+
displayName: "Remark<remarkEmojiPlugin>"
|
|
107
|
+
});
|
|
108
|
+
s(d.options, {
|
|
109
|
+
displayName: "RemarkConfig<remarkEmojiPlugin>"
|
|
110
|
+
});
|
|
111
|
+
const p = y("remarkTwemoji", () => $);
|
|
112
|
+
s(p.plugin, {
|
|
113
|
+
displayName: "Remark<remarkTwemojiPlugin>"
|
|
114
|
+
});
|
|
115
|
+
s(p.options, {
|
|
116
|
+
displayName: "RemarkConfig<remarkTwemojiPlugin>"
|
|
117
|
+
});
|
|
118
|
+
const E = T((n) => new v(/(:([^:\s]+):)$/, (t, m, e, r) => {
|
|
119
|
+
const i = m[0];
|
|
120
|
+
if (!i)
|
|
111
121
|
return null;
|
|
112
|
-
const
|
|
113
|
-
if (!
|
|
122
|
+
const a = P(i);
|
|
123
|
+
if (!a || i.includes(a))
|
|
114
124
|
return null;
|
|
115
|
-
const o = w(
|
|
116
|
-
return
|
|
125
|
+
const o = w(a, n.get(p.options.key));
|
|
126
|
+
return t.tr.setMeta("emoji", !0).replaceRangeWith(e, r, u.type(n).create({ html: o })).scrollIntoView();
|
|
117
127
|
}));
|
|
118
|
-
|
|
128
|
+
s(E, {
|
|
119
129
|
displayName: "InputRule<insertEmojiInputRule>"
|
|
120
130
|
});
|
|
121
|
-
const
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
});
|
|
125
|
-
const E = y((n) => D(n.get(u.key).twemojiOptions));
|
|
126
|
-
a(E, {
|
|
127
|
-
displayName: "Remark<remarkTwemojiPlugin>"
|
|
128
|
-
});
|
|
129
|
-
const J = [
|
|
130
|
-
h,
|
|
131
|
-
u,
|
|
132
|
-
x,
|
|
133
|
-
E,
|
|
131
|
+
const F = [
|
|
132
|
+
j,
|
|
133
|
+
d,
|
|
134
134
|
p,
|
|
135
|
-
|
|
135
|
+
u,
|
|
136
|
+
E
|
|
136
137
|
].flat();
|
|
137
138
|
export {
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
u as
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
E as remarkTwemojiPlugin
|
|
139
|
+
F as emoji,
|
|
140
|
+
j as emojiAttr,
|
|
141
|
+
u as emojiSchema,
|
|
142
|
+
E as insertEmojiInputRule,
|
|
143
|
+
d as remarkEmojiPlugin,
|
|
144
|
+
p as remarkTwemojiPlugin
|
|
145
145
|
};
|
|
146
146
|
//# sourceMappingURL=index.es.js.map
|
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 { 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(ctx).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,Y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AAMwD,GAAKlB,EAAI,IAAIN,EAAY,GAAG,EAAE,cAAc;AAE/D,SAAOc,EAAM,GACV,QAAQ,SAAS,EAAI,EACrB,iBAAiBO,GAAOC,GAAKlB,EAAY,KAAKE,CAAG,EAAE,OAAO,EAAE,MAAAoB,GAAM,CAAC,EACnE;AACL,CAAC,CAAC;AAEF7B,EAASqB,GAAsB;AAAA,EAC7B,aAAa;AACf,CAAC;AAGY,MAAAS,IAAoBC,EAAQ,MAAMC,CAA2B;AAE1EhC,EAAS8B,GAAmB;AAAA,EAC1B,aAAa;AACf,CAAC;AAGY,MAAAG,IAAsBF,EAAQ,CAAAtB,MAAOhB,EAAcgB,EAAI,IAAIN,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;"}
|
|
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 { Node, RemarkPluginRaw } from '@milkdown/transformer'\nimport emojiRegex from 'emoji-regex'\n\nimport { parse } from './parse'\n\nconst regex = emojiRegex()\n\nconst isParent = (node: Node): node is Node & { children: Node[] } => !!(node as Node & { children: Node[] }).children\nconst isLiteral = (node: Node): node is Node & { value: string } => !!(node as Node & { value: string }).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: RemarkPluginRaw<TwemojiOptions> = (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: Array<Node & { value: 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 { RemarkPluginRaw } from '@milkdown/transformer'\nimport { expectDomTypeError } from '@milkdown/exception'\nimport { InputRule } from '@milkdown/prose/inputrules'\nimport { $inputRule, $nodeAttr, $nodeSchema, $remark } from '@milkdown/utils'\nimport { get } from 'node-emoji'\nimport type { RemarkEmojiOptions } from 'remark-emoji'\nimport remarkEmoji from 'remark-emoji'\n\nimport { parse } from './__internal__/parse'\nimport { twemojiPlugin } from './__internal__/remark-twemoji'\nimport { withMeta } from './__internal__/with-meta'\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/// This plugin wraps [remark-emoji](https://github.com/rhysd/remark-emoji).\nexport const remarkEmojiPlugin = $remark('remarkEmoji', () => remarkEmoji as RemarkPluginRaw<RemarkEmojiOptions>)\n\nwithMeta(remarkEmojiPlugin.plugin, {\n displayName: 'Remark<remarkEmojiPlugin>',\n})\n\nwithMeta(remarkEmojiPlugin.options, {\n displayName: 'RemarkConfig<remarkEmojiPlugin>',\n})\n\n/// This plugin is used for transforming emoji to twemoji.\nexport const remarkTwemojiPlugin = $remark('remarkTwemoji', () => twemojiPlugin)\n\nwithMeta(remarkTwemojiPlugin.plugin, {\n displayName: 'Remark<remarkTwemojiPlugin>',\n})\n\nwithMeta(remarkTwemojiPlugin.options, {\n displayName: 'RemarkConfig<remarkTwemojiPlugin>',\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(remarkTwemojiPlugin.options.key))\n\n return state.tr\n .setMeta('emoji', true)\n .replaceRangeWith(start, end, emojiSchema.type(ctx).create({ html }))\n .scrollIntoView()\n}))\n\nwithMeta(insertEmojiInputRule, {\n displayName: 'InputRule<insertEmojiInputRule>',\n})\n\n/// All plugins exported by this package.\nexport const emoji: MilkdownPlugin[] = [\n emojiAttr,\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","emojiAttr","$nodeAttr","emojiSchema","$nodeSchema","ctx","dom","expectDomTypeError","attrs","tmp","_a","key","type","state","span","img","title","remarkEmojiPlugin","$remark","remarkEmoji","remarkTwemojiPlugin","insertEmojiInputRule","$inputRule","InputRule","start","end","content","got","get","html"],"mappings":";;;;;;;AAGA,MAAMA,IAAU,CAACC,OAAkB,EAAE,OAAOA,EAAK,IAEpCC,IAAQ,CAACC,GAAeC,MACnCC,EAAQ,MAAMF,GAAO,EAAE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CAAAjB,MAAO,IAAIkB,EAAU,kBAAkB,CAACV,GAAOjB,GAAO4B,GAAOC,MAAQ;AAC5G,QAAAC,IAAU9B,EAAM,CAAC;AACvB,MAAI,CAAC8B;AACI,WAAA;AACH,QAAAC,IAAMC,EAAIF,CAAO;AACvB,MAAI,CAACC,KAAOD,EAAQ,SAASC,CAAG;AACvB,WAAA;AAEH,QAAAE,IAAO7D,EAAM2D,GAAKtB,EAAI,IAAIe,EAAoB,QAAQ,GAAG,CAAC;AAEhE,SAAOP,EAAM,GACV,QAAQ,SAAS,EAAI,EACrB,iBAAiBW,GAAOC,GAAKtB,EAAY,KAAKE,CAAG,EAAE,OAAO,EAAE,MAAAwB,GAAM,CAAC,EACnE;AACL,CAAC,CAAC;AAEF/B,EAASuB,GAAsB;AAAA,EAC7B,aAAa;AACf,CAAC;AAGM,MAAMpD,IAA0B;AAAA,EACrCgC;AAAA,EACAgB;AAAA,EACAG;AAAA,EACAjB;AAAA,EACAkB;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.
|
|
4
|
+
"version": "7.3.1",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
@@ -29,25 +29,24 @@
|
|
|
29
29
|
"@types/node-emoji": "^1.8.1",
|
|
30
30
|
"emoji-regex": "^10.0.0",
|
|
31
31
|
"node-emoji": "^2.0.0",
|
|
32
|
-
"remark-emoji": "^
|
|
32
|
+
"remark-emoji": "^4.0.0",
|
|
33
33
|
"tslib": "^2.5.0",
|
|
34
34
|
"twemoji": "^14.0.1",
|
|
35
|
-
"unist-util-visit": "^
|
|
36
|
-
"@milkdown/exception": "7.
|
|
37
|
-
"@milkdown/utils": "7.
|
|
35
|
+
"unist-util-visit": "^5.0.0",
|
|
36
|
+
"@milkdown/exception": "7.3.1",
|
|
37
|
+
"@milkdown/utils": "7.3.1"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
|
-
"@
|
|
41
|
-
"@milkdown/
|
|
42
|
-
"@milkdown/
|
|
43
|
-
"@milkdown/
|
|
44
|
-
"@milkdown/transformer": "7.2.4"
|
|
40
|
+
"@milkdown/core": "7.3.1",
|
|
41
|
+
"@milkdown/ctx": "7.3.1",
|
|
42
|
+
"@milkdown/prose": "7.3.1",
|
|
43
|
+
"@milkdown/transformer": "7.3.1"
|
|
45
44
|
},
|
|
46
45
|
"nx": {
|
|
47
46
|
"targets": {
|
|
48
47
|
"build": {
|
|
49
48
|
"outputs": [
|
|
50
|
-
"
|
|
49
|
+
"{projectRoot}/lib"
|
|
51
50
|
],
|
|
52
51
|
"dependsOn": [
|
|
53
52
|
{
|
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
/* Copyright 2021, Milkdown by Mirone. */
|
|
2
|
-
import type {
|
|
2
|
+
import type { Node, RemarkPluginRaw } from '@milkdown/transformer'
|
|
3
3
|
import emojiRegex from 'emoji-regex'
|
|
4
|
-
import type { Literal, Node, Parent } from 'unist'
|
|
5
4
|
|
|
6
5
|
import { parse } from './parse'
|
|
7
6
|
|
|
8
7
|
const regex = emojiRegex()
|
|
9
8
|
|
|
10
|
-
const isParent = (node: Node): node is
|
|
11
|
-
const isLiteral = (node: Node): node is
|
|
9
|
+
const isParent = (node: Node): node is Node & { children: Node[] } => !!(node as Node & { children: Node[] }).children
|
|
10
|
+
const isLiteral = (node: Node): node is Node & { value: string } => !!(node as Node & { value: string }).value
|
|
12
11
|
|
|
13
12
|
function flatMap(ast: Node, fn: (node: Node, index: number, parent: Node | null) => Node[]) {
|
|
14
13
|
return transform(ast, 0, null)[0]
|
|
@@ -36,14 +35,14 @@ function flatMap(ast: Node, fn: (node: Node, index: number, parent: Node | null)
|
|
|
36
35
|
}
|
|
37
36
|
}
|
|
38
37
|
|
|
39
|
-
export const twemojiPlugin:
|
|
38
|
+
export const twemojiPlugin: RemarkPluginRaw<TwemojiOptions> = (twemojiOptions) => {
|
|
40
39
|
function transformer(tree: Node) {
|
|
41
40
|
flatMap(tree, (node) => {
|
|
42
41
|
if (!isLiteral(node))
|
|
43
42
|
return [node]
|
|
44
43
|
|
|
45
44
|
const value = node.value as string
|
|
46
|
-
const output:
|
|
45
|
+
const output: Array<Node & { value: string }> = []
|
|
47
46
|
let match
|
|
48
47
|
let str = value
|
|
49
48
|
// eslint-disable-next-line no-cond-assign
|
package/src/index.ts
CHANGED
|
@@ -1,30 +1,17 @@
|
|
|
1
1
|
/* Copyright 2021, Milkdown by Mirone. */
|
|
2
2
|
import type { MilkdownPlugin } from '@milkdown/ctx'
|
|
3
|
-
import type {
|
|
3
|
+
import type { RemarkPluginRaw } from '@milkdown/transformer'
|
|
4
4
|
import { expectDomTypeError } from '@milkdown/exception'
|
|
5
5
|
import { InputRule } from '@milkdown/prose/inputrules'
|
|
6
|
-
import { $
|
|
6
|
+
import { $inputRule, $nodeAttr, $nodeSchema, $remark } from '@milkdown/utils'
|
|
7
7
|
import { get } from 'node-emoji'
|
|
8
|
+
import type { RemarkEmojiOptions } from 'remark-emoji'
|
|
8
9
|
import remarkEmoji from 'remark-emoji'
|
|
9
|
-
import type Twemoji from 'twemoji'
|
|
10
10
|
|
|
11
11
|
import { parse } from './__internal__/parse'
|
|
12
12
|
import { twemojiPlugin } from './__internal__/remark-twemoji'
|
|
13
13
|
import { withMeta } from './__internal__/with-meta'
|
|
14
14
|
|
|
15
|
-
type TwemojiOptions = Exclude<Parameters<typeof Twemoji.parse>[1], Function | undefined>
|
|
16
|
-
|
|
17
|
-
/// @internal
|
|
18
|
-
export interface EmojiConfig {
|
|
19
|
-
twemojiOptions?: TwemojiOptions
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/// A slice that contains [options for twemoji](https://github.com/twitter/twemoji#object-as-parameter).
|
|
23
|
-
export const emojiConfig = $ctx<EmojiConfig, 'emojiConfig'>({}, 'emojiConfig')
|
|
24
|
-
withMeta(emojiConfig, {
|
|
25
|
-
displayName: 'Ctx<emojiConfig>',
|
|
26
|
-
})
|
|
27
|
-
|
|
28
15
|
/// HTML attributes for emoji node.
|
|
29
16
|
export const emojiAttr = $nodeAttr('emoji', () => ({
|
|
30
17
|
span: {},
|
|
@@ -91,6 +78,28 @@ withMeta(emojiSchema.ctx, {
|
|
|
91
78
|
displayName: 'NodeSchemaCtx<emoji>',
|
|
92
79
|
})
|
|
93
80
|
|
|
81
|
+
/// This plugin wraps [remark-emoji](https://github.com/rhysd/remark-emoji).
|
|
82
|
+
export const remarkEmojiPlugin = $remark('remarkEmoji', () => remarkEmoji as RemarkPluginRaw<RemarkEmojiOptions>)
|
|
83
|
+
|
|
84
|
+
withMeta(remarkEmojiPlugin.plugin, {
|
|
85
|
+
displayName: 'Remark<remarkEmojiPlugin>',
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
withMeta(remarkEmojiPlugin.options, {
|
|
89
|
+
displayName: 'RemarkConfig<remarkEmojiPlugin>',
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
/// This plugin is used for transforming emoji to twemoji.
|
|
93
|
+
export const remarkTwemojiPlugin = $remark('remarkTwemoji', () => twemojiPlugin)
|
|
94
|
+
|
|
95
|
+
withMeta(remarkTwemojiPlugin.plugin, {
|
|
96
|
+
displayName: 'Remark<remarkTwemojiPlugin>',
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
withMeta(remarkTwemojiPlugin.options, {
|
|
100
|
+
displayName: 'RemarkConfig<remarkTwemojiPlugin>',
|
|
101
|
+
})
|
|
102
|
+
|
|
94
103
|
/// Input rule for inserting emoji.
|
|
95
104
|
/// For example, `:smile:` will be replaced with `😄`.
|
|
96
105
|
export const insertEmojiInputRule = $inputRule(ctx => new InputRule(/(:([^:\s]+):)$/, (state, match, start, end) => {
|
|
@@ -101,7 +110,7 @@ export const insertEmojiInputRule = $inputRule(ctx => new InputRule(/(:([^:\s]+)
|
|
|
101
110
|
if (!got || content.includes(got))
|
|
102
111
|
return null
|
|
103
112
|
|
|
104
|
-
const html = parse(got, ctx.get(
|
|
113
|
+
const html = parse(got, ctx.get(remarkTwemojiPlugin.options.key))
|
|
105
114
|
|
|
106
115
|
return state.tr
|
|
107
116
|
.setMeta('emoji', true)
|
|
@@ -113,24 +122,9 @@ withMeta(insertEmojiInputRule, {
|
|
|
113
122
|
displayName: 'InputRule<insertEmojiInputRule>',
|
|
114
123
|
})
|
|
115
124
|
|
|
116
|
-
/// This plugin wraps [remark-emoji](https://github.com/rhysd/remark-emoji).
|
|
117
|
-
export const remarkEmojiPlugin = $remark(() => remarkEmoji as RemarkPlugin)
|
|
118
|
-
|
|
119
|
-
withMeta(remarkEmojiPlugin, {
|
|
120
|
-
displayName: 'Remark<remarkEmojiPlugin>',
|
|
121
|
-
})
|
|
122
|
-
|
|
123
|
-
/// This plugin is used for transforming emoji to twemoji.
|
|
124
|
-
export const remarkTwemojiPlugin = $remark(ctx => twemojiPlugin(ctx.get(emojiConfig.key).twemojiOptions))
|
|
125
|
-
|
|
126
|
-
withMeta(remarkTwemojiPlugin, {
|
|
127
|
-
displayName: 'Remark<remarkTwemojiPlugin>',
|
|
128
|
-
})
|
|
129
|
-
|
|
130
125
|
/// All plugins exported by this package.
|
|
131
126
|
export const emoji: MilkdownPlugin[] = [
|
|
132
127
|
emojiAttr,
|
|
133
|
-
emojiConfig,
|
|
134
128
|
remarkEmojiPlugin,
|
|
135
129
|
remarkTwemojiPlugin,
|
|
136
130
|
emojiSchema,
|