Skip to content

Commit

Permalink
Map WordPress categories tags to Eleventy tags
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Nov 15, 2024
1 parent 0d43015 commit d0e4f14
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ A small utility (and CLI) to import content files from various content sources.
- Downloads all referenced assets (images, videos, stylesheets, scripts, etc) in content and co-locates the assets with the content.
- Works with a bunch of different data sources (see below).
- **Resumable**: Can stop and resume a large import later, reusing a local cache (with configurable cache duration)
- **Safe**: avoids overwriting existing files by default (unless you opt-in with `--overwrite`).
- **Repeatable**: avoids overwriting existing content files (unless you opt-in with `--overwrite`).
- This allows you to continue using an import source for new content while editing the already imported content.
- Use `--dryrun` for testing without writing any files.

Expand Down
46 changes: 45 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"dependencies": {
"@11ty/eleventy-fetch": "^5.0.0-beta.7",
"@11ty/posthtml-urls": "^1.0.0",
"@sindresorhus/slugify": "^2.2.1",
"dotenv": "^16.4.5",
"fast-xml-parser": "^4.5.0",
"filesize": "^10.1.6",
Expand Down
5 changes: 5 additions & 0 deletions src/DataSource/WordPressApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ class WordPressApi extends DataSource {
metadata,
};

if(metadata.categories) {
// map WordPress categories for use in Eleventy tags (not WordPress metadata tags, which are different)
obj.tags = metadata.categories;
}

if(entry.og_image) {
obj.metadata.opengraphImage = {
width: entry.og_image?.width,
Expand Down
12 changes: 11 additions & 1 deletion src/Importer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import path from "node:path";
import { createRequire } from "node:module";
import fs from "graceful-fs";
import yaml from "js-yaml";
import kleur from "kleur";
import { createRequire } from "node:module";
import slugify from '@sindresorhus/slugify';

import { Logger } from "./Logger.js";
import { Fetcher } from "./Fetcher.js";
Expand Down Expand Up @@ -309,6 +310,15 @@ class Importer {
// TODO map metadata.categories and/or metadata.tags to Eleventy `tags`
}

if(entry.tags) {
if(!Array.isArray(entry.tags)) {
entry.tags = [entry.tags];
}

// slugify the tags
data.tags = entry.tags.map(tag => slugify(tag));
}

// https://www.npmjs.com/package/js-yaml#dump-object---options-
let frontMatter = yaml.dump(data, {
// sortKeys: true,
Expand Down

0 comments on commit d0e4f14

Please sign in to comment.