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

[stable25] Use the href of links for link previews, not node.textContent #3887

Merged
merged 2 commits into from
Mar 5, 2023
Merged
Changes from 1 commit
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
43 changes: 37 additions & 6 deletions src/nodes/ParagraphView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
const PATTERN = /(^)(https?:\/\/)?((?:[-A-Z0-9+_]+\.)+[-A-Z]+(?:\/[-A-Z0-9+&@#%?=~_|!:,.;()]*)*)($)/ig
if ((new RegExp(PATTERN)).test(text)) {
return 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(href)) {
return href
}

return null
Expand Down