Skip to content

Instantly share code, notes, and snippets.

@joshcanhelp
Last active September 22, 2024 01:58
Show Gist options
  • Save joshcanhelp/00fc9719e766881c16b880d42b702ad5 to your computer and use it in GitHub Desktop.
Save joshcanhelp/00fc9719e766881c16b880d42b702ad5 to your computer and use it in GitHub Desktop.
import { readFileSync, writeFileSync } from "fs";
import { DateTime } from "luxon";
import { stringify } from "yaml";
// Subfolder in the vault
const PATH_TO_FOLDER = "/Users/joshcanhelp/Notes/Log/Day One/";
// Day One tags as keys, Obsidian tags as values
const tagMap = {
FromTag1: "to-tag-1",
FromTag2: "to-tag-2",
FromTag3: "to-tag-3",
};
// Fix up location names or return as-is
const mapLocation = (location) => {
const { localityName, placeName, administrativeArea } = location;
if (placeName.includes("38th Ave W")) {
return "983 38th Ave W";
}
if (placeName.includes("Sukhumvit")) {
return "Rung Rueng Noodle";
}
if (
(placeName.includes("701 Broadway"),
placeName.includes("456 Canyon Dr"),
placeName.includes("A Random Store"))
) {
return `${localityName}, ${administrativeArea}`;
}
return placeName;
};
// File > Export > JSON
const journal = JSON.parse(
readFileSync("./Journal.json", { encoding: "utf8" }),
);
const unslash = (str) =>
str
.replace(/\\\-/g, "-")
.replace(/\\\(/g, "(")
.replace(/\\\]/g, "]")
.replace(/\\\[/g, "[")
.replace(/\\\)/g, ")")
.replace(/\\\!/g, "!")
.replace(/\\\+/g, "+")
.replace(/\\\./g, ".")
.replace(/\\\//g, "-");
for (const entry of journal.entries) {
const frontMatter = {
places: [],
uuid: entry.uuid,
device: `${entry.creationDevice} (${entry.creationOSName || "unknown OS"} ${entry.creationOSVersion || "unknown version"})`,
};
if (entry.weather) {
frontMatter.weather = entry.weather.conditionsDescription;
frontMatter.temperature =
Math.round((entry.weather.temperatureCelsius * 9) / 5 + 32) + "°F";
}
const entryPhotos = {};
if (entry.photos) {
for (const photo of entry.photos) {
entryPhotos[photo.identifier] = `${photo.md5}.${photo.type}`;
}
}
let entryText = entry.text;
const imgRegex = /!\[\]\(dayone-moment:\/\/([a-fA-F0-9]+)\)/g;
const imgMatches = entry.text.matchAll(imgRegex);
for (const match of imgMatches) {
entryText = entryText.replace(
match[0],
`![[_files/Day One/${entryPhotos[match[1]]}]]`,
);
}
let entryLineOne = entryText.split("\n")[0].trim();
if (entryLineOne[0] === "#") {
entryLineOne =
` - ` +
unslash(entryLineOne.replace("# ", "").replace(":", "")).replace(
/\//g,
"-",
);
entryText = unslash(entryText.split("\n").slice(1).join("\n"));
} else {
entryLineOne = "";
}
const fileName =
DateTime.fromISO(entry.creationDate)
.setZone(entry.timeZone)
.toFormat("yyyy-MM-dd'T'HH-mm-ss") +
entryLineOne +
".md";
if (entry.tags) {
frontMatter.tags = [];
for (const tag of entry.tags) {
if (tagMap[tag]) {
frontMatter.tags.push(tagMap[tag]);
}
}
}
if (entry.location) {
if (mapLocation(entry.location)) {
const locNoteName = mapLocation(entry.location);
frontMatter.places.push(`[[Places/${locNoteName}|${locNoteName}]]`);
frontMatter.lat = entry.location.latitude;
frontMatter.long = entry.location.longitude;
}
}
const fileContent = `---
${stringify(frontMatter)}
---
${entryText}
`;
writeFileSync(PATH_TO_FOLDER + fileName, fileContent);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment