Skip to content

Commit 68b3019

Browse files
committed
fix: view as markdown opens .md URL, copy page uses mdream
1 parent 2d7e6b1 commit 68b3019

File tree

2 files changed

+60
-22
lines changed

2 files changed

+60
-22
lines changed

packages/nimiq-vitepress-theme/src/composables/useSourceCode.ts

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,25 @@ export function useSourceCode() {
112112
return window.location.href
113113
}
114114

115+
const getMarkdownUrl = () => {
116+
if (typeof window === 'undefined')
117+
return ''
118+
119+
const url = new URL(window.location.href)
120+
const pathname = url.pathname
121+
122+
// If it's a clean URL (no .html), append .md
123+
// Handle index pages: /path/ -> /path/index.md
124+
if (pathname.endsWith('/')) {
125+
url.pathname = `${pathname}index.md`
126+
}
127+
else {
128+
url.pathname = `${pathname}.md`
129+
}
130+
131+
return url.href
132+
}
133+
115134
const showSourceCode = computed(() => {
116135
if (!isSupported.value)
117136
return false
@@ -264,31 +283,13 @@ export function useSourceCode() {
264283
return `https://claude.ai/new?q=${encodedMessage}`
265284
})
266285

267-
const viewAsMarkdown = async () => {
286+
const viewAsMarkdown = () => {
268287
if (typeof window === 'undefined')
269288
return
270289

271-
try {
272-
// Get the main content container
273-
const contentEl = document.querySelector('.vp-doc') || document.querySelector('main')
274-
if (!contentEl) {
275-
throw new Error('Could not find page content')
276-
}
277-
278-
// Convert HTML to markdown using mdream
279-
const markdown = await mdream(contentEl.innerHTML)
280-
281-
// Create a blob and open it in a new window
282-
const blob = new Blob([markdown], { type: 'text/markdown' })
283-
const url = URL.createObjectURL(blob)
284-
window.open(url, '_blank', 'noopener,noreferrer')
285-
286-
// Clean up the blob URL after a delay
287-
setTimeout(() => URL.revokeObjectURL(url), 1000)
288-
}
289-
catch (error) {
290-
console.error('Failed to view as markdown:', error)
291-
toast.error('Failed to open markdown view')
290+
const markdownUrl = getMarkdownUrl()
291+
if (markdownUrl) {
292+
window.open(markdownUrl, '_blank', 'noopener,noreferrer')
292293
}
293294
}
294295

packages/nimiq-vitepress-theme/src/index.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import type { NimiqVitepressThemeConfig } from 'nimiq-vitepress-theme/types.js'
22
import type { UserConfig } from 'vitepress'
3+
import { copyFileSync, mkdirSync, readdirSync } from 'node:fs'
4+
import { join, relative } from 'node:path'
5+
import consola from 'consola'
36
import { defineConfig } from 'vitepress'
47
import inlineActionsPlugin from './vite/inline-actions-plugin'
58

@@ -21,6 +24,7 @@ export function defineNimiqVitepressConfig<T = NimiqVitepressThemeConfig>(config
2124
// Merge user's preConfig with default if provided
2225
const userMarkdown = config.markdown || {}
2326
const userPreConfig = userMarkdown.preConfig
27+
const userBuildEnd = config.buildEnd
2428

2529
return defineConfig<T>({
2630
extends: defaultConfig,
@@ -37,5 +41,38 @@ export function defineNimiqVitepressConfig<T = NimiqVitepressThemeConfig>(config
3741
}
3842
},
3943
},
44+
buildEnd(siteConfig) {
45+
const srcDir = siteConfig.srcDir
46+
const outDir = siteConfig.outDir
47+
48+
function copyMarkdownFiles(dir: string) {
49+
const entries = readdirSync(dir, { withFileTypes: true })
50+
51+
for (const entry of entries) {
52+
const fullPath = join(dir, entry.name)
53+
54+
if (entry.isDirectory()) {
55+
copyMarkdownFiles(fullPath)
56+
}
57+
else if (entry.isFile() && entry.name.endsWith('.md')) {
58+
const relativePath = relative(srcDir, fullPath)
59+
const destPath = join(outDir, relativePath)
60+
const destDir = join(destPath, '..')
61+
62+
mkdirSync(destDir, { recursive: true })
63+
copyFileSync(fullPath, destPath)
64+
}
65+
}
66+
}
67+
68+
consola.info('Copying markdown files to build output...')
69+
copyMarkdownFiles(srcDir)
70+
consola.success('Markdown files copied successfully')
71+
72+
// Call user's buildEnd if provided
73+
if (userBuildEnd) {
74+
userBuildEnd(siteConfig)
75+
}
76+
},
4077
})
4178
}

0 commit comments

Comments
 (0)