Skip to content
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

test: add e2e for latex feature #1618

Merged
merged 3 commits into from
Jan 14, 2025
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 @@ -18,6 +18,7 @@
"@codemirror/view": "^6.26.0",
"@milkdown/core": "workspace:*",
"@milkdown/ctx": "workspace:*",
"@milkdown/crepe": "workspace:*",
"@milkdown/plugin-automd": "workspace:*",
"@milkdown/plugin-clipboard": "workspace:*",
"@milkdown/plugin-cursor": "workspace:*",
Expand Down
11 changes: 11 additions & 0 deletions e2e/src/crepe/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Crepe</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/crepe/main.ts"></script>
</body>
</html>
4 changes: 4 additions & 0 deletions e2e/src/crepe/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const crepe = {
title: 'Crepe',
link: '/crepe/',
}
13 changes: 13 additions & 0 deletions e2e/src/crepe/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Crepe } from '@milkdown/crepe'
import '@milkdown/crepe/theme/common/style.css'
import '@milkdown/crepe/theme/frame.css'

import { setup } from '../utils'

setup(async () => {
const crepe = new Crepe({
root: '#app',
})
await crepe.create()
return crepe.editor
})
2 changes: 2 additions & 0 deletions e2e/src/data.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { crepe } from './crepe'
import { multiEditor } from './multi-editor'
import { automd } from './plugin-automd'
import { listener } from './plugin-listener'
Expand All @@ -10,4 +11,5 @@ export const cases: { title: string; link: string }[] = [
multiEditor,
listener,
automd,
crepe,
]
74 changes: 74 additions & 0 deletions e2e/tests/crepe/latex.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { expect, test } from '@playwright/test'
import { focusEditor, getMarkdown } from 'tests/misc'

test.beforeEach(async ({ page }) => {
await page.goto('/crepe/')
})

test('latex block preview toggle', async ({ page }) => {
await focusEditor(page)
await page.keyboard.type('$$')
await page.keyboard.press('Enter')

const codeBlock = page.locator('milkdown-code-block')
const codemirror = codeBlock.locator('.codemirror-host')
const preview = codeBlock.locator('.preview')
const codeTools = codeBlock.locator('.tools')
const previewToggleButton = codeTools.locator('.preview-toggle-button')

await expect(codeBlock).toBeVisible()

// when code block is empty, preview toggle button is not visible
await codeTools.hover()
await expect(previewToggleButton).not.toBeVisible()

await page.keyboard.press('ArrowUp')
await page.keyboard.type('E=mc^2')

await expect(preview).toBeVisible()
const markdown = await getMarkdown(page)
expect(markdown.trim()).toEqual('$$\nE=mc^2\n$$')

await codeTools.hover()
await expect(previewToggleButton).toBeVisible()

await expect(codemirror).toBeVisible()
await previewToggleButton.click()
await expect(codemirror).not.toBeVisible()

await previewToggleButton.click()
await expect(codemirror).toBeVisible()
})

test('latex inline', async ({ page }) => {
await focusEditor(page)
await page.keyboard.type('$E=mc^2$')

const inlineLatex = page.locator('span[data-type="math_inline"]')
const inlineLatexInput = page.locator('milkdown-latex-inline-edit')
const inlineLatexInputConfirm = inlineLatexInput.locator('button')

await expect(inlineLatex).toBeVisible()

let markdown = await getMarkdown(page)
expect(markdown.trim()).toEqual('$E=mc^2$')

await page.keyboard.press('ArrowLeft')
await expect(inlineLatexInput).toBeVisible()

await inlineLatexInput.click()
await page.keyboard.press('Backspace')
await page.keyboard.type('3')
await page.keyboard.press('Enter')

markdown = await getMarkdown(page)
expect(markdown.trim()).toEqual('$E=mc^3$')

await inlineLatexInput.click()
await page.keyboard.press('Backspace')
await page.keyboard.type('4')
await inlineLatexInputConfirm.click()

markdown = await getMarkdown(page)
expect(markdown.trim()).toEqual('$E=mc^4$')
})
25 changes: 25 additions & 0 deletions packages/crepe/src/feature/latex/block-latex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { codeBlockSchema } from '@milkdown/kit/preset/commonmark'

export const blockLatexSchema = codeBlockSchema.extendSchema((prev) => {
return (ctx) => {
const baseSchema = prev(ctx)
return {
...baseSchema,
toMarkdown: {
match: baseSchema.toMarkdown.match,
runner: (state, node) => {
const language = node.attrs.language
if (language.toLowerCase() === 'latex') {
state.addNode(
'math',
undefined,
node.content.firstChild?.text || ''
)
} else {
return baseSchema.toMarkdown.runner(state, node)
}
},
},
}
}
})
2 changes: 2 additions & 0 deletions packages/crepe/src/feature/latex/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { inlineLatexTooltip } from './inline-tooltip/tooltip'
import { LatexInlineTooltip } from './inline-tooltip/view'
import { confirmIcon } from '../../icons'
import { mathBlockInputRule, mathInlineInputRule } from './input-rule'
import { blockLatexSchema } from './block-latex'

export interface LatexConfig {
katexOptions: KatexOptions
Expand Down Expand Up @@ -59,6 +60,7 @@ export const defineFeature: DefineFeature<LatexFeatureConfig> = (
.use(inlineLatexTooltip)
.use(mathInlineInputRule)
.use(mathBlockInputRule)
.use(blockLatexSchema)
}

function renderLatex(content: string, options?: KatexOptions) {
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.

Loading