Skip to content

Instantly share code, notes, and snippets.

@NathanFlurry
Last active February 13, 2023 20:58
Show Gist options
  • Save NathanFlurry/2eaad9a9b9baac917cf7640e42605519 to your computer and use it in GitHub Desktop.
Save NathanFlurry/2eaad9a9b9baac917cf7640e42605519 to your computer and use it in GitHub Desktop.
Inkdrop Import Markdown Files

About

This script will import Markdown files from a folder. It will only import files ending in .md, so it skips folders and images. This will use the file name as the note name. If there is a H1 on the first line, it will remove that line and trim the file in order to remove redundancy.

How to use

  1. Select the notebook you want to import into in the sidebar.
  2. Under Inkdrop, select Developer > Toggle Developer Tools.
  3. Paste this script.
  4. Replace path/to/folder/with/markdown/files/ with a path to the folder with the markdown files in it. Make sure to include the trailing "/".
  5. Ignore the error and please forgive my laziness. If you get a grey screen, just click a few times and the ghost windows will disappear.

Todo (that probably won't ever be completed)

  • Transferring Bear-style tags
  • Image import
  • Use file chooser
async function importMd(path) {
const editorActions = inkdrop.flux.getActions("editor");
const editorStore = inkdrop.flux.getStore("editor");
const files = readFiles(path);
for (let file of files) {
console.log("Importing...", file.name);
await editorActions.create();
const state = editorStore.getState();
const lines = file.content.trim().split("\n");
const newDocument = Object.assign(Object.assign({}, state.document), {
title: file.name,
body: lines[0][0] == "#" ? lines.slice(1).join("\n").trim() : file.contents
});
await editorActions.update({ document: newDocument, changed: true });
await editorActions.save({ document: newDocument });
}
}
function readFiles(dirname) {
const fs = require("fs");
const filenames = fs.readdirSync(dirname);
return filenames.filter(n => n.slice(-3) == ".md").map(name => {
const content = fs.readFileSync(dirname + name, "utf-8");
return { name: name.slice(0,-3), content: content };
});
}
importMd("path/to/folder/with/markdown/files/");
@flaturtha
Copy link

In response to OSoG's question, click on Console and paste there (at least that's what I just did). However, I got an uncaught type error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment