fix path parsing

This commit is contained in:
Jacky Zhao 2023-07-06 16:56:30 -07:00
parent c9eecf813a
commit a80ddd0de8
2 changed files with 22 additions and 13 deletions

View File

@ -45,6 +45,14 @@ export const BuildArgv = {
} }
} }
function escapePath(fp) {
return fp
.replace(/\\ /g, " ") // unescape spaces
.replace(/^".*"$/, "$1")
.replace(/^'.*"$/, "$1")
.trim()
}
yargs(hideBin(process.argv)) yargs(hideBin(process.argv))
.scriptName("quartz") .scriptName("quartz")
.version(version) .version(version)
@ -69,30 +77,29 @@ yargs(hideBin(process.argv))
} }
async function rmContentFolder() { async function rmContentFolder() {
if (fs.existsSync(contentFolder)) { const contentStat = await fs.promises.lstat(contentFolder)
const contentStat = await fs.promises.lstat(contentFolder) if (contentStat) {
if (contentStat.isSymbolicLink()) { if (contentStat.isSymbolicLink()) {
await fs.promises.unlink(contentFolder) await fs.promises.unlink(contentFolder)
} else { } else {
await rimraf(contentFolder) await rimraf(contentFolder)
} }
} }
await fs.promises.mkdir(contentFolder)
} }
if (setupStrategy === 'copy' || setupStrategy === 'symlink') { if (setupStrategy === 'copy' || setupStrategy === 'symlink') {
const originalFolder = await text({ const originalFolder = escapePath(await text({
message: "Enter the full path to existing content folder", message: "Enter the full path to existing content folder",
placeholder: 'On most terminal emulators, you can drag and drop a folder into the window and it will paste the full path', placeholder: 'On most terminal emulators, you can drag and drop a folder into the window and it will paste the full path',
validate(fp) { validate(fp) {
if (!fs.existsSync(fp)) { const fullPath = escapePath(fp)
if (!fs.existsSync(fullPath)) {
return "The given path doesn't exist" return "The given path doesn't exist"
} else if (!fs.lstatSync(fp).isDirectory()) { } else if (!fs.lstatSync(fullPath).isDirectory()) {
return "The given path is not a folder" return "The given path is not a folder"
} }
} }
}) }))
if (isCancel(originalFolder)) { if (isCancel(originalFolder)) {
outro(chalk.red("Exiting")) outro(chalk.red("Exiting"))
@ -101,14 +108,15 @@ yargs(hideBin(process.argv))
await rmContentFolder() await rmContentFolder()
if (setupStrategy === 'copy') { if (setupStrategy === 'copy') {
await fs.promises.cp(originalFolder, contentFolder) await fs.promises.cp(originalFolder, contentFolder, { recursive: true })
} else if (setupStrategy === 'symlink') { } else if (setupStrategy === 'symlink') {
await fs.promises.symlink(originalFolder, contentFolder) await fs.promises.symlink(originalFolder, contentFolder, 'dir')
} }
} else if (setupStrategy === 'new') { } else if (setupStrategy === 'new') {
await rmContentFolder() await rmContentFolder()
await fs.promises.mkdir(contentFolder)
await fs.promises.writeFile(path.join(contentFolder, "index.md"), await fs.promises.writeFile(path.join(contentFolder, "index.md"),
`--- `---
title: Welcome to Quartz title: Welcome to Quartz
--- ---

View File

@ -220,6 +220,7 @@ export const ObsidianFlavoredMarkdown: QuartzTransformerPlugin<Partial<Options>
} }
const text = firstChild.children[0].value const text = firstChild.children[0].value
const restChildren = firstChild.children.splice(1)
const [firstLine, ...remainingLines] = text.split("\n") const [firstLine, ...remainingLines] = text.split("\n")
const remainingText = remainingLines.join("\n") const remainingText = remainingLines.join("\n")
@ -253,7 +254,7 @@ export const ObsidianFlavoredMarkdown: QuartzTransformerPlugin<Partial<Options>
children: [{ children: [{
type: 'text', type: 'text',
value: remainingText, value: remainingText,
}] }, ...restChildren]
}) })
} }