UNPKG

@nextcloud/vue

Version:
1 lines 3.77 kB
{"version":3,"file":"index.mjs","sources":["../../../src/utils/linkify.ts","../../../src/directives/Linkify/index.ts"],"sourcesContent":["/**\n * SPDX-FileCopyrightText: 2024 Nick Frasser <https://nfrasser.com>\n * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors\n * SPDX-License-Identifier: AGPL-3.0-or-later\n */\nimport type { IntermediateRepresentation } from 'linkifyjs'\n\nimport escapeHTML from 'escape-html'\nimport { Options, tokenize } from 'linkifyjs'\n\n/**\n * Convert a plan text string to an HTML string with links. Expects that the\n * given strings does not contain any HTML entities. Use the linkify-html\n * interface if you need to parse HTML entities.\n *\n * @param str - String to linkify\n */\nexport function linkifyString(str: string): string {\n\tconst options = new Options({\n\t\tdefaultProtocol: 'https',\n\t\ttarget: '_blank',\n\t\tclassName: 'external linkified',\n\t\tattributes: {\n\t\t\trel: 'nofollow noopener noreferrer',\n\t\t},\n\t}, defaultRender)\n\n\tconst tokens = tokenize(str)\n\tconst result: string[] = []\n\n\tfor (const token of tokens) {\n\t\tif (token.t === 'nl' && options.get('nl2br')) {\n\t\t\tresult.push('<br>\\n')\n\t\t} else if (!token.isLink || !options.check(token)) {\n\t\t\tresult.push(escapeHTML(token.toString()))\n\t\t} else {\n\t\t\tresult.push(options.render(token))\n\t\t}\n\t}\n\n\treturn result.join('')\n}\n\n/**\n * Escape quotation marks in links for href attribute\n *\n * @param href The link to escape\n */\nfunction escapeAttr(href: string): string {\n\treturn href.replace(/\"/g, '&quot;')\n}\n\n/**\n * Map attributes to a string.\n *\n * @param attributes - The attribute mapping\n */\nfunction attributesToString(attributes: Record<string, string>): string {\n\tconst result: string[] = []\n\tfor (const attr in attributes) {\n\t\tconst val = attributes[attr] + ''\n\t\tresult.push(`${attr}=\"${escapeAttr(val)}\"`)\n\t}\n\treturn result.join(' ')\n}\n\n/**\n * Render the link.\n *\n * @param options - Options\n * @param options.tagName - The tag name\n * @param options.attributes - The attributes\n * @param options.content - The text content\n */\nfunction defaultRender({ tagName, attributes, content }: IntermediateRepresentation): string {\n\treturn `<${tagName} ${attributesToString(attributes)}>${escapeHTML(content)}</${tagName}>`\n}\n","/**\n * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors\n * SPDX-License-Identifier: AGPL-3.0-or-later\n */\n\nimport type { Directive } from 'vue'\n\nimport { linkifyString } from '../../utils/linkify.ts'\n\nconst directive: Directive = function(el: HTMLElement, { value }) {\n\tif (value?.linkify === true) {\n\t\tel.innerHTML = linkifyString(value.text)\n\t}\n}\n\nexport default directive\n"],"names":[],"mappings":";;AAiBO,SAAS,cAAc,KAAqB;AAClD,QAAM,UAAU,IAAI,QAAQ;AAAA,IAC3B,iBAAiB;AAAA,IACjB,QAAQ;AAAA,IACR,WAAW;AAAA,IACX,YAAY;AAAA,MACX,KAAK;AAAA,IAAA;AAAA,EACN,GACE,aAAa;AAEhB,QAAM,SAAS,SAAS,GAAG;AAC3B,QAAM,SAAmB,CAAA;AAEzB,aAAW,SAAS,QAAQ;AAC3B,QAAI,MAAM,MAAM,QAAQ,QAAQ,IAAI,OAAO,GAAG;AAC7C,aAAO,KAAK,QAAQ;AAAA,IACrB,WAAW,CAAC,MAAM,UAAU,CAAC,QAAQ,MAAM,KAAK,GAAG;AAClD,aAAO,KAAK,WAAW,MAAM,SAAA,CAAU,CAAC;AAAA,IACzC,OAAO;AACN,aAAO,KAAK,QAAQ,OAAO,KAAK,CAAC;AAAA,IAClC;AAAA,EACD;AAEA,SAAO,OAAO,KAAK,EAAE;AACtB;AAOA,SAAS,WAAW,MAAsB;AACzC,SAAO,KAAK,QAAQ,MAAM,QAAQ;AACnC;AAOA,SAAS,mBAAmB,YAA4C;AACvE,QAAM,SAAmB,CAAA;AACzB,aAAW,QAAQ,YAAY;AAC9B,UAAM,MAAM,WAAW,IAAI,IAAI;AAC/B,WAAO,KAAK,GAAG,IAAI,KAAK,WAAW,GAAG,CAAC,GAAG;AAAA,EAC3C;AACA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