Skip to content

Commit 2fd6305

Browse files
committed
handle edge case
1 parent cb700a1 commit 2fd6305

2 files changed

Lines changed: 12 additions & 2 deletions

File tree

packages/theme/components/layout/code-block/code-transformer.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,13 @@ describe('codeTransformer', () => {
175175
expect(result).toBe(expectedCode)
176176
})
177177

178+
it('transforms URLs that only share the basePath prefix', () => {
179+
const sourceCode = '<img src="/docs2/image.png" alt="test" />'
180+
const expectedCode = '<img src="/docs/docs2/image.png" alt="test" />'
181+
const result = codeTransformer(sourceCode, basePath)
182+
expect(result).toBe(expectedCode)
183+
})
184+
178185
it('does not transform nested component with multiple dots when basePath is set', () => {
179186
const sourceCode = '<Gallery.Image.Item src="/gallery/nested.jpg" alt="nested" />'
180187
const expectedCode = '<Gallery.Image.Item src="/gallery/nested.jpg" alt="nested" />'

packages/theme/components/layout/code-block/code-transformer.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ export const codeTransformer = (sourceCode: string, basePath: string): string =>
22
if (!basePath) return sourceCode
33

44
// to skip external URLs and other irrelevant paths
5-
const shouldTransform = (assetPath: string) =>
6-
!assetPath.startsWith('http') && !assetPath.startsWith('//') && !assetPath.startsWith(basePath)
5+
const shouldTransform = (assetPath: string) => {
6+
const hasBasePath = assetPath === basePath || assetPath.startsWith(`${basePath}/`)
7+
8+
return !assetPath.startsWith('http') && !assetPath.startsWith('//') && !hasBasePath
9+
}
710

811
// normalise for absolute (/path) and relative (path) values
912
const transformAssetPath = (assetPath: string) =>

0 commit comments

Comments
 (0)