diff --git a/.eslintrc.json b/.eslintrc.json new file mode 100644 index 0000000..79bff50 --- /dev/null +++ b/.eslintrc.json @@ -0,0 +1,17 @@ +{ + "env": { + "browser": true, + "commonjs": true, + "es6": true + }, + "extends": "eslint:recommended", + "globals": { + "Atomics": "readonly", + "SharedArrayBuffer": "readonly" + }, + "parserOptions": { + "ecmaVersion": 2018 + }, + "rules": { + } +} \ No newline at end of file diff --git a/README.md b/README.md index c17f3a3..92911c0 100644 --- a/README.md +++ b/README.md @@ -1,135 +1,156 @@ # wordpress-export-to-markdown -Converts a WordPress export XML file into Markdown files. +A script that converts a WordPress export XML file into Markdown files suitable for a static site generator ([Gatsby](https://www.gatsbyjs.org/), [Hugo](https://gohugo.io/), [Jekyll](https://jekyllrb.com/), etc.). -Useful if you want to migrate from WordPress to a static site generator ([Gatsby](https://www.gatsbyjs.org/), [Hugo](https://gohugo.io/), [Jekyll](https://jekyllrb.com/), etc.). +Each post is saved as a separate Markdown file with appropriate frontmatter. Images are also downloaded and saved. Embedded content from YouTube, Twitter, CodePen, etc. is carefully preserved. -Saves each post as a separate file with appropriate frontmatter. Also saves attached images and (optionally) any additional images found in post body content. Posts and images can be saved into a variety of folder structures. +![wordpress-export-to-markdown running in a terminal](https://user-images.githubusercontent.com/1245573/72686026-3aa04280-3abe-11ea-92c1-d756a24657dd.gif) ## Quick Start You'll need: -- [Node.js](https://nodejs.org/) v10.12 or later -- Your [WordPress export file](https://codex.wordpress.org/Tools_Export_Screen) +- [Node.js](https://nodejs.org/) v12.14 or later +- Your [WordPress export file](https://wordpress.org/support/article/tools-export-screen/) -There are a few ways you can use this package: +It is recommended that you drop your WordPress export file into the same directory that you run this script from so it's easy to find. -1. To run via `npx`, run `npx wordpress-export-to-markdown` -2. To add to an existing repo, run `npm i wordpress-export-to-markdown` and then `wordpress-export-to-markdown` -3. Clone this repo, open your terminal to this package's directory, then run `npm install` and then `node index.js` - -This will create an `/output` folder filled with your posts and images. - -## Customization - -You can use command line arguments to control options for how the script runs. For example, this will give you [Jekyll](https://jekyllrb.com/)-style output in terms of folder structure and filenames: +You can run this script immediately in your terminal with `npx`: ``` -node index.js --postfolders=false --prefixdate=true +npx wordpress-export-to-markdown ``` -### --input +Or you can clone and run (this makes repeated runs faster and allows you to tinker with the code). After cloning this repo, open your terminal to the package's directory and run: -- Type: String +``` +npm install && node index.js +``` + +Either way you run it, the script will start the wizard. Answer the questions and off you go! + +## Command Line + +The wizard makes it easy to configure your options, but you can also do so via the command line if you want. For example, the following will give you [Jekyll](https://jekyllrb.com/)-style output in terms of folder structure and filenames. + +Using `npx`: + +``` +npx wordpress-export-to-markdown --post-folders=false --prefix-date=true +``` + +Using a locally cloned repo: + +``` +node index.js --post-folders=false --prefix-date=true +``` + +The wizard will still ask you about any options not specifed on the command line. To skip the wizard entirely and use default values for unspecified options, add `--wizard=false`. + +## Options + +### Use wizard? + +- Argument: `--wizard` +- Type: `boolean` +- Default: `true` + +Enable to have the script prompt you for each option. Disable to skip the wizard and use default values for any options not specified via the command line. + +### Path to WordPress export file? + +- Argument: `--input` +- Type: `file` (as a path string) - Default: `export.xml` -The file to parse. This should be the WordPress export XML file that you downloaded. +The path to the WordPress export file that you want to parse. It is recommended that you drop your WordPress export file into the same directory that you run this script from so it's easy to find. -### --output +### Path to output folder? -- Type: String +- Argument: `--output` +- Type: `folder` (as a path string) - Default: `output` -The output directory where Markdown and image files will be saved. +The path to the output directory where Markdown and image files will be saved. If it does not exist, it will be created for you. -### --yearmonthfolders +### Create year folders? -- Type: Boolean +- Argument: `--year-folders` +- Type: `boolean` - Default: `false` -Whether or not to organize output files into year and month folders. +Whether or not to organize output files into folders by year. - /output - /2017 - /01 - /02 - /2018 - /01 +### Create month folders? -### --yearfolders - -- Type: Boolean +- Argument: `--month-folders` +- Type: `boolean` - Default: `false` -Whether or not to organize output files into year folders. +Whether or not to organize output files into folders by month. You'll probably want to combine this with `--year-folders` to organize files by year then month. - /output - /2017 - /2018 +### Create a folder for each post? -### --postfolders - -- Type: Boolean +- Argument: `--post-folders` +- Type: `boolean` - Default: `true` Whether or not to save files and images into post folders. If `true`, the post slug is used for the folder name and the post's Markdown file is named `index.md`. Each post folder will have its own `/images` folder. - /output - /first-post - /images - potato.png - index.md - /oh-look-another-post - /images - cat1.gif - cat2.gif - index.md + /first-post + /images + potato.png + index.md + /second-post + /images + carrot.jpg + celery.jpg + index.md If `false`, the post slug is used to name the post's Markdown file. These files will be side-by-side and images will go into a shared `/images` folder. - /output - /images - cat1.gif - cat2.gif - potato.png - first-post.md - oh-look-another-post.md + /images + carrot.jpg + celery.jpg + potato.png + first-post.md + second-post.md -Either way, this can be combined with with `--yearmonthfolderes` and `--yearfolders`, in which case the above output will be organized under the appropriate year and month folders. +Either way, this can be combined with with `--year-folders` and `--month-folders`, in which case the above output will be organized under the appropriate year and month folders. -### --prefixdate +### Prefix post folders/files with date? -- Type: Boolean +- Argument: `--prefix-date` +- Type: `boolean` - Default: `false` Whether or not to prepend the post date to the post slug when naming a post's folder or file. -If `--postfolders` is `true`, this affects the folder. +If `--post-folders` is `true`, this affects the folder. - /output - /2017-01-14-first-post - index.md - /2017-01-23-oh-look-another-post - index.md + /2019-10-14-first-post + index.md + /2019-10-23-second-post + index.md -If `--postfolders` is `false`, this affects the file. +If `--post-folders` is `false`, this affects the file. - /output - 2017-01-14-first-post.md - 2017-01-23-oh-look-another-post.md + 2019-10-14-first-post.md + 2019-10-23-second-post.md -### --saveimages +### Save images attached to posts? -- Type: Boolean +- Argument: `--save-attached-images` +- Type: `boolean` - Default: `true` -Whether or not to download and save images attached to posts. Generally speaking, these are images that were added by dragging/dropping or clicking **Add Media** or **Set Featured Image** when editing a post in WordPress. Images are saved into `/images`. See `--postfolders` for more details. +Whether or not to download and save images attached to posts. Generally speaking, these are images that were uploaded by using **Add Media** or **Set Featured Image** when editing a post in WordPress. Images are saved into `/images`. -### --addcontentimages +### Save images scraped from post body content? -- Type: Boolean -- Default: `false` +- Argument: `--save-scraped-images` +- Type: `boolean` +- Default: `true` -Whether or not to also include images scraped from <img> tags in post body content. These images are downloaded and saved along with other images as dictated by `--saveimages`. The <img> tags are updated to point to where the images are saved. +Whether or not to download and save images scraped from <img> tags in post body content. Images are saved into `/images`. The <img> tags are updated to point to where the images are saved. diff --git a/index.js b/index.js index 9699fba..ff993cc 100644 --- a/index.js +++ b/index.js @@ -1,383 +1,27 @@ #!/usr/bin/env node -const fs = require('fs'); -const luxon = require('luxon'); -const minimist = require('minimist'); const path = require('path'); -const request = require('request'); -const turndown = require('turndown'); -const xml2js = require('xml2js'); +const process = require('process'); -// global so various functions can access arguments -let argv; +const wizard = require('./src/wizard'); +const parser = require('./src/parser'); +const writer = require('./src/writer'); -function init() { - argv = minimist(process.argv.slice(2), { - string: [ - 'input', - 'output' - ], - boolean: [ - 'yearmonthfolders', - 'yearfolders', - 'postfolders', - 'prefixdate', - 'saveimages', - 'addcontentimages' - ], - default: { - input: 'export.xml', - output: 'output', - yearmonthfolders: false, - yearfolders: false, - postfolders: true, - prefixdate: false, - saveimages: true, - addcontentimages: false - } - }); +(async () => { + // parse any command line arguments and run wizard + const config = await wizard.getConfig(process.argv); - let content = readFile(argv.input); - parseFileContent(content); -} + // parse data from XML and do Markdown translations + const posts = await parser.parseFilePromise(config) -function readFile(path) { - try { - return fs.readFileSync(path, 'utf8'); - } catch (ex) { - console.log('Unable to read file.'); - console.log(ex.message); - } -} + // write files, downloading images as needed + await writer.writeFilesPromise(posts, config); -function parseFileContent(content) { - const processors = { tagNameProcessors: [xml2js.processors.stripPrefix] }; - xml2js.parseString(content, processors, (err, data) => { - if (err) { - console.log('Unable to parse file content.'); - console.log(err); - } else { - processData(data); - } - }); -} - -function processData(data) { - let images = collectImages(data); - let posts = collectPosts(data); - mergeImagesIntoPosts(images, posts); - writeFiles(posts); -} - -function collectImages(data) { - // start by collecting all attachment images - let images = getItemsOfType(data, 'attachment') - // filter to certain image file types - .filter(attachment => (/\.(gif|jpg|png)$/i).test(attachment.attachment_url[0])) - .map(attachment => ({ - id: attachment.post_id[0], - postId: attachment.post_parent[0], - url: attachment.attachment_url[0] - })); - - // optionally add images scraped from tags in post content - if (argv.addcontentimages) { - addContentImages(data, images); - } - - return images; -} - -function addContentImages(data, images) { - let regex = (/]*src="(.+?\.(?:gif|jpg|png))"[^>]*>/gi); - let match; - - getItemsOfType(data, 'post').forEach(post => { - let postId = post.post_id[0]; - let postContent = post.encoded[0]; - let postLink = post.link[0]; - - // reset lastIndex since we're reusing the same regex object - regex.lastIndex = 0; - while ((match = regex.exec(postContent)) !== null) { - // base the matched image URL relative to the post URL - let url = new URL(match[1], postLink).href; - - // add image if it hasn't already been added for this post - let exists = images.some(image => image.postId === postId && image.url === url); - if (!exists) { - images.push({ - id: -1, - postId: postId, - url: url - }); - console.log('Scraped ' + url + '.'); - } - } - }); -} - -function collectPosts(data) { - // this is passed into getPostContent() for the markdown conversion - turndownService = initTurndownService(); - - return getItemsOfType(data, 'post') - .map(post => ({ - // meta data isn't written to file, but is used to help with other things - meta: { - id: getPostId(post), - slug: getPostSlug(post), - coverImageId: getPostCoverImageId(post) - }, - frontmatter: { - title: getPostTitle(post), - date: getPostDate(post) - }, - content: getPostContent(post, turndownService) - })); -} - -function initTurndownService() { - let turndownService = new turndown({ - headingStyle: 'atx', - bulletListMarker: '-', - codeBlockStyle: 'fenced' - }); - - // preserve embedded tweets - turndownService.addRule('tweet', { - filter: node => node.nodeName === 'BLOCKQUOTE' && node.getAttribute('class') === 'twitter-tweet', - replacement: (content, node) => '\n\n' + node.outerHTML - }); - - // preserve embedded codepens - turndownService.addRule('codepen', { - filter: node => { - // codepen embed snippets have changed over the years - // but this series of checks should find the commonalities - return ( - ['P', 'DIV'].includes(node.nodeName) && - node.attributes['data-slug-hash'] && - node.getAttribute('class') === 'codepen' - ); - }, - replacement: (content, node) => '\n\n' + node.outerHTML - }); - - // preserve embedded scripts (for tweets, codepens, gists, etc.) - turndownService.addRule('script', { - filter: 'script', - replacement: (content, node) => { - let before = '\n\n'; - let src = node.getAttribute('src'); - if (node.previousSibling && node.previousSibling.nodeName !== '#text') { - // keep twitter and codepen