In this article, I will look into automated solutions for converting WordPress posts to Markdown.
Use the methods described to migrate your WordPress site to Markdown-enabled CMS (such as Hugo, Jekyll, etc.) or simply backup your articles in a convenient and easy-to-read format.
Convert the WordPress XML export with a script
In this section, I will cover a two-step process that involves generating an XML file from WordPress and then using a Node.js script to produce the Markdown content.
Generate an XML file that includes all your WordPress posts
Follow these steps to create an export of your WordPress content in XML format. Note that this functionality is available by default in WordPress without installing plugins.
- Visit the Tools -> Export menu
- Select which posts you want to export. Here you can select all your content or only specific post types.
- Click the Download Export File button
You now have an XML file that includes the selected material. In the next step, you’ll use this file as a source for the Node.js script.
Run the wordpress-export-to-markdown script
The Node.js script in question is the wordpress-export-to-markdown script. A specialized piece of code that can generate Markdown files from your WordPress posts, download the images referenced in the posts, and categorize your posts in hierarchical directories that reflect the post type, year, and month of the posts.
This script requires an environment with Node.js installed and a terminal where you can run basic commands. Start by installing Node.js with the instructions provided on the official website. The process simply involves downloading and executing the provided installer.
Once you have done this, verify the success of the operation by running this command:
node -v
You can now install the wordpress-export-to-markdown package and start the script with this command:
npx wordpress-export-to-markdown
This script generates a command line wizard. Your answers will be used to configure the XML to Markdown conversion.
Let’s see in detail how to respond to the questions of the Wizard:
Path to WordPress export file?
Here set the path to the XML file that you just downloaded. To facilitate the process, move the downloaded XML file to the same folder used to run the script. Then enter the file name.
In my test, I generated the Markdown export from this file:
example.WordPress.2022-09-20.xml
Path to output folder?
Set a custom output folder. In my Linux machine, for example, I entered the following folder:
/home/danilo/Documents/output/
Create year folders?/Create month folders?/Create a folder for each post?
The script uses these three answers to configure the hierarchical structure of the folder used to archive the Markdown files.
Prefix post folders/files with date?
Your answer determines if the generated folders and files will be prefixed with the post date.
Save images attached to posts?
If you enable this feature, the script downloads all the images uploaded in your articles.
Save images scraped from post body content?
If you respond affirmatively to this question, the script will scrap any image referenced in the article, even the pictures stored on external websites. The downloaded images will be stored in the same folder of the article.
Include custom post types and pages?
Based on this answer, the script will include or not the Markdown files for the “page” post type and the custom post types.
The conversion process
For a successful conversion process and download of the referenced images, ensure a stable connection, proper writing permission for the output folder, and enough storage on your computer.
If you enable the automatic download of the images, the script will take much longer to complete its execution. You should generally expect to wait minutes or hours for websites with thousands of posts. This is because the script individually downloads each image with an HTTP request to the server.
With the Markdown Export feature of the Ultimate Markdown plugin
Ultimate Markdown is a WordPress plugin that includes multiple Markdown-related tools. Such as a Markdown editor, Markdown conversion tools, Markdown import and export functionalities, and more.
General information on the HTML to Markdown conversion
The plugin uses the HTML To Markdown for PHP library to convert an HTML document to Markdown.
The HTML to Markdown feature is available in the post editor for converting individual articles and in a dedicated plugin menu for converting collections of posts.
Convert a single WordPress post to Markdown
If you are migrating a small website or if, for any reason, you want to download a limited amount of articles, you can use the Export Markdown editor sidebar section provided by the Ultimate Markdown plugin for WordPress.
With this functionality, you can create the Markdown version of the edited article with a single click.
Convert a large number of WordPress posts to Markdown
This plugin has a dedicated menu to mass export the WordPress articles to Markdown.
To use this functionality, proceed to the Export menu of the plugin. Then select “Posts” as a source and click “Export Data”.
The plugin generates a compressed archive (in zip format) of Markdown files in your default browser download folder. Extract this file to access your documents.
Options for advanced users
Additional options are also available for advanced users. Specifically, you can select the post types, categories, and tags used as a source. For example, you can mass export only blog posts, only pages, only articles in the “News” category, etc.
Note that this plugin doesn’t download the images referenced in the posts. Consequently, you might have to manually download them with an FTP client or use the wordpress-export-to-markdown script described in this tutorial.