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

Show multiple direct link suggestions in a single line #280

Merged
merged 1 commit into from
Mar 2, 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
6 changes: 4 additions & 2 deletions src/vendor/plugins/references/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import type { Config } from './index'

export const completions = (config: Config) => {
return (context: CompletionContext): CompletionResult | null => {
const match = context.matchBefore(/\[\[.*?/)
const match = context.matchBefore(/\[\[(?:.(?!\[\[))*?/)

if (!match) { return null }
if (!match) {
return null
}

return {
from: match.from + 2,
Expand Down
20 changes: 11 additions & 9 deletions src/vendor/plugins/references/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { HighlightStyle, syntaxHighlighting } from '@codemirror/language'
import { Tag, tags as highlightTags } from '@lezer/highlight'
import type { MarkdownConfig } from '@lezer/markdown'
import { completions } from './completions'
import { replacements } from './replacements'
import type { MarkdownConfig } from '@lezer/markdown'

const LEFT_BRACKET_CODE = 91
const RIGHT_BRACKET_CODE = 93

export interface Config {
docs: Doc[]
docs: Doc[],
}

export interface Doc {
id: string
title: string
id: string,
title: string,
}

const tags = {
Expand All @@ -38,7 +38,7 @@ const grammar: MarkdownConfig = {
{
name: 'ReferenceStart',
parse(cx, next, pos) {
return next === LEFT_BRACKET_CODE && cx.char(pos + 1) === LEFT_BRACKET_CODE
return (next === LEFT_BRACKET_CODE && cx.char(pos + 1) === LEFT_BRACKET_CODE)
? cx.addDelimiter(ReferenceStartDelimiter, pos, pos + 2, true, false)
: -1
},
Expand All @@ -47,9 +47,11 @@ const grammar: MarkdownConfig = {
{
name: 'Reference',
parse(cx, next, pos) {
if (!(next === RIGHT_BRACKET_CODE && cx.char(pos + 1) === RIGHT_BRACKET_CODE)) { return -1 }
if (!(next === RIGHT_BRACKET_CODE && cx.char(pos + 1) === RIGHT_BRACKET_CODE)) {
return -1
}

// @ts-ignore
// @ts-expect-error private api
const parts = cx.parts
const openIndex = cx.findOpeningDelimiter(ReferenceStartDelimiter)

Expand All @@ -61,7 +63,7 @@ const grammar: MarkdownConfig = {
content.unshift(cx.elt('ReferenceMark', start, start + 2))
content.push(cx.elt('ReferenceMark', end - 2, end))

let ref = parts[openIndex] = cx.elt('Reference', start, end, content)
const ref = parts[openIndex] = cx.elt('Reference', start, end, content)

return ref.to
}
Expand All @@ -81,7 +83,7 @@ const theme = syntaxHighlighting(
{
tag: tags.referenceMark,
},
])
]),
)

export const references = (config: Config) => {
Expand Down
69 changes: 69 additions & 0 deletions test/e2e/editor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,73 @@ test.describe('editor', () => {

await expect(page.locator('.ink-mde-editor-content')).toHaveText('hello')
})

test('shows direct link suggestions', async ({ page }) => {
await page.goto('/docs/new')
await page.waitForSelector('[data-is-mounted="true"]')

await page.keyboard.type('# My Test Doc')

// Wait for the data to be persisted.
await page.waitForTimeout(200)

await page.goto('/docs/new')
await page.waitForSelector('[data-is-mounted="true"]')

await page.keyboard.type('Check out [[My ')

await expect(page.locator('.cm-tooltip-autocomplete')).toBeVisible()
await expect(page.locator('.cm-tooltip-autocomplete')).toContainText(/My Test Doc/)
await expect(page.locator('.ink-mde-editor-content')).toHaveText('Check out [[My ]]')
})

test('shows direct link suggestions after an existing direct link', async ({ page }) => {
await page.goto('/docs/new')
await page.waitForSelector('[data-is-mounted="true"]')

await page.keyboard.type('# My Test Doc')

// Wait for the data to be persisted.
await page.waitForTimeout(200)

await page.goto('/docs/new')
await page.waitForSelector('[data-is-mounted="true"]')

await page.keyboard.type('Check out [[link]] and [[My ')

await expect(page.locator('.cm-tooltip-autocomplete')).toBeVisible()
await expect(page.locator('.cm-tooltip-autocomplete')).toContainText(/My Test Doc/)
await expect(page.locator('.ink-mde-editor-content')).toHaveText('Check out [[link]] and [[My ]]')
})

test('shows direct link suggestions before an existing direct link', async ({ page }) => {
await page.goto('/docs/new')
await page.waitForSelector('[data-is-mounted="true"]')

await page.keyboard.type('# My Test Doc')

// Wait for the data to be persisted.
await page.waitForTimeout(200)

await page.goto('/docs/new')
await page.waitForSelector('[data-is-mounted="true"]')

await page.keyboard.type('Check out [[link]]')

await page.keyboard.press('ArrowLeft')
await page.keyboard.press('ArrowLeft')
await page.keyboard.press('ArrowLeft')
await page.keyboard.press('ArrowLeft')
await page.keyboard.press('ArrowLeft')
await page.keyboard.press('ArrowLeft')
await page.keyboard.press('ArrowLeft')
await page.keyboard.press('ArrowLeft')
await page.keyboard.press('ArrowLeft')

await page.keyboard.type('[[My ')

await expect(page.locator('.cm-tooltip-autocomplete')).toBeVisible()
await expect(page.locator('.cm-tooltip-autocomplete')).toContainText(/My Test Doc/)
await expect(page.locator('.ink-mde-editor-content')).toHaveText('Check out[[My ]] [[link]]')
})
})
Loading