Skip to content

fix: 🐛 should not throw error when emoji in code #1268

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions e2e/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"@milkdown/ctx": "workspace:*",
"@milkdown/plugin-automd": "workspace:*",
"@milkdown/plugin-clipboard": "workspace:*",
"@milkdown/plugin-emoji": "workspace:*",
"@milkdown/plugin-history": "workspace:*",
"@milkdown/plugin-listener": "workspace:*",
"@milkdown/plugin-math": "workspace:*",
Expand Down
63 changes: 63 additions & 0 deletions e2e/src/plugin-emoji/main.stories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/* Copyright 2021, Milkdown by Mirone. */
import type { Meta, StoryObj } from '@storybook/html'
import { Editor, defaultValueCtx, rootCtx } from '@milkdown/core'
import { nord } from '@milkdown/theme-nord'
import { commonmark } from '@milkdown/preset-commonmark'
import { history } from '@milkdown/plugin-history'
import { emoji } from '@milkdown/plugin-emoji'

import '@milkdown/theme-nord/style.css'

import '../style.css'

interface Args {
enableInspector: boolean
defaultValue: string
instance: Editor
}

const meta: Meta<Args> = {
title: 'Emoji/Main',
}

export default meta

type Story = StoryObj<Args>

export const Empty: Story = {
render: (args) => {
const root = document.createElement('div')
const editor = Editor.make()
.enableInspector(args.enableInspector ?? false)
.config((ctx) => {
ctx.set(defaultValueCtx, args.defaultValue ?? '')
ctx.set(rootCtx, root)
})
.config(nord)
.use(commonmark)
.use(history)
.use(emoji)
.create()

editor.then((instance) => {
args.instance = instance
})

return root
},
}

const defaultValue = `
# Milkdown

🫥

:+1:
`

export const WithDefaultValue: Story = {
...Empty,
args: {
defaultValue: defaultValue.trim(),
},
}
4 changes: 2 additions & 2 deletions e2e/src/preset-commonmark/commands.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export const ToggleStrong: Story = {

await expect(args.instance.action(getMarkdown())).toContain(`**${text}**`)

const strong = canvasElement.querySelector('strong')
const strong = canvasElement.querySelector('strong') ?? undefined

await expect(strong).toHaveTextContent(text)
await userEvent.pointer([{ target: strong, offset: 0, keys: '[MouseLeft>]' }, { offset: text.length }])
Expand Down Expand Up @@ -91,7 +91,7 @@ export const ToggleItalic: Story = {

await expect(args.instance.action(getMarkdown())).toContain(`*${text}*`)

const em = canvasElement.querySelector('em')
const em = canvasElement.querySelector('em') ?? undefined

await expect(em).toHaveTextContent(text)
await userEvent.pointer([{ target: em, offset: 0, keys: '[MouseLeft>]' }, { offset: text.length }])
Expand Down
8 changes: 8 additions & 0 deletions e2e/src/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,11 @@ input:focus {
outline: none;
box-shadow: none;
}

span[data-type='emoji'] {
@apply inline-block;
}

span[data-type='emoji'] .emoji {
@apply w-4;
}
4 changes: 4 additions & 0 deletions packages/plugin-emoji/src/__internal__/remark-twemoji.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ export const twemojiPlugin: RemarkPluginRaw<TwemojiOptions> = (twemojiOptions) =
if (!isLiteral(node))
return [node]

// Should not convert code block
if (node.type === 'code')
return [node]

const value = node.value
const output: Array<Node & { value: string }> = []
let match
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.