Skip to content

Commit f6ec86e

Browse files
committed
relative/absolute resolved urls will stay within the base url
1 parent 1d93405 commit f6ec86e

File tree

1 file changed

+38
-27
lines changed

1 file changed

+38
-27
lines changed

server/utils/changelog/markdown.ts

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -155,31 +155,31 @@ export function sanitizeRawHTML(rawHtml: string, mdRepoInfo: MarkdownRepoInfo) {
155155
if (attribs['data-level']) return { tagName: 'h6', attribs: attribs }
156156
return { tagName: 'h6', attribs: { ...attribs, 'data-level': '6' } }
157157
},
158-
// img: (tagName, attribs) => {
159-
// if (attribs.src) {
160-
// attribs.src = resolveImageUrl(attribs.src, packageName, repoInfo)
161-
// }
162-
// return { tagName, attribs }
163-
// },
164-
// source: (tagName, attribs) => {
165-
// if (attribs.src) {
166-
// attribs.src = resolveImageUrl(attribs.src, packageName, repoInfo)
167-
// }
168-
// if (attribs.srcset) {
169-
// attribs.srcset = attribs.srcset
170-
// .split(',')
171-
// .map(entry => {
172-
// const parts = entry.trim().split(/\s+/)
173-
// const url = parts[0]
174-
// if (!url) return entry.trim()
175-
// const descriptor = parts[1]
176-
// const resolvedUrl = resolveImageUrl(url, packageName, repoInfo)
177-
// return descriptor ? `${resolvedUrl} ${descriptor}` : resolvedUrl
178-
// })
179-
// .join(', ')
180-
// }
181-
// return { tagName, attribs }
182-
// },
158+
img: (tagName, attribs) => {
159+
if (attribs.src) {
160+
attribs.src = resolveUrl(attribs.src, mdRepoInfo)
161+
}
162+
return { tagName, attribs }
163+
},
164+
source: (tagName, attribs) => {
165+
if (attribs.src) {
166+
attribs.src = resolveUrl(attribs.src, mdRepoInfo)
167+
}
168+
if (attribs.srcset) {
169+
attribs.srcset = attribs.srcset
170+
.split(',')
171+
.map(entry => {
172+
const parts = entry.trim().split(/\s+/)
173+
const url = parts[0]
174+
if (!url) return entry.trim()
175+
const descriptor = parts[1]
176+
const resolvedUrl = resolveUrl(url, mdRepoInfo)
177+
return descriptor ? `${resolvedUrl} ${descriptor}` : resolvedUrl
178+
})
179+
.join(', ')
180+
}
181+
return { tagName, attribs }
182+
},
183183
a: (tagName, attribs) => {
184184
if (!attribs.href) {
185185
return { tagName, attribs }
@@ -250,11 +250,22 @@ function resolveUrl(url: string, repoInfo: MarkdownRepoInfo) {
250250
const baseUrl = isMarkdownFile ? repoInfo.blobBaseUrl : repoInfo.rawBaseUrl
251251
if (url.startsWith('./') || url.startsWith('../')) {
252252
// url constructor handles relative paths
253-
return new URL(url, `${baseUrl}/${repoInfo.path ?? ''}`).href
253+
return checkResolvedUrl(new URL(url, `${baseUrl}/${repoInfo.path ?? ''}`).href, baseUrl)
254254
}
255255
if (url.startsWith('/')) {
256-
return new URL(`${baseUrl}${url}`).href
256+
return checkResolvedUrl(new URL(`${baseUrl}${url}`).href, baseUrl)
257257
}
258258

259259
return url
260260
}
261+
262+
/**
263+
* check resolved url that it still contains the base url
264+
* @returns the resolved url if starting with baseUrl else baseUrl
265+
*/
266+
function checkResolvedUrl(resolved: string, baseUrl: string) {
267+
if (resolved.startsWith(baseUrl)) {
268+
return resolved
269+
}
270+
return baseUrl
271+
}

0 commit comments

Comments
 (0)