Last active
October 12, 2024 10:05
-
-
Save dustinknopoff/0913e25d059f111f57045c904de25980 to your computer and use it in GitHub Desktop.
This is written expecting to be in the top level directory of a Zola project and can be run `deno run --allow-read=. --allow-write=. migrateToTaxonomies.ts`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
extract, | |
test as containsFrontmatter, | |
} from "https://deno.land/[email protected]/encoding/front_matter/any.ts"; | |
import { walk } from "https://deno.land/[email protected]/fs/mod.ts"; | |
import { stringify } from "npm:[email protected]" | |
async function writeFile(path: string, attrs: { [key: string]: any }, body: string) { | |
await Deno.writeTextFile(path, `---\n${stringify(attrs)}\n---\n\n${body}`) | |
} | |
const permittedTopLevelKeys = new Set(["title", "description", "updated", "weight", "draft", "slug", "path", "aliases", "in_search_index", "template", "taxonomies", "extra", "date"]) | |
const taxonomies = new Set(["tags"]) | |
function difference<T>(setA: Set<T>, setB: Set<T>): Set<T> { | |
const _difference = new Set(setA); | |
for (const elem of setB) { | |
_difference.delete(elem); | |
} | |
return _difference; | |
} | |
for await (const entry of walk("./content/articles", { includeDirs: false })) { | |
if (!entry.path.includes("_index")) { | |
console.log(entry.path); | |
const str = await Deno.readTextFile(entry.path); | |
let post; | |
if (containsFrontmatter(str)) { | |
post = extract(str); | |
} else { | |
post = { body: str, attrs: {} } | |
} | |
if (!post.attrs.extra) { | |
post.attrs.extra = {} | |
} | |
if (!post.attrs.taxonomies) { | |
post.attrs.taxonomies = {} | |
} | |
const diff = difference(new Set(Object.keys(post.attrs)), permittedTopLevelKeys) | |
if (diff.size > 0) { | |
for (const elem of diff) { | |
if (taxonomies.has(elem)) { | |
post.attrs.taxonomies[elem] = post.attrs[elem] | |
} else { | |
post.attrs.extra[elem] = post.attrs[elem] | |
} | |
delete post.attrs[elem] | |
} | |
} | |
await writeFile(entry.path, post.attrs, post.body) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the upgrade. I still have issues with the script, as regular working posts fail.
I issued a PR on deno