mirror of
https://github.com/10h30/wordpress-export-to-markdown.git
synced 2026-07-20 07:04:14 +09:00
Refactor in progress
This commit is contained in:
@@ -52,7 +52,7 @@ function processData(data) {
|
|||||||
|
|
||||||
function collectImages(data) {
|
function collectImages(data) {
|
||||||
return getItemsOfType(data, 'attachment')
|
return getItemsOfType(data, 'attachment')
|
||||||
.filter(image => (/\.(gif|jpg|png)$/i).test(image.attachment_url[0]))
|
.filter(attachment => (/\.(gif|jpg|png)$/i).test(attachment.attachment_url[0]))
|
||||||
.map(attachment => ({
|
.map(attachment => ({
|
||||||
id: attachment.post_id[0],
|
id: attachment.post_id[0],
|
||||||
postId: attachment.post_parent[0],
|
postId: attachment.post_parent[0],
|
||||||
@@ -60,22 +60,52 @@ function collectImages(data) {
|
|||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
function collectPosts(data, images) {
|
function collectPosts(data) {
|
||||||
return getItemsOfType(data, 'post')
|
return getItemsOfType(data, 'post')
|
||||||
.map(post => ({
|
.map(post => ({
|
||||||
meta: {
|
meta: {
|
||||||
id: post.post_id[0],
|
id: getPostId(post),
|
||||||
coverImageId: getCoverImageId(post)
|
coverImageId: getPostCoverImageId(post)
|
||||||
},
|
},
|
||||||
frontmatter: {
|
frontmatter: {
|
||||||
slug: translateSlug(post.post_name[0]),
|
slug: getPostSlug(post),
|
||||||
title: translateTitle(post.title[0]),
|
title: getPostTitle(post),
|
||||||
date: translateDate(post.pubDate[0])
|
date: getPostDate(post)
|
||||||
},
|
},
|
||||||
content: translateContent(post.encoded[0])
|
content: getPostContent(post)
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getItemsOfType(data, type) {
|
||||||
|
return data.rss.channel[0].item.filter(item => item.post_type[0] === type);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getPostId(post) {
|
||||||
|
return post.post_id[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
function getPostCoverImageId(post) {
|
||||||
|
let postmeta = post.postmeta.find(postmeta => postmeta.meta_key[0] === '_thumbnail_id');
|
||||||
|
let result = postmeta ? postmeta.meta_value[0] : undefined;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getPostSlug(post) {
|
||||||
|
return post.post_name[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
function getPostTitle(post) {
|
||||||
|
return post.title[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
function getPostDate(post) {
|
||||||
|
return luxon.DateTime.fromRFC2822(post.pubDate[0], { zone: 'utc' }).toISO();
|
||||||
|
}
|
||||||
|
|
||||||
|
function getPostContent(post) {
|
||||||
|
return post.encoded[0].trim();
|
||||||
|
}
|
||||||
|
|
||||||
function mergeImagesIntoPosts(images, posts) {
|
function mergeImagesIntoPosts(images, posts) {
|
||||||
let postsLookup = posts.reduce((lookup, post) => {
|
let postsLookup = posts.reduce((lookup, post) => {
|
||||||
lookup[post.meta.id] = post;
|
lookup[post.meta.id] = post;
|
||||||
@@ -96,59 +126,16 @@ function mergeImagesIntoPosts(images, posts) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function getItemsOfType(data, type) {
|
|
||||||
return data.rss.channel[0].item.filter(item => item.post_type[0] === type);
|
|
||||||
}
|
|
||||||
|
|
||||||
function getCoverImageId(post) {
|
|
||||||
let postMeta = post.postmeta.find(postmeta => postmeta.meta_key[0] === '_thumbnail_id');
|
|
||||||
let result = postMeta ? postMeta.meta_value[0] : undefined;
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
function translateSlug(value) {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
function translateTitle(value) {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
function translateDate(value) {
|
|
||||||
return luxon.DateTime.fromRFC2822(value, { zone: 'utc' }).toISO();
|
|
||||||
}
|
|
||||||
|
|
||||||
function translateContent(value) {
|
|
||||||
return value.trim();
|
|
||||||
}
|
|
||||||
|
|
||||||
function getFilenameFromPath(path) {
|
function getFilenameFromPath(path) {
|
||||||
return path.split('/').slice(-1)[0];
|
return path.split('/').slice(-1)[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
function createDir(path) {
|
|
||||||
try {
|
|
||||||
fs.accessSync(path, fs.constants.F_OK);
|
|
||||||
} catch (ex) {
|
|
||||||
fs.mkdirSync(path, { recursive: true });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function writeFiles(posts) {
|
function writeFiles(posts) {
|
||||||
posts.forEach(post => {
|
posts.forEach(post => {
|
||||||
const postDir = path.join(outputDir, post.frontmatter.slug);
|
const postDir = path.join(outputDir, post.frontmatter.slug);
|
||||||
createDir(postDir);
|
createDir(postDir);
|
||||||
|
|
||||||
const content = createMarkdownContent(post);
|
writeMarkdownFile(post, postDir);
|
||||||
const postPath = path.join(postDir, 'index.md');
|
|
||||||
fs.writeFile(postPath, content, (err) => {
|
|
||||||
if (err) {
|
|
||||||
console.log('Unable to write file.')
|
|
||||||
console.log(err);
|
|
||||||
} else {
|
|
||||||
console.log('Wrote ' + postPath + '.');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
if (post.meta.imageUrls) {
|
if (post.meta.imageUrls) {
|
||||||
post.meta.imageUrls.forEach(imageUrl => {
|
post.meta.imageUrls.forEach(imageUrl => {
|
||||||
@@ -178,6 +165,27 @@ function writeFiles(posts) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function createDir(path) {
|
||||||
|
try {
|
||||||
|
fs.accessSync(path, fs.constants.F_OK);
|
||||||
|
} catch (ex) {
|
||||||
|
fs.mkdirSync(path, { recursive: true });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function writeMarkdownFile(post, postDir) {
|
||||||
|
const content = createMarkdownContent(post);
|
||||||
|
const postPath = path.join(postDir, 'index.md');
|
||||||
|
fs.writeFile(postPath, content, (err) => {
|
||||||
|
if (err) {
|
||||||
|
console.log('Unable to write file.')
|
||||||
|
console.log(err);
|
||||||
|
} else {
|
||||||
|
console.log('Wrote ' + postPath + '.');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
function createMarkdownContent(post) {
|
function createMarkdownContent(post) {
|
||||||
const frontmatter = Object.entries(post.frontmatter)
|
const frontmatter = Object.entries(post.frontmatter)
|
||||||
.reduce((accumulator, pair) => {
|
.reduce((accumulator, pair) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user