Data refactoring and renaming

This commit is contained in:
Will Boyd
2025-02-21 18:54:19 -05:00
parent aaafd6bd07
commit 841111f850
3 changed files with 48 additions and 49 deletions
+17 -18
View File
@@ -27,31 +27,30 @@ class Data {
return expression; return expression;
} }
#getPropArray(propName, isRequired) { children(propName) {
const propArray = this.#obj[propName]; const nodes = this.#obj[propName] ?? [];
if (propArray === undefined && isRequired) { return nodes.map((value, index) => new Data(value, this.#buildExpression(propName, index)));
}
child(propName, index = 0) {
const nodes = this.#obj[propName];
if (nodes === undefined) {
throw new Error(`Could not find ${this.#buildExpression(propName)}.`); throw new Error(`Could not find ${this.#buildExpression(propName)}.`);
} }
return propArray; const node = nodes[index];
} if (node === undefined) {
throw new Error(`Could not find ${this.#buildExpression(propName, index)}.`);
getAll(propName, isRequired = true) {
const propArray = this.#getPropArray(propName, isRequired);
return propArray !== undefined ? propArray.map((value, index) => new Data(value, this.#buildExpression(propName, index))) : undefined;
}
getSingle(propName, index, isRequired = true) {
const prop = (this.#getPropArray(propName, isRequired) ?? [])[index];
if (prop === undefined && isRequired) {
throw new Error(`Could not find ${this.#buildExpression(propName, index)}.`)
} }
return prop !== undefined ? new Data(prop, this.#buildExpression(propName, index)) : undefined; return new Data(node, this.#buildExpression(propName, index));
} }
getAttribute(attrName) { childValue(propName, index = 0) {
return this.child(propName, index).value;
}
attribute(attrName) {
const attribute = this.#obj.$?.[attrName]; const attribute = this.#obj.$?.[attrName];
if (attribute === undefined) { if (attribute === undefined) {
throw new Error(`Could not get attribute ${attrName} from ${this.#expression}.`); throw new Error(`Could not get attribute ${attrName} from ${this.#expression}.`);
+9 -9
View File
@@ -1,14 +1,14 @@
export function author(post) { export function author(post) {
// not decoded, WordPress doesn't allow funky characters in usernames anyway // not decoded, WordPress doesn't allow funky characters in usernames anyway
return post.data.getSingle('creator', 0).value; return post.data.childValue('creator');
} }
export function categories(post) { export function categories(post) {
// array of decoded category names, excluding 'uncategorized' // array of decoded category names, excluding 'uncategorized'
const categories = post.data.getAll('category', false) ?? []; const categories = post.data.children('category');
return categories return categories
.filter((category) => category.getAttribute('domain') === 'category' && category.getAttribute('nicename') !== 'uncategorized') .filter((category) => category.attribute('domain') === 'category' && category.attribute('nicename') !== 'uncategorized')
.map((category) => decodeURIComponent(category.getAttribute('nicename'))); .map((category) => decodeURIComponent(category.attribute('nicename')));
} }
export function coverImage(post) { export function coverImage(post) {
@@ -28,7 +28,7 @@ export function draft(post) {
export function excerpt(post) { export function excerpt(post) {
// not decoded, newlines collapsed // not decoded, newlines collapsed
return post.data.getSingle('encoded', 1).value.replace(/[\r\n]+/gm, ' '); return post.data.childValue('encoded', 1).replace(/[\r\n]+/gm, ' ');
} }
export function id(post) { export function id(post) {
@@ -43,15 +43,15 @@ export function slug(post) {
export function tags(post) { export function tags(post) {
// array of decoded tag names (yes, they come from <category> nodes, not a typo) // array of decoded tag names (yes, they come from <category> nodes, not a typo)
const categories = post.data.getAll('category', false) ?? []; const categories = post.data.children('category');
return categories return categories
.filter((category) => category.getAttribute('domain') === 'post_tag') .filter((category) => category.attribute('domain') === 'post_tag')
.map((category) => decodeURIComponent(category.getAttribute('nicename'))); .map((category) => decodeURIComponent(category.attribute('nicename')));
} }
export function title(post) { export function title(post) {
// not decoded // not decoded
return post.data.getSingle('title', 0).value; return post.data.childValue('title');
} }
export function type(post) { export function type(post) {
+22 -22
View File
@@ -10,8 +10,8 @@ export async function parseFilePromise() {
const content = await fs.promises.readFile(shared.config.input, 'utf8'); const content = await fs.promises.readFile(shared.config.input, 'utf8');
const rssData = await data.load(content); const rssData = await data.load(content);
const channelData = rssData.getSingle('channel', 0); const channelData = rssData.child('channel');
const allPostData = channelData.getAll('item'); const allPostData = channelData.children('item');
const postTypes = getPostTypes(allPostData); const postTypes = getPostTypes(allPostData);
const posts = collectPosts(allPostData, postTypes); const posts = collectPosts(allPostData, postTypes);
@@ -33,7 +33,7 @@ export async function parseFilePromise() {
function getPostTypes(allPostData) { function getPostTypes(allPostData) {
// search export file for all post types minus some specific types we don't want // search export file for all post types minus some specific types we don't want
const postTypes = allPostData const postTypes = allPostData
.map((postData) => postData.getSingle('post_type', 0).value) .map((postData) => postData.childValue('post_type'))
.filter((postType) => ![ .filter((postType) => ![
'attachment', 'attachment',
'revision', 'revision',
@@ -52,15 +52,15 @@ function getPostTypes(allPostData) {
} }
function getItemsOfType(allPostData, type) { function getItemsOfType(allPostData, type) {
return allPostData.filter(item => item.getSingle('post_type', 0).value === type); return allPostData.filter(item => item.childValue('post_type') === type);
} }
function collectPosts(allPostData, postTypes) { function collectPosts(allPostData, postTypes) {
let allPosts = []; let allPosts = [];
postTypes.forEach(postType => { postTypes.forEach(postType => {
const postsForType = getItemsOfType(allPostData, postType) const postsForType = getItemsOfType(allPostData, postType)
.filter(postData => postData.getSingle('status', 0).value !== 'trash') .filter(postData => postData.childValue('status') !== 'trash')
.filter(postData => !(postType === 'page' && postData.getSingle('post_name', 0).value === 'sample-page')) .filter(postData => !(postType === 'page' && postData.childValue('post_name') === 'sample-page'))
.map(postData => buildPost(postData)); .map(postData => buildPost(postData));
if (postsForType.length > 0) { if (postsForType.length > 0) {
@@ -79,13 +79,13 @@ function buildPost(data) {
data, data,
// body content converted to markdown // body content converted to markdown
content: translator.getPostContent(data.getSingle('encoded', 0).value), content: translator.getPostContent(data.childValue('encoded')),
// particularly useful values for all sorts of things // particularly useful values for all sorts of things
type: data.getSingle('post_type', 0).value, type: data.childValue('post_type'),
id: data.getSingle('post_id', 0).value, id: data.childValue('post_id'),
isDraft: data.getSingle('status', 0).value === 'draft', isDraft: data.childValue('status') === 'draft',
slug: decodeURIComponent(data.getSingle('post_name', 0).value), slug: decodeURIComponent(data.childValue('post_name')),
date: getPostDate(data), date: getPostDate(data),
coverImageId: getPostMetaValue(data, '_thumbnail_id'), coverImageId: getPostMetaValue(data, '_thumbnail_id'),
@@ -96,27 +96,27 @@ function buildPost(data) {
} }
function getPostDate(data) { function getPostDate(data) {
const date = luxon.DateTime.fromRFC2822(data.getSingle('pubDate', 0).value ?? '', { zone: shared.config.customDateTimezone }); const date = luxon.DateTime.fromRFC2822(data.childValue('pubDate'), { zone: shared.config.customDateTimezone });
return date.isValid ? date : undefined; return date.isValid ? date : undefined;
} }
function getPostMetaValue(data, key) { function getPostMetaValue(data, key) {
const metas = data.getAll('postmeta', false) ?? []; const metas = data.children('postmeta');
const meta = metas.find((meta) => meta.getSingle('meta_key', 0).value === key); const meta = metas.find((meta) => meta.childValue('meta_key') === key);
return meta ? meta.getSingle('meta_value', 0).value : undefined; return meta ? meta.childValue('meta_value') : undefined;
} }
function collectAttachedImages(allPostData) { function collectAttachedImages(allPostData) {
const images = getItemsOfType(allPostData, 'attachment') const images = getItemsOfType(allPostData, 'attachment')
// filter to certain image file types // filter to certain image file types
.filter(attachment => { .filter(attachment => {
const url = attachment.getSingle('attachment_url', 0).value; const url = attachment.childValue('attachment_url');
return url && (/\.(gif|jpe?g|png|webp)$/i).test(url); return url && (/\.(gif|jpe?g|png|webp)$/i).test(url);
}) })
.map(attachment => ({ .map(attachment => ({
id: attachment.getSingle('post_id', 0).value, id: attachment.childValue('post_id'),
postId: attachment.getSingle('post_parent', 0).value, postId: attachment.childValue('post_parent'),
url: attachment.getSingle('attachment_url', 0).value url: attachment.childValue('attachment_url')
})); }));
console.log(images.length + ' attached images found.'); console.log(images.length + ' attached images found.');
@@ -127,9 +127,9 @@ function collectScrapedImages(allPostData, postTypes) {
const images = []; const images = [];
postTypes.forEach(postType => { postTypes.forEach(postType => {
getItemsOfType(allPostData, postType).forEach(postData => { getItemsOfType(allPostData, postType).forEach(postData => {
const postId = postData.getSingle('post_id', 0).value; const postId = postData.childValue('post_id');
const postContent = postData.getSingle('encoded', 0).value; const postContent = postData.childValue('encoded');
const postLink = postData.getSingle('link', 0).value; const postLink = postData.childValue('link');
const matches = [...postContent.matchAll(/<img[^>]*src="(.+?\.(?:gif|jpe?g|png|webp))"[^>]*>/gi)]; const matches = [...postContent.matchAll(/<img[^>]*src="(.+?\.(?:gif|jpe?g|png|webp))"[^>]*>/gi)];
matches.forEach(match => { matches.forEach(match => {