mirror of
https://github.com/10h30/wordpress-export-to-markdown.git
synced 2026-07-20 15:14:01 +09:00
Save images into /images
This commit is contained in:
@@ -47,8 +47,7 @@ function processData(data) {
|
|||||||
let images = collectImages(data);
|
let images = collectImages(data);
|
||||||
let posts = collectPosts(data);
|
let posts = collectPosts(data);
|
||||||
mergeImagesIntoPosts(images, posts);
|
mergeImagesIntoPosts(images, posts);
|
||||||
|
writeFiles(posts);
|
||||||
writeMarkdownFiles(posts);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function collectImages(data) {
|
function collectImages(data) {
|
||||||
@@ -127,17 +126,21 @@ function getFilenameFromPath(path) {
|
|||||||
return path.split('/').slice(-1)[0];
|
return path.split('/').slice(-1)[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
function writeMarkdownFiles(posts) {
|
function createDir(path) {
|
||||||
|
try {
|
||||||
|
fs.accessSync(path, fs.constants.F_OK);
|
||||||
|
} catch (ex) {
|
||||||
|
fs.mkdirSync(path, { recursive: true });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function writeFiles(posts) {
|
||||||
posts.forEach(post => {
|
posts.forEach(post => {
|
||||||
const dir = path.join(outputDir, post.frontmatter.slug);
|
const postDir = path.join(outputDir, post.frontmatter.slug);
|
||||||
try {
|
createDir(postDir);
|
||||||
fs.accessSync(dir, fs.constants.F_OK);
|
|
||||||
} catch (ex) {
|
|
||||||
fs.mkdirSync(dir, { recursive: true });
|
|
||||||
}
|
|
||||||
|
|
||||||
const content = createMarkdownContent(post);
|
const content = createMarkdownContent(post);
|
||||||
const postPath = path.join(dir, 'index.md');
|
const postPath = path.join(postDir, 'index.md');
|
||||||
fs.writeFile(postPath, content, (err) => {
|
fs.writeFile(postPath, content, (err) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.log('Unable to write file.')
|
console.log('Unable to write file.')
|
||||||
@@ -149,7 +152,10 @@ function writeMarkdownFiles(posts) {
|
|||||||
|
|
||||||
if (post.meta.imageUrls) {
|
if (post.meta.imageUrls) {
|
||||||
post.meta.imageUrls.forEach(imageUrl => {
|
post.meta.imageUrls.forEach(imageUrl => {
|
||||||
let imagePath = path.join(dir, getFilenameFromPath(imageUrl));
|
const postImagesDir = path.join(postDir, 'images');
|
||||||
|
createDir(postImagesDir);
|
||||||
|
|
||||||
|
let imagePath = path.join(postImagesDir, getFilenameFromPath(imageUrl));
|
||||||
let stream = fs.createWriteStream(imagePath);
|
let stream = fs.createWriteStream(imagePath);
|
||||||
stream.on('finish', () => {
|
stream.on('finish', () => {
|
||||||
console.log('Saved ' + imagePath + '.');
|
console.log('Saved ' + imagePath + '.');
|
||||||
|
|||||||
Reference in New Issue
Block a user