Skip to content

Commit

Permalink
fix(preview): allow preview for links followed by whitespace
Browse files Browse the repository at this point in the history
Signed-off-by: Max <[email protected]>
  • Loading branch information
max-nextcloud committed Mar 7, 2024
1 parent 885810e commit 81ec438
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 11 deletions.
36 changes: 28 additions & 8 deletions cypress/e2e/nodes/Preview.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { createCustomEditor } from './../../support/components.js'
import testData from '../../fixtures/Preview.md'
import { loadMarkdown, runCommands, expectMarkdown } from './helpers.js'

describe.only('Preview extension', { retries: 0 }, () => {
describe('Preview extension', { retries: 0 }, () => {

const editor = createCustomEditor({
content: '',
Expand Down Expand Up @@ -71,23 +71,34 @@ describe.only('Preview extension', { retries: 0 }, () => {
expect(editor.can().setPreview()).to.be.false
})

it('can run on a paragraph with a link', () => {
it('convert a paragraph with a link', () => {
prepareEditor('[link text](https://nextcloud.com)\n')
expect(editor.can().setPreview()).to.be.true
editor.commands.setPreview()
expectPreview()
})

it('can run the second a paragraph with a link', () => {
it('convert the second a paragraph with a link', () => {
prepareEditor('hello\n\n[link text](https://nextcloud.com)\n')
editor.commands.setTextSelection(10)
expect(editor.can().setPreview()).to.be.true
editor.commands.setPreview()
expectPreview()
})

it('convert a paragraph with a link and a space', () => {
prepareEditor('[link text](https://nextcloud.com)\n')
editor.commands.insertContentAt(
editor.state.doc.content.size - 1,
' ',
{ updateSelection: false },
)
editor.commands.setPreview()
expectPreview()
})

it('results in a preview node with the href and text with link mark', () => {
prepareEditor('[link text](https://nextcloud.com)\n')
editor.commands.setPreview()
expect(getParentNode().type.name).to.equal('preview')
expect(getParentNode().attrs.href).to.equal('https://nextcloud.com')
expect(getMark().attrs.href).to.equal('https://nextcloud.com')
expectPreview()
})

it('cannot run twice', () => {
Expand Down Expand Up @@ -131,6 +142,15 @@ describe.only('Preview extension', { retries: 0 }, () => {

})

/**
* Expect a preview in the editor.
*/
function expectPreview() {
expect(getParentNode().type.name).to.equal('preview')
expect(getParentNode().attrs.href).to.equal('https://nextcloud.com')
expect(getMark().attrs.href).to.equal('https://nextcloud.com')
}

/**
*
*/
Expand Down
4 changes: 2 additions & 2 deletions cypress/fixtures/Preview.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ empty

[link text](https://nextcloud.com)

## Preserves a link preview
## Preserves a link preview - TODO

[link text](https://nextcloud.com (Preview))

---

[link text](https://nextcloud.com (Preview))
[link text](https://nextcloud.com "Preview")
11 changes: 10 additions & 1 deletion src/nodes/Preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ function isPreview(typeOrName, attributes, state) {
*/
function previewPossible({ selection }) {
const { $from } = selection
if (childCount($from.parent) > 1) {
if (hasOtherContent($from.parent)) {
return false
}
const href = extractHref($from.nodeAfter)
Expand All @@ -148,6 +148,15 @@ function previewPossible({ selection }) {
return true
}

/**
* Does the node contain more content than the first child
* @param node - node to inspect
*/
function hasOtherContent(node) {
return childCount(node) > 2
|| (childCount(node) === 2 && node.lastChild.textContent.trim())
}

/**
*
* @param node
Expand Down

0 comments on commit 81ec438

Please sign in to comment.