Fine-tune linkification technique for link previews
This commit is contained in:
parent
021e807180
commit
858c7e629f
3 changed files with 104 additions and 3 deletions
|
@ -1,5 +1,6 @@
|
|||
/* global URL */
|
||||
|
||||
const { isNumber, compact } = require('lodash');
|
||||
const he = require('he');
|
||||
const LinkifyIt = require('linkify-it');
|
||||
|
||||
|
@ -96,9 +97,28 @@ function getImageMetaTag(html) {
|
|||
return _getMetaTag(html, META_IMAGE);
|
||||
}
|
||||
|
||||
function findLinks(text) {
|
||||
function findLinks(text, caretLocation) {
|
||||
const haveCaretLocation = isNumber(caretLocation);
|
||||
const textLength = text ? text.length : 0;
|
||||
|
||||
const matches = linkify.match(text || '') || [];
|
||||
return matches.map(match => match.text);
|
||||
return compact(
|
||||
matches.map(match => {
|
||||
if (!haveCaretLocation) {
|
||||
return match.text;
|
||||
}
|
||||
|
||||
if (match.lastIndex === textLength && caretLocation === textLength) {
|
||||
return match.text;
|
||||
}
|
||||
|
||||
if (match.index > caretLocation || match.lastIndex < caretLocation) {
|
||||
return match.text;
|
||||
}
|
||||
|
||||
return null;
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
function getDomain(url) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue