Last active
September 27, 2023 12:43
-
-
Save Xheldon/dfc675c271c909dc3a6e94c869d6ebd4 to your computer and use it in GitHub Desktop.
A way to export Markdown files with images from Craft and import them into Notion while also uploading the images to Notion's own AWS S3 service (instead of using image links). Recursively call pandoc command to convert from markdown with pictures to docx.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
const path = require('path'); | |
const exec = require('child_process').execSync; | |
/** | |
* I use this script to transfer notes from Craft to Notion. The process involves the following considerations: | |
* 1. Craft can export Markdown and place images in a folder at the same level as the Markdown file, with the extension .assets. | |
* 2. Notion only uploads images to its own AWS S3 server when importing Word (.docx format) files, while other formats only include a link to the image. | |
* 3. Therefore, I need to convert the Markdown files exported from Craft to docx files before importing them into Notion. | |
* 4. Notion only supports files at the same level, not nested directories, when importing Word files, so I convert the nested notes exported from Craft to Word format and place them all in a specified directory (absolute path). | |
* 5. There is a chance of failure when importing Word files into Notion, so please try a few at a time and do not import a large number of files at once. | |
* 6. Notion's free version only supports importing files smaller than 5MB. To import Word files larger than 5MB, you need to upgrade to a Pro membership. | |
* 7.To convert Markdown to Word files, I use the command line tool "pandoc". | |
* | |
* Note: You need modify this file to meet your own needs, (I'm not good at the Node Cli), such as putting the converted Word file in the same directory as the Markdown file or convert markdown to html. If you have any suggestions, please feel free to issue! | |
*/ | |
/** | |
* Usage: | |
* 1. Install nodejs: https://nodejs.org/en | |
* 2. Install pandoc: https://pandoc.org/installing.html | |
* 3. Download this script and put it in your notes root dir. | |
* 4. Modify the paths that need to be modified in the comments below. | |
* 5. Run `node Craft2Notion.js` | |
* 6. Enjoy~ | |
*/ | |
// Note: Handling special characters in paths (perhaps there is a better way?) | |
// you can add more characters if your file or path have special characters. | |
function filterStr(str) { | |
return str.split(' ').join('\\ ').split('(').join('\\(').split(')').join('\\)').split('&').join('\\&').split('|').join('\\|'); | |
} | |
// CHANGE TO YOUR OWN: | |
const outputPath = '/Users/x/Developer/import2notion/'; | |
(function md2docx(currDir){ | |
var fileList = fs.readdirSync(currDir); | |
if (fileList && Array.isArray(fileList)) { | |
fileList.forEach(file => { | |
const currPath = `${currDir}/${file}`; | |
const pathInfo = fs.lstatSync(currPath); | |
const ext = path.extname(currPath); | |
if (pathInfo.isFile() && ext === '.md') { | |
const fileName = path.basename(file, '.md'); | |
console.log('------------------------'); | |
console.log(`start: convert「${fileName}」from md to docx`); | |
const _currDir = filterStr(currDir); | |
const _file = filterStr(file); | |
const _fileName = filterStr(fileName); | |
// Note: Will not process files that have already been converted, in order to quickly resume from unexpected interruptions. | |
if (fs.existsSync(`${outputPath}${_fileName}.docx`)) { | |
console.log('file exist!'); | |
console.log('------------------------'); | |
return; | |
} | |
// Note: if not cd to current path, pandoc can't fetch the right image path because it's always use the location where the script is locate. | |
exec(`cd ${_currDir} && pandoc ${_file} --from markdown --to docx -o ${outputPath}${_fileName}.docx`); | |
console.log('success!'); | |
console.log('------------------------'); | |
} else if (pathInfo.isDirectory() && ext !== '.assets') { | |
md2docx(currPath); | |
} | |
}); | |
} | |
})('.'); // start from current dir |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment