-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdev.ts
119 lines (93 loc) · 3.08 KB
/
dev.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import chokidar from "chokidar"
import fs, { copyFileSync } from "node:fs"
import path from "node:path"
import { rimrafSync } from "rimraf"
import matter from "gray-matter"
import { extractImageSources } from "./src/lib/extract-image-sources"
import { resolveLinks } from "./src/lib/resolve-links"
import { addFilepath } from "./src/lib/add-filepath"
import { stripHashFromTags } from "./src/lib/strip-hash-from-tags"
const obsidianPath = resolveHome(
`${process.env["OBSIDIAN_PATH"] || "~/obsidian"}`
)
function resolveHome(filepath: string): string {
if (filepath[0] === "~") {
return path.join(process.env.HOME!, filepath.slice(1))
}
return filepath
}
chokidar.watch(obsidianPath).on("change", (filePath, stats) => {
// Ignore files that starts with .
if (filePath.includes("/.")) {
return
}
// Ignore non md/mdx/sdx files
if (
!filePath.endsWith(".md") &&
!filePath.endsWith(".mdx") &&
!filePath.endsWith(".sdx")
) {
return
}
// Ignore Untitled files, they are probably temporary
if (filePath.includes("/Untitled")) {
return
}
const file = matter.read(filePath)
if (
!(
file?.data?.date &&
file?.data?.title &&
String(file?.data?.publish) === "true"
)
) {
return
}
// Copy file to src/content/second-brain/local/**
const destinationPath = `./src/content/second-brain/local/${filePath.replace(
obsidianPath,
""
)}`.replaceAll("//", "/")
const destinationDir = path.dirname(destinationPath)
// Create the destination directory if it doesn't exist
if (!fs.existsSync(destinationDir)) {
fs.mkdirSync(destinationDir, { recursive: true })
}
copyFileSync(filePath, destinationPath)
console.log("Copied", filePath, "to", destinationPath)
// Edit file and add local- to slug in the frontmatter to prevent collision
const fileContent = fs.readFileSync(destinationPath, {
encoding: "utf-8",
})
const fileContentWithLocal = fileContent.replace(
/(?<=^slug: )(.*)/gm,
"local-$1"
)
fs.writeFileSync(destinationPath, fileContentWithLocal, {
encoding: "utf-8",
})
// Parse the file with remark, and then get image paths
// Copy those images to public/images/**
const imageSources = extractImageSources(filePath)
imageSources.forEach((imageSource) => {
const srcPath = `${obsidianPath}/${imageSource}`
const destinationPath = `./public/images/${imageSource}`
const destinationDir = path.dirname(destinationPath)
// Create the destination directory if it doesn't exist
if (!fs.existsSync(destinationDir)) {
fs.mkdirSync(destinationDir, { recursive: true })
}
copyFileSync(srcPath, destinationPath)
console.log("Copied", srcPath, "to", destinationPath)
})
resolveLinks(destinationPath)
addFilepath(destinationPath)
stripHashFromTags(destinationPath)
})
// Cleanup src/content/second-brain/local on exit
process.on("SIGINT", () => {
console.log("Cleaning up src/content/second-brain/local/")
rimrafSync("./src/content/second-brain/local/*", { glob: true })
process.exit()
})
console.log(`Watching ${obsidianPath} for changes`)