link processing

This commit is contained in:
Jacky Zhao 2023-05-31 17:41:44 -04:00
parent 2d3b816c44
commit e5f95504e1
5 changed files with 13 additions and 15 deletions

View File

@ -11,6 +11,7 @@ export default buildQuartz({
}, },
plugins: { plugins: {
transformers: [ transformers: [
new LinkProcessing(),
new FrontMatter(), new FrontMatter(),
new GitHubFlavoredMarkdown(), new GitHubFlavoredMarkdown(),
new Katex(), new Katex(),
@ -18,7 +19,6 @@ export default buildQuartz({
new CreatedModifiedDate({ new CreatedModifiedDate({
priority: ['frontmatter', 'filesystem'] // you can add 'git' here for last modified from Git but this makes the build slower priority: ['frontmatter', 'filesystem'] // you can add 'git' here for last modified from Git but this makes the build slower
}), }),
new LinkProcessing()
], ],
filters: [ filters: [
new RemoveDrafts() new RemoveDrafts()

View File

@ -59,7 +59,7 @@ export function buildQuartz(cfg: QuartzConfig) {
const filePaths = fps.map(fp => `${argv.directory}${path.sep}${fp}`) const filePaths = fps.map(fp => `${argv.directory}${path.sep}${fp}`)
const parsedFiles = await parseMarkdown(processor, argv.directory, filePaths, argv.verbose) const parsedFiles = await parseMarkdown(processor, argv.directory, filePaths, argv.verbose)
const filteredContent = filterContent(cfg.plugins.filters, parsedFiles, argv.verbose) const filteredContent = filterContent(cfg.plugins.filters, parsedFiles, argv.verbose)
await emitContent(output, cfg, filteredContent, argv.verbose) await emitContent(argv.directory, output, cfg, filteredContent, argv.verbose)
console.log(chalk.green(`Done in ${perf.timeSince()}`)) console.log(chalk.green(`Done in ${perf.timeSince()}`))
if (argv.serve) { if (argv.serve) {

View File

@ -12,7 +12,6 @@ export function slugify(s: string): string {
const slugParts: string = rawSlugSegments const slugParts: string = rawSlugSegments
.map((segment) => slugSegment(segment)) .map((segment) => slugSegment(segment))
.join(path.posix.sep) .join(path.posix.sep)
// .replace(/index$/, '')
.replace(/\/$/, '') .replace(/\/$/, '')
return path.normalize(slugParts) + sluggedAnchor return path.normalize(slugParts) + sluggedAnchor
} }

View File

@ -29,7 +29,7 @@ export class LinkProcessing extends QuartzTransformerPlugin {
markdownPlugins(): PluggableList { markdownPlugins(): PluggableList {
return [[remarkWikiLink, { return [[remarkWikiLink, {
pathFormat: this.opts.markdownLinkResolution === "absolute" ? 'obsidian-absolute' : 'raw' pathFormat: this.opts.markdownLinkResolution === "absolute" ? 'obsidian-absolute' : 'raw',
}]] }]]
} }
@ -38,7 +38,7 @@ export class LinkProcessing extends QuartzTransformerPlugin {
return (tree, file) => { return (tree, file) => {
const curSlug = file.data.slug! const curSlug = file.data.slug!
const transformLink = (target: string) => { const transformLink = (target: string) => {
const targetSlug = slugify(decodeURI(target)) const targetSlug = slugify(decodeURI(target).trim())
if (this.opts.markdownLinkResolution === 'relative' && !path.isAbsolute(targetSlug)) { if (this.opts.markdownLinkResolution === 'relative' && !path.isAbsolute(targetSlug)) {
return './' + relative(curSlug, targetSlug) return './' + relative(curSlug, targetSlug)
} else { } else {
@ -46,8 +46,8 @@ export class LinkProcessing extends QuartzTransformerPlugin {
} }
} }
// rewrite all links
visit(tree, 'element', (node, _index, _parent) => { visit(tree, 'element', (node, _index, _parent) => {
// rewrite all links
if ( if (
node.tagName === 'a' && node.tagName === 'a' &&
node.properties && node.properties &&
@ -60,14 +60,13 @@ export class LinkProcessing extends QuartzTransformerPlugin {
node.properties.href = transformLink(node.properties.href) node.properties.href = transformLink(node.properties.href)
} }
// rewrite link internals if prettylinks is on
if (this.opts.prettyLinks && node.children.length === 1 && node.children[0].type === 'text') { if (this.opts.prettyLinks && node.children.length === 1 && node.children[0].type === 'text') {
node.children[0].value = path.basename(node.children[0].value) node.children[0].value = path.basename(node.children[0].value)
} }
} }
})
// transform all images // transform all images
visit(tree, 'element', (node, _index, _parent) => {
if ( if (
node.tagName === 'img' && node.tagName === 'img' &&
node.properties && node.properties &&
@ -75,7 +74,7 @@ export class LinkProcessing extends QuartzTransformerPlugin {
) { ) {
if (!isAbsoluteUrl(node.properties.src)) { if (!isAbsoluteUrl(node.properties.src)) {
const ext = path.extname(node.properties.src) const ext = path.extname(node.properties.src)
node.properties.src = transformLink("/assets/" + node.properties.src) + ext node.properties.src = transformLink(path.join("assets", node.properties.src)) + ext
} }
} }
}) })

View File

@ -8,7 +8,7 @@ import { ProcessedContent } from "../plugins/vfile"
import { QUARTZ, slugify } from "../path" import { QUARTZ, slugify } from "../path"
import { globbyStream } from "globby" import { globbyStream } from "globby"
export async function emitContent(output: string, cfg: QuartzConfig, content: ProcessedContent[], verbose: boolean) { export async function emitContent(contentFolder: string, output: string, cfg: QuartzConfig, content: ProcessedContent[], verbose: boolean) {
const perf = new PerfTimer() const perf = new PerfTimer()
@ -39,11 +39,11 @@ export async function emitContent(output: string, cfg: QuartzConfig, content: Pr
// glob all non MD/MDX/HTML files in content folder and copy it over // glob all non MD/MDX/HTML files in content folder and copy it over
const assetsPath = path.join("public", "assets") const assetsPath = path.join("public", "assets")
for await (const fp of globbyStream("**", { for await (const fp of globbyStream("**", {
ignore: ["**/*.{md,mdx,html}"], ignore: ["**/*.md"],
cwd: "./content", cwd: contentFolder,
})) { })) {
const ext = path.extname(fp as string) const ext = path.extname(fp as string)
const src = path.join("content", fp as string) const src = path.join(contentFolder, fp as string)
const dest = path.join(assetsPath, slugify(fp as string) + ext) const dest = path.join(assetsPath, slugify(fp as string) + ext)
const dir = path.dirname(dest) const dir = path.dirname(dest)
await fs.promises.mkdir(dir, { recursive: true }) // ensure dir exists await fs.promises.mkdir(dir, { recursive: true }) // ensure dir exists