Skip to content

Commit

Permalink
support two different base URLs, for operation in both Github Pages &…
Browse files Browse the repository at this point in the history
… Cascade
  • Loading branch information
wbbaker committed Oct 15, 2024
1 parent 6099f01 commit 808af0d
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 11 deletions.
5 changes: 2 additions & 3 deletions src/components/Layout/LayoutHeader.vue
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,16 @@
<script setup>
import SwitchTheme from '../SwitchTheme.vue'
import NavbarMobile from '../Navbar/NavbarMobile.vue'
import { isValidUrl } from '@/utils/url'
import { isValidUrl, getBaseUrl } from '@/utils/url'
const {
header_links,
header_logo_text,
header_logo_url,
base_url,
project_name
} = __APP_ENV__
const logoUrl = isValidUrl(header_logo_url)
? header_logo_url
: (base_url + header_logo_url).replace('//', '/')
: (getBaseUrl() + header_logo_url).replace('//', '/')
</script>
9 changes: 4 additions & 5 deletions src/plugins/markdown/relativeToRouter.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { getBaseUrl } from "../../utils/url"

export function relativeToRouterPlugin(md, configuration) {
const isStaticAsset = (url) => {
const regex = /\.\w+$/

return regex.test(url)
}

const makeUrlToStaticAsset = (url) => {
const baseUrl = configuration.base_url || ''

return (baseUrl + url).replaceAll('//', '/')
}
const makeUrlToStaticAsset = (url) =>
(getBaseUrl(configuration) + url).replaceAll('//', '/')

const scan = (state) => {
state.tokens.forEach((tokens) => {
Expand Down
6 changes: 4 additions & 2 deletions src/plugins/schemaOrg/nodes/Taxon.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { getBaseUrl } from "@/utils/url"

function removeEmptyProperties(obj) {
const copyObj = { ...obj }

Expand All @@ -13,8 +15,8 @@ function removeEmptyProperties(obj) {
}

function makeUrlPath(host, path) {
const { hash_mode, base_url = '' } = __APP_ENV__
const fullPath = (base_url + (hash_mode ? '/#' + path : path)).replaceAll(
const { hash_mode } = __APP_ENV__
const fullPath = (getBaseUrl() + (hash_mode ? '/#' + path : path)).replaceAll(
'//',
'/'
)
Expand Down
4 changes: 3 additions & 1 deletion src/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from 'vue-router'

import dynamicRoutes from '~pages'
import { getBaseUrl } from "@/utils/url"

const coreModuleRoutes = import.meta.glob('@/modules/**/router/*.js', {
import: 'default',
Expand All @@ -16,7 +17,8 @@ const userModuleRoutes = import.meta.glob('#/modules/**/router/*.js', {
eager: true
})

const { base_url, hash_mode } = __APP_ENV__
const { hash_mode } = __APP_ENV__
const base_url = getBaseUrl()

const moduleRoutes = [].concat(
...Object.values(coreModuleRoutes),
Expand Down
1 change: 1 addition & 0 deletions src/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './color'
export * from './request'
export * from './strings'
export * from './url'
10 changes: 10 additions & 0 deletions src/utils/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,13 @@ export function isValidUrl(string) {
return false
}
}

// Allow base_url to be different on Github Pages than when deployed elsewhere, for example to Cascade.
// GithubPages requires the base_url to be the repository name, while Cascade needs the page to
// fit into its own directory structure.
export const getBaseUrl = (configuration = __APP_ENV__) => {
const baseUrl = configuration?.base_url || ''
const githubPagesBaseUrl = configuration?.github_pages_base_url || ''
const isGithubPages = window.location.origin.indexOf('.github.io/') > 0
return isGithubPages ? githubPagesBaseUrl : baseUrl
}

0 comments on commit 808af0d

Please sign in to comment.