From 8e0ba45789a81ee28f6c67468f7fcf9ea45832db Mon Sep 17 00:00:00 2001
From: Jacky Zhao <j.zhao2k19@gmail.com>
Date: Sun, 16 Jul 2023 17:39:35 +0000
Subject: [PATCH] add link resolution prompt to quartz create

---
 quartz/bootstrap-cli.mjs |   62 +++++++++++++++++++++++-------
 1 files changed, 47 insertions(+), 15 deletions(-)

diff --git a/quartz/bootstrap-cli.mjs b/quartz/bootstrap-cli.mjs
index b1503f2..c5d8403 100755
--- a/quartz/bootstrap-cli.mjs
+++ b/quartz/bootstrap-cli.mjs
@@ -9,6 +9,7 @@
 import fs from 'fs'
 import { intro, isCancel, outro, select, text } from '@clack/prompts'
 import { rimraf } from 'rimraf'
+import prettyBytes from 'pretty-bytes'
 
 const cacheFile = "./.quartz-cache/transpiled-build.mjs"
 const fp = "./quartz/build.ts"
@@ -53,6 +54,16 @@
     .trim()
 }
 
+function exitIfCancel(val) {
+
+  if (isCancel(val)) {
+    outro(chalk.red("Exiting"))
+    process.exit(0)
+  } else {
+    return val
+  }
+}
+
 yargs(hideBin(process.argv))
   .scriptName("quartz")
   .version(version)
@@ -60,8 +71,9 @@
   .command('create', 'Initialize Quartz', async (_argv) => {
     console.log()
     intro(chalk.bgGreen.black(` Quartz v${version} `))
-    const contentFolder = path.join(process.cwd(), "content")
-    const setupStrategy = await select({
+    const cwd = process.cwd()
+    const contentFolder = path.join(cwd, "content")
+    const setupStrategy = exitIfCancel(await select({
       message: `Choose how to initialize the content in \`${contentFolder}\``,
       options: [
         { value: 'new', label: "Empty Quartz" },
@@ -69,12 +81,17 @@
         { value: 'symlink', label: "Symlink an existing folder", hint: "don't select this unless you know what you are doing!" },
         { value: 'keep', label: "Keep the existing files" },
       ]
-    })
+    }))
 
-    if (isCancel(setupStrategy)) {
-      outro(chalk.red("Exiting"))
-      process.exit(0)
-    }
+    // TODO
+    const linkResolutionStrategy = exitIfCancel(await select({
+      message: `Choose how Quartz should resolve links in your content. You can change this later in \`quartz.config.ts\`.`,
+      options: [
+        { value: 'absolute', label: "Treat links as absolute path", hint: "for content made for Quartz 3 and Hugo" },
+        { value: 'shortest', label: "Treat links as shortest path", hint: "for most Obsidian vaults" },
+        { value: 'relative', label: "Treat links as relative paths", hint: "for just normal Markdown files" },
+      ]
+    }))
 
     async function rmContentFolder() {
       const contentStat = await fs.promises.lstat(contentFolder)
@@ -88,7 +105,7 @@
     }
 
     if (setupStrategy === 'copy' || setupStrategy === 'symlink') {
-      const originalFolder = escapePath(await text({
+      const originalFolder = escapePath(exitIfCancel(await text({
         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',
         validate(fp) {
@@ -99,12 +116,7 @@
             return "The given path is not a folder"
           }
         }
-      }))
-
-      if (isCancel(originalFolder)) {
-        outro(chalk.red("Exiting"))
-        process.exit(0)
-      }
+      })))
 
       await rmContentFolder()
       if (setupStrategy === 'copy') {
@@ -126,14 +138,26 @@
       )
     }
 
+    // now, do config changes
+    const configFilePath = path.join(cwd, "quartz.config.ts")
+    let configContent = await fs.promises.readFile(configFilePath, { encoding: 'utf-8' })
+    configContent = configContent.replace(/markdownLinkResolution: '(.+)'/, `markdownLinkResolution: '${linkResolutionStrategy}'`)
+    await fs.promises.writeFile(configFilePath, configContent)
+
     outro(`You're all set! Not sure what to do next? Try:
    • Customizing Quartz a bit more by editing \`quartz.config.ts\`
    • Running \`npx quartz build --serve\` to preview your Quartz locally
    • Hosting your Quartz online (see: https://quartz.jzhao.xyz/setup/hosting)
 `)
   })
+  .command('update', 'Get the latest Quartz updates', () => {
+    // TODO
+  })
+  .command('push', 'Push your Quartz updates to GitHub', () => {
+    // TODO
+  })
   .command('build', 'Build Quartz into a bundle of static HTML files', BuildArgv, async (argv) => {
-    await esbuild.build({
+    const result = await esbuild.build({
       entryPoints: [fp],
       outfile: path.join("quartz", cacheFile),
       bundle: true,
@@ -143,6 +167,8 @@
       jsx: "automatic",
       jsxImportSource: "preact",
       packages: "external",
+      metafile: true,
+      sourcemap: true,
       plugins: [
         sassPlugin({
           type: 'css-text',
@@ -186,6 +212,12 @@
       process.exit(1)
     })
 
+    if (argv.verbose) {
+      const outputFileName = 'quartz/.quartz-cache/transpiled-build.mjs'
+      const meta = result.metafile.outputs[outputFileName]
+      console.log(chalk.gray(`[debug] Successfully transpiled ${Object.keys(meta.inputs).length} files (${prettyBytes(meta.bytes)})`))
+    }
+
     const { default: init } = await import(cacheFile)
     init(argv, version)
   })

--
Gitblit v1.10.0