UNPKG

@nextcloud/vue

Version:
1 lines 10.6 kB
{"version":3,"file":"autolink-B2azbG18.mjs","sources":["../../src/components/NcRichText/NcRichTextExternalLink.vue","../../src/components/NcRichText/helpers.ts","../../src/components/NcRichText/autolink.ts"],"sourcesContent":["<!--\n - SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors\n - SPDX-License-Identifier: AGPL-3.0-or-later\n-->\n\n<script setup lang=\"ts\">\nconst { href, decorateExternal = false } = defineProps<{\n\t/** Link attribute to render */\n\thref: string\n\t/** Whether to add the appended arrow decoration */\n\tdecorateExternal?: boolean\n}>()\n</script>\n\n<template>\n\t<a\n\t\t:href=\"href\"\n\t\trel=\"noopener noreferrer\"\n\t\ttarget=\"_blank\"\n\t\t:class=\"[$style.externalLink, {\n\t\t\t[$style.externalLink_decorated]: decorateExternal,\n\t\t}]\">\n\t\t<slot name=\"default\">\n\t\t\t{{ href }}\n\t\t</slot>\n\t</a>\n</template>\n\n<style module lang=\"scss\">\n.externalLink {\n\ttext-decoration: underline;\n}\n\n.externalLink_decorated::after {\n\tcontent: ' ↗';\n}\n</style>\n\n<docs>\nAn internal component\n</docs>\n","/*!\n * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors\n * SPDX-License-Identifier: AGPL-3.0-or-later\n */\n\nimport type { Literal } from 'unist'\n\n/**\n * Unist literal node with string value like code blocks or text nodes\n */\nexport interface TextNode extends Literal {\n\tvalue: string\n}\n\n/**\n * Regex pattern to match links to be resolved by the link-reference provider\n */\nexport const URL_PATTERN = /(\\s|^)(https?:\\/\\/)([-A-Z0-9+_.]+(?::[0-9]+)?(?:\\/[-A-Z0-9+&@#%?=~_|!:,.;()]*)*)(\\s|$)/ig\n\n/**\n * Regex pattern to identify strings as links and then making them clickable\n */\nexport const URL_PATTERN_AUTOLINK = /(\\s|\\(|^)((https?:\\/\\/)([-A-Z0-9+_.]+[-A-Z0-9]+(?::[0-9]+)?(?:\\/[-A-Z0-9+&@#%?=~_|!:,.;()]*)*))(?=\\s|\\)|$)/ig\n","/**\n * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors\n * SPDX-License-Identifier: AGPL-3.0-or-later\n */\n\nimport type { Node, Parent } from 'unist'\nimport type { Router } from 'vue-router'\n\nimport { getBaseUrl, getRootUrl } from '@nextcloud/router'\nimport { u } from 'unist-builder'\nimport { SKIP, visitParents } from 'unist-util-visit-parents'\nimport NcRichTextExternalLink from './NcRichTextExternalLink.vue'\nimport { logger } from '../../utils/logger.ts'\nimport { URL_PATTERN_AUTOLINK } from './helpers.js'\n\n/**\n *\n * @param root0\n * @param root0.autolink\n * @param root0.useMarkdown\n * @param root0.useExtendedMarkdown\n */\nexport function remarkAutolink({ autolink, useMarkdown, useExtendedMarkdown }) {\n\treturn function(tree) {\n\t\t// remark-gfm has its own autolink parser which can not be disabled\n\t\t// and thus a local one is not needed\n\t\tif (useExtendedMarkdown || !useMarkdown || !autolink) {\n\t\t\treturn\n\t\t}\n\n\t\tvisitParents(tree, (node) => node.type === 'text', (node, ancestors: Parent[]) => {\n\t\t\t// Do not autolink text already inside a link node\n\t\t\tif (ancestors.some((ancestor) => ancestor.type === 'link' || ancestor.type === 'linkReference')) {\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\tconst parent = ancestors.at(-1)\n\t\t\tconst index = parent!.children.indexOf(node) ?? 0\n\n\t\t\tconst parsed = parseUrl(node.value)\n\t\t\tconst parsedNodes: Node[] = (typeof parsed === 'string')\n\t\t\t\t? [u('text', parsed)]\n\t\t\t\t: parsed.map((n) => {\n\t\t\t\t\t\tif (typeof n === 'string') {\n\t\t\t\t\t\t\treturn u('text', n)\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\treturn u('link', {\n\t\t\t\t\t\t\turl: n.props.href,\n\t\t\t\t\t\t}, [u('text', n.props.href)])\n\t\t\t\t\t})\n\t\t\t\t\t\t.filter((x) => x)\n\t\t\t\t\t\t.flat()\n\n\t\t\tparent!.children.splice(index, 1, ...parsedNodes)\n\t\t\treturn [SKIP, index + parsedNodes.length]\n\t\t})\n\t}\n}\n\n/**\n *\n * @param text\n */\nexport function parseUrl(text: string) {\n\tlet match = URL_PATTERN_AUTOLINK.exec(text)\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tconst list: (string | Record<string, any>)[] = []\n\tlet start = 0\n\twhile (match !== null) {\n\t\tlet href = match[2]!\n\t\tlet textAfter\n\t\tlet textBefore = text.substring(start, match.index + match[1]!.length)\n\t\tif (href[0] === ' ') {\n\t\t\ttextBefore += href[0]\n\t\t\thref = href.substring(1).trim()\n\t\t}\n\t\tconst lastChar = href[(href.length - 1)]\n\t\tif (lastChar === '.' || lastChar === ',' || lastChar === ';' || (match[0][0] === '(' && lastChar === ')')) {\n\t\t\thref = href.substring(0, href.length - 1)\n\t\t\ttextAfter = lastChar\n\t\t}\n\t\tlist.push(textBefore)\n\t\tlist.push({ component: NcRichTextExternalLink, props: { href: href.trim(), decorateExternal: true } })\n\t\tif (textAfter) {\n\t\t\tlist.push(textAfter)\n\t\t}\n\t\tstart = match.index + match[0].length\n\t\tmatch = URL_PATTERN_AUTOLINK.exec(text)\n\t}\n\tlist.push(text.substring(start))\n\tconst joinedText = list.map((item) => typeof item === 'string' ? item : item.props.href).join('')\n\tif (text === joinedText) {\n\t\treturn list\n\t}\n\tlogger.error('[NcRichText] Failed to reassemble the chunked text: ' + text)\n\treturn text\n}\n\n/**\n * Try to get path for router link from an absolute or relative URL.\n *\n * @param router - VueRouter instance of the router link\n * @param url - absolute URL to parse\n * @return a path that can be used in the router link or null if this URL doesn't match this router config\n * @example http://cloud.ltd/nextcloud/index.php/apps/files/favorites?fileid=2#fragment => /files/favorites?fileid=2#fragment\n */\nexport function getRoute(router: Router | null, url: string): string | null {\n\t/**\n\t * http://cloud.ltd /nextcloud /index.php /apps/files /favorites?fileid=2#fragment\n\t * |_____origin____|___________router-base____________| |\n\t * |__________base____________| |______________relative-url______________|\n\t * | |___root___|_optional_|__app-base__|_________router-path_______|\n\t */\n\n\tconst removePrefix = (str, prefix) => str.startsWith(prefix) ? str.slice(prefix.length) : str\n\tconst removePrefixes = (str, ...prefixes) => prefixes.reduce((acc, prefix) => removePrefix(acc, prefix), str)\n\n\t// Router is not defined in the app => not an app route\n\tif (!router) {\n\t\treturn null\n\t}\n\n\tconst isAbsoluteURL = /^https?:\\/\\//.test(url)\n\n\t// URL is a \"mailto:\", \"tel:\" or any custom app protocol link\n\tconst isNonHttpLink = /^[a-z][a-z0-9+.-]*:.+/.test(url)\n\tif (!isAbsoluteURL && isNonHttpLink) {\n\t\treturn null\n\t}\n\n\t// URL is not a link to this Nextcloud server instance => not an app route\n\tif (isAbsoluteURL && !url.startsWith(getBaseUrl())) {\n\t\treturn null\n\t}\n\n\tif (!isAbsoluteURL && !url.startsWith('/')) {\n\t\t// Relative URL without a leading slash are not allowed\n\t\t// VueRouter handles such urls according to the URL RFC,\n\t\t// for example, being on /call/abc page, link \"def\" is resolved as \"/call/def\" relative to \"/call/\".\n\t\t// We don't want to support such links,\n\t\t// so relative URL MUST start with a slash.\n\t\treturn null\n\t}\n\n\t// URL relative to the instance root\n\tconst relativeUrl = isAbsoluteURL ? removePrefixes(url, getBaseUrl(), '/index.php') : url\n\n\t// Router base relative to the instance root (app-base above)\n\tconst relativeRouterBase = removePrefixes(router.options.history.base, getRootUrl(), '/index.php')\n\n\t// Root route may have an empty '' path, fallback to '/'\n\tconst potentialRouterPath = removePrefixes(relativeUrl, relativeRouterBase) || '/'\n\n\t// Check if there is actually matching route in the router for this path\n\tconst route = router.resolve(potentialRouterPath)\n\n\tif (!route.matched.length) {\n\t\treturn null\n\t}\n\n\treturn route.fullPath\n}\n"],"names":["_createElementBlock","_normalizeClass","$style","_renderSlot"],"mappings":";;;;;;;;;;;;;;;0BAeCA,mBAUI,KAAA;AAAA,QATF,MAAM,QAAA;AAAA,QACP,KAAI;AAAA,QACJ,QAAO;AAAA,QACN,OAAKC,eAAA,CAAGC,KAAAA,OAAO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