Skip to content

Commit

Permalink
Merge pull request #3886 from nextcloud/backport/3873/stable26
Browse files Browse the repository at this point in the history
[stable26] Use the href of links for link previews, not `node.textContent`
  • Loading branch information
juliusknorr authored Mar 8, 2023
2 parents f143baf + 8a4975b commit 98326b1
Show file tree
Hide file tree
Showing 13 changed files with 55 additions and 24 deletions.
4 changes: 2 additions & 2 deletions js/editor.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/editor.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/text-editors.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/text-editors.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/text-files.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/text-files.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/text-public.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/text-public.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/text-text.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/text-text.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/text-viewer.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/text-viewer.js.map

Large diffs are not rendered by default.

43 changes: 37 additions & 6 deletions src/nodes/ParagraphView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export default {
data() {
return {
text: null,
isLoggedIn: getCurrentUser()
isLoggedIn: getCurrentUser(),
}
},
watch: {
Expand All @@ -63,20 +63,51 @@ export default {
},
beforeCreate() {
this.debouncedUpdateText = debounce((newNode) => {
this.text = this.getTextReference(this.node?.textContent)
this.text = this.getTextReference(this.node)
}, 500)
},
created() {
this.text = this.getTextReference(this.node?.textContent)
this.text = this.getTextReference(this.node)
},
beforeUnmount() {
this.debouncedUpdateText?.cancel()
},
methods: {
getTextReference(text) {
getTextReference(node) {
if (!node?.childCount) {
return null
}

// Only regard paragraphs with exactly one text node (ignoring whitespace-only nodes)
let textNode
for (let i = 0; i < node.childCount; i++) {
const childNode = node.child(i)

// Disregard paragraphs with non-text nodes
if (childNode.type.name !== 'text') {
return null
}

// Ignore children with empty text
if (!childNode.textContent.trim()) {
continue
}

// Disregard paragraphs with more than one text nodes
if (textNode) {
return null
}

textNode = childNode
}

// Check if the text node is a link
const linkMark = textNode?.marks.find((m) => m.type.name === 'link')
const href = linkMark?.attrs?.href

const PATTERN = /(^)(https?:\/\/)((?:[-A-Z0-9+_]+\.)+[-A-Z]+(?:\/[-A-Z0-9+&@#%?=~_|!:,.;()]*)*)($)/ig
if ((new RegExp(PATTERN)).test(text.trim())) {
return text.trim()
if ((new RegExp(PATTERN)).test(href)) {
return href
}

return null
Expand Down

0 comments on commit 98326b1

Please sign in to comment.