Skip to content

Instantly share code, notes, and snippets.

@dustinknopoff
Last active January 8, 2023 19:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dustinknopoff/0913e25d059f111f57045c904de25980 to your computer and use it in GitHub Desktop.
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`
import {
extract,
test as containsFrontmatter,
} from "https://deno.land/std@0.170.0/encoding/front_matter/any.ts";
import { walk } from "https://deno.land/std@0.170.0/fs/mod.ts";
import { stringify } from "npm:yaml@2.1.3"
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)
}
}
@jpcaruana
Copy link

Thanks for the upgrade. I still have issues with the script, as regular working posts fail.

content/posts/2013/03/04/afpy.md
error: Uncaught Error: Parse error on line 1, column 26: Unexpected character: "+"
      throw new TOMLParseError(message);
            ^
    at parse (https://deno.land/std@0.171.0/encoding/_toml/parser.ts:890:13)
    at _extract (file:///xxxmigrate_taxonomies.ts:21:19)
    at extract (file:///xxx/migrate_taxonomies.ts:59:16)
    at file:///Usersjxxxmigrate_taxonomies.ts:153:20

I issued a PR on deno

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment