Files
test-repo/docs/.vitepress/theme/components/CopyOrDownloadAsMarkdownButtons/utils.js
T

36 lines
1.1 KiB
JavaScript
Raw Normal View History

2026-03-19 14:30:03 +08:00
var removeHtmlExtension = (pathSegment) => {
const lastSlashIndex = pathSegment.lastIndexOf('/')
const lastDotIndex = pathSegment.lastIndexOf('.')
if (
lastDotIndex > lastSlashIndex &&
lastDotIndex !== -1 &&
pathSegment.endsWith('.html')
) {
return pathSegment.slice(0, lastDotIndex)
}
return pathSegment
}
function cleanUrl(url) {
const { origin, pathname } = new URL(url)
const pathnameWithoutTrailingSlash = pathname.replace(/\/+$/, '')
if (pathname.length > 0) {
return origin + removeHtmlExtension(pathnameWithoutTrailingSlash)
}
return origin
}
function resolveMarkdownPageURL(url) {
const cleanedURL = cleanUrl(url)
return `${cleanedURL}/index.md`
}
function downloadFile(filename, content, blobType = 'text/plain') {
const blob =
content instanceof Blob ? content : new Blob([content], { type: blobType })
const url = URL.createObjectURL(blob)
Object.assign(document.createElement('a'), {
href: url,
download: filename
}).click()
URL.revokeObjectURL(url)
}
export { resolveMarkdownPageURL, downloadFile, cleanUrl }