diff --git a/js/modules/link_text.d.ts b/js/modules/link_text.d.ts new file mode 100644 index 000000000..72e015249 --- /dev/null +++ b/js/modules/link_text.d.ts @@ -0,0 +1,9 @@ +declare namespace LinkText { + type Attributes = { + [key: string]: string; + } +} + +declare function linkText(value: string, attributes: LinkText.Attributes): string; + +export = linkText; diff --git a/js/modules/link_text.js b/js/modules/link_text.js new file mode 100644 index 000000000..b1be65dc1 --- /dev/null +++ b/js/modules/link_text.js @@ -0,0 +1,47 @@ +// Fork of https://github.com/uiureo/link-text with HTML escaping disabled as we leverage +// jQuery’s escaping mechanism: + +var linkify = require('linkify-it')() +var escape = require('escape-html') + +function createLink (url, text, attrs) { + attrs = attrs || {} + + var html = [] + html.push('') + html.push(decodeURIComponent(text)) + html.push('') + + return html.join('') +} + +module.exports = function (text, attrs) { + attrs = attrs || {} + text = escape(text) + + var matchData = linkify.match(text) || [] + + var result = [] + var last = 0 + + matchData.forEach(function (match) { + if (last < match.index) { + result.push(text.slice(last, match.index)) + } + + result.push( + createLink(match.url, match.text, attrs) + ) + + last = match.lastIndex + }) + + result.push(text.slice(last)) + + return result.join('') +} diff --git a/ts/@types/link-text/index.d.ts b/ts/@types/link-text/index.d.ts deleted file mode 100644 index 9663edce6..000000000 --- a/ts/@types/link-text/index.d.ts +++ /dev/null @@ -1,6 +0,0 @@ -declare module 'link-text' { - type Attributes = { - [key: string]: string; - } - export default function (value: string, attributes: Attributes): string -} diff --git a/ts/html/index.ts b/ts/html/index.ts index b3c6ae211..3aad63218 100644 --- a/ts/html/index.ts +++ b/ts/html/index.ts @@ -1,4 +1,4 @@ -import linkTextInternal from 'link-text'; +import linkTextInternal from '../../js/modules/link_text'; export const linkText = (value: string): string =>