refactor: reuse regexp

pull/363/head
Eduardo San Martin Morote 3 years ago
parent 6c2c0f093a
commit 4a34b93090

@ -1,6 +1,7 @@
import { computed } from 'vue' import { computed } from 'vue'
import { useData } from 'vitepress' import { useData } from 'vitepress'
import type { DefaultTheme } from '../config' import type { DefaultTheme } from '../config'
import { EXTERNAL_URL_RE } from '../../shared'
export const platforms = ['GitHub', 'GitLab', 'Bitbucket'].map((platform) => { export const platforms = ['GitHub', 'GitLab', 'Bitbucket'].map((platform) => {
return [platform, new RegExp(platform, 'i')] as const return [platform, new RegExp(platform, 'i')] as const
@ -26,7 +27,7 @@ export function useRepo() {
function getRepoUrl(repo: string): string { function getRepoUrl(repo: string): string {
// if the full url is not provided, default to GitHub repo // if the full url is not provided, default to GitHub repo
return /^https?:/.test(repo) ? repo : `https://github.com/${repo}` return EXTERNAL_URL_RE.test(repo) ? repo : `https://github.com/${repo}`
} }
function getRepoText(url: string, text?: string): string { function getRepoText(url: string, text?: string): string {

@ -5,6 +5,7 @@
import MarkdownIt from 'markdown-it' import MarkdownIt from 'markdown-it'
import { MarkdownParsedData } from '../markdown' import { MarkdownParsedData } from '../markdown'
import { URL } from 'url' import { URL } from 'url'
import { EXTERNAL_URL_RE } from '../../shared'
const indexRE = /(^|.*\/)index.md(#?.*)$/i const indexRE = /(^|.*\/)index.md(#?.*)$/i
@ -18,7 +19,7 @@ export const linkPlugin = (
if (hrefIndex >= 0) { if (hrefIndex >= 0) {
const hrefAttr = token.attrs![hrefIndex] const hrefAttr = token.attrs![hrefIndex]
const url = hrefAttr[1] const url = hrefAttr[1]
const isExternal = /^https?:/.test(url) const isExternal = EXTERNAL_URL_RE.test(url)
if (isExternal) { if (isExternal) {
Object.entries(externalAttrs).forEach(([key, val]) => { Object.entries(externalAttrs).forEach(([key, val]) => {
token.attrSet(key, val) token.attrSet(key, val)

Loading…
Cancel
Save