Skip to content

Commit

Permalink
Merge pull request #1084 from alan-cole/feature/strip-site-id-util
Browse files Browse the repository at this point in the history
feat(@dpc-sdp/ripple-tide-api): add utility to strip site id
  • Loading branch information
waitingallday authored Apr 16, 2024
2 parents d51b319 + 85f9930 commit ca8be1f
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 12 deletions.
6 changes: 3 additions & 3 deletions examples/nuxt-app/components/global/VsbaMapPopupContent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@
</RplContent>

<p class="rpl-type-p-small rpl-u-margin-t-3">
<RplTextLink :url="formatUrl(feature.url[0])">
<RplTextLink :url="stripSiteId(feature.url[0])">
View {{ feature.title[0] }}
</RplTextLink>
</p>
</div>
</template>

<script setup lang="ts">
import { stripSiteId } from '@dpc-sdp/ripple-tide-api'
interface Props {
feature: any
}
const props = withDefaults(defineProps<Props>(), {})
const formatUrl = (str) => str.replace(/\/site-(\d+)/, '')
</script>

<style>
Expand Down
5 changes: 3 additions & 2 deletions packages/nuxt-ripple/utils/breadcrumbs.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { stripSiteId } from '@dpc-sdp/ripple-tide-api'

// Private helpers
const siteMatch = /\/site-(\d+)/
function getActivePath(branch, path) {
for (let i = 0; i < branch.length; i++) {
const item = branch[i]
// ignore site id prefix when matching active path
if (item.url.replace(siteMatch, '') === path.replace(siteMatch, '')) {
if (stripSiteId(item.url) === stripSiteId(path)) {
return [
{
text: item.text,
Expand Down
1 change: 1 addition & 0 deletions packages/ripple-tide-api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export { default as TidePageApi } from './services/tide-page.js'
export { default as TideSiteApi } from './services/tide-site.js'
export { default as logger } from './logger/logger.js'
export * from './utils/createHandler.js'
export { stripSiteId } from './utils/stripSiteId.js'

export {
addAnchorLinksToHTML,
Expand Down
14 changes: 14 additions & 0 deletions packages/ripple-tide-api/src/utils/stripSiteId.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { expect, describe, it } from '@jest/globals'

import { stripSiteId } from './stripSiteId.js'

describe('stripSiteId(url, replace)', () => {
it(`returns url without site prefix`, () => {
expect(stripSiteId('/site-8888/demo')).toEqual('/demo')
})

it(`returns undefined if param is null / undefined`, () => {
expect(stripSiteId(null)).toEqual(undefined)
expect(stripSiteId(undefined)).toEqual(undefined)
})
})
5 changes: 5 additions & 0 deletions packages/ripple-tide-api/src/utils/stripSiteId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export function stripSiteId(url, replace = '') {
return url?.replace(/\/site-(\d+)/, replace)
}

export default stripSiteId
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import {
IContentCollectionSort
} from '../../../mapping/components/content-collection/content-collection-mapping'
import type { IRplFeatureFlags } from '@dpc-sdp/ripple-tide-api/types'
import { stripSiteId } from '@dpc-sdp/ripple-tide-api'
const { public: config } = useRuntimeConfig()
Expand Down Expand Up @@ -97,7 +98,7 @@ const searchResultsMappingFn = (item): any => {
props: {
el: 'li',
title: item.title?.raw?.[0],
url: item.url?.raw?.[0].replace(/\/site-(\d+)/, $app_origin || ''),
url: stripSiteId(item.url?.raw?.[0], $app_origin || ''),
image:
props.display.style === 'thumbnail' && rawImage
? { src: rawImage }
Expand Down
3 changes: 2 additions & 1 deletion packages/ripple-tide-search/components/TideSearchPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { FormKit } from '@formkit/vue'
import { SearchDriverOptions } from '@elastic/search-ui'
import { useRippleEvent } from '@dpc-sdp/ripple-ui-core'
import type { rplEventPayload } from '@dpc-sdp/ripple-ui-core'
import { stripSiteId } from '@dpc-sdp/ripple-tide-api'
interface Props {
id: string
Expand Down Expand Up @@ -50,7 +51,7 @@ const props = withDefaults(defineProps<Props>(), {
component: 'TideAppSearchResult',
props: {
title: item.title?.raw?.[0],
url: item.url?.raw?.[0].replace(/\/site-(\d+)/, ''),
url: stripSiteId(item.url?.raw?.[0]),
updated: item.changed?.raw?.[0]
}
}
Expand Down
6 changes: 2 additions & 4 deletions packages/ripple-tide-search/composables/useSearchResult.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { computed } from 'vue'
import { getSearchResultValue, truncateText } from '#imports'
import { stripSiteId } from '@dpc-sdp/ripple-tide-api'

interface ResultOptions {
summaryMaxLength: number | null
Expand All @@ -15,10 +16,7 @@ export default (result, options: ResultOptions = { summaryMaxLength: 150 }) => {
return externalURL
}

return getSearchResultValue(result, 'url').replace(
/\/site-(\d+)/,
$app_origin || ''
)
return stripSiteId(getSearchResultValue(result, 'url'), $app_origin || '')
})
const updated = computed(() => {
const rawDate = getSearchResultValue(result, 'changed')
Expand Down
3 changes: 2 additions & 1 deletion packages/ripple-tide-search/pages/search.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<script setup lang="ts">
import { stripSiteId } from '@dpc-sdp/ripple-tide-api'
import { AppSearchFilterConfigItem, MappedSearchResult } from '../types'
import { formatDate, useRuntimeConfig, useAppConfig } from '#imports'
Expand Down Expand Up @@ -90,7 +91,7 @@ const searchResultsMappingFn = (item): MappedSearchResult<any> => {
component: 'TideAppSearchResult',
props: {
title: item.title?.raw?.[0],
url: item.url?.raw?.[0].replace(/\/site-(\d+)/, $app_origin || ''),
url: stripSiteId(item.url?.raw?.[0], $app_origin || ''),
content: summaryField,
updated: rawUpdated ? formatDate(rawUpdated) : ''
}
Expand Down

0 comments on commit ca8be1f

Please sign in to comment.