Skip to content

Commit

Permalink
added support for regex based internalDomains matching
Browse files Browse the repository at this point in the history
  • Loading branch information
pirog committed Oct 29, 2024
1 parent c982258 commit 617c550
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 9 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## {{ UNRELEASED_VERSION }} - [{{ UNRELEASED_DATE }}]({{ UNRELEASED_LINK }})

* Added `RegEx` support for `internalDomains` matching
* Added `is-faux-internal` helper util

## v1.1.0-beta.15 - [October 19, 2024](https://github.com/lando/vitepress-theme-default-plus/releases/tag/v1.1.0-beta.15)

* Fixed bug with `<VPLLink>` not dynamically updating and showing strange behavior for `prev|next` links
Expand Down
4 changes: 3 additions & 1 deletion components/VPLLink.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import {useData} from 'vitepress';
import {computed} from 'vue';
import {normalizeLink} from 'vitepress/dist/client/theme-default/support/utils.js';
import {default as checkIsFauxInternal} from '../utils/is-faux-internal';
const EXTERNAL_URL_RE = /^(?:[a-z]+:|\/\/)/i;
const {theme} = useData();
Expand Down Expand Up @@ -52,7 +54,7 @@ const props = defineProps({
const relation = computed(() => props.rel === 'mvb' ? 'alternate' : props.rel);
const tag = computed(() => props.tag ?? (props.href ? 'a' : 'span'));
const isFauxInternal = computed(() => props.href && internalDomains.find(domain => props.href.startsWith(domain)) !== undefined);
const isFauxInternal = computed(() => props.href && checkIsFauxInternal(props.href, internalDomains));
const isExternal = computed(() => !isFauxInternal.value && props.href && EXTERNAL_URL_RE.test(props.href));
const getLink = href => {
Expand Down
2 changes: 2 additions & 0 deletions config/landov3.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ export default function({base, landoPlugin, themeConfig, version}) {
},
internalDomain: [],
internalDomains: [
'http://localhost',
'https://localhost',
'http://docs.lando.dev',
'https://docs.lando.dev',
],
Expand Down
6 changes: 5 additions & 1 deletion docs/.vitepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ if (!isDevRelease(version)) {
export default defineConfig({
title: 'VitePress Theme +',
description: 'The VitePress default theme with some MOARPOWAH!',
// landoDocs: 3,
base: '/',
lang: 'en-US',
head: [
Expand Down Expand Up @@ -148,6 +147,11 @@ export default defineConfig({
},
ga: {id: 'G-ZSK3T9FTQ9'},
hubspot: {id: '6478338'},
internalDomains: [
'http://localhost',
'https://localhost',
new RegExp('^https:\/\/[a-zA-Z0-9-]+--vitepress-theme-default-plus\.netlify\.app(\/.*)?$'),
],
layouts: {
cats: './components/VPLCats.vue',
dogs: './components/VPLDogs.vue',
Expand Down
11 changes: 8 additions & 3 deletions docs/config/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,13 +286,18 @@ Once you have you should be able to use all the things below.

```js
internalDomains:
- 'http://docs.lando.dev'
- 'https://docs.lando.dev'
- 'http://localhost',
- 'https://localhost',
- 'http://vitepress-theme-default-plus.lando.dev/'
- 'https://vitepress-theme-default-plus.lando.dev/'
- new RegExp('^https:\/\/[a-zA-Z0-9-]+--vitepress-theme-default-plus\.netlify\.app(\/.*)?$'),
```

* Details:

This allows external links _starting with_ the specified `internalDomains` to be experentially treated like internal links.
This allows external links matchin `internalDomains` to be experentially treated like internal links.

If you use `string` it will match any path that `startsWith` the `internalDomain` or you can use a `RegEx`.

This is useful if you have multiple VitePress sites that are all tied together into a single domain experience a la Netlify's rewrite functionality.

Expand Down
6 changes: 2 additions & 4 deletions markdown/link-override-plugin.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import Debug from 'debug';
import {URL} from 'url';

import {default as isFauxInternal} from '../utils/is-faux-internal.js';

const indexRE = /(^|.*\/)index.md(#?.*)$/i;
const EXTERNAL_URL_RE = /^(?:[a-z]+:|\/\/)/i;

function isFauxInternal(path, domains = []) {
return domains.find(domain => path.startsWith(domain)) !== undefined;
}

function isExternal(path) {
return EXTERNAL_URL_RE.test(path);
}
Expand Down
10 changes: 10 additions & 0 deletions utils/is-faux-internal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

export default function(path, domains = []) {
// separate out strings and regex
const regexers = domains.filter(domain => domain instanceof RegExp) ?? [];
const stringers = domains.filter(domain => typeof domain === 'string') ?? [];

return false
|| stringers.find(domain => path.startsWith(domain)) !== undefined
|| regexers.map(regexer => regexer.test(path)).find(matched => matched === true) !== undefined;
}
1 change: 1 addition & 0 deletions vitepress-theme-default-plus.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import VPLYouTube from './components/VPLYouTube.vue';
// composables
export {default as isActive} from './client/is-active.js';
export {default as isDevRelease} from './utils/is-dev-release.js';
export {default as isFauxInternal} from './utils/is-faux-internal.js';
export {default as encodeTag} from './client/encode-tag.js';
export {default as useCollection} from './client/use-collection.js';
export {default as useTags} from './client/use-tags.js';
Expand Down

0 comments on commit 617c550

Please sign in to comment.