Giter VIP home page Giter VIP logo

inkdrop-live-export's Introduction

Inkdrop Live Export

An Inkdrop module which allows you to programmatically export notes to local filesystem via the local HTTP server. It supports live export, which continuously exports notes as the changes occur.

Prerequisites

  • NodeJS >= 18
  • Inkdrop >= 5.5.1

Demo project

A simple blog:

How to use it

Enable the Inkdrop local server

Follow the instruction in the documentation.

Now you should be able to invoke the API like so:

curl http://username:password@localhost:19840/
# => {"version":"5.5.1","ok":true}

Install dev-tools plugin

It helps copy notebook IDs quickly from the context menu.

https://my.inkdrop.app/plugins/dev-tools

Then, copy a bookId of a notebook you'd like to export by right-clicking the notebook on the sidebar and select Copy Notebook ID.

Copy notebook ID

Install live-export

Suppose that you have a static website project such as a blog or a documentation, and you are in its root directory.

cd <PROJECT_ROOT>
npm i -D @inkdropapp/live-export

Example

Create a file import.mjs (It must be an ES Module). Initialize a live exporter:

import { LiveExporter, toKebabCase } from '@inkdropapp/live-export'

const liveExport = new LiveExporter({
  username: 'foo',
  password: 'bar',
  port: 19840
})

Then, start exporting like so:

const sub = await liveExport.start({
  live: true,
  bookId: '<YOUR_BOOK_ID>',
  preProcessNote: ({ note, frontmatter, tags }) => {
    frontmatter.title = note.title
    // Convert note title to kebab case (eg. "kebab-case-note-title")
    frontmatter.slug = toKebabCase(note.title)
    frontmatter.tags = tags.map(t => t.name)
  },
  pathForNote: ({ /* note, */ frontmatter }) => {
    // export only if it's public
    if (frontmatter.public) {
      return `./<PATH_TO_EXPORT_NOTES>/${frontmatter.slug}.md`
    } else return false
  },
  urlForNote: ({ frontmatter }) => {
    if (frontmatter.public) {
      return `/<URL_TO_LINK_NOTES>/${frontmatter.slug}`
    } else return false
  },
  pathForFile: ({ mdastNode, /* note, file, */ extension, frontmatter }) => {
    if (frontmatter.slug && mdastNode.alt) {
      const fn = `${frontmatter.slug}_${toKebabCase(
        mdastNode.alt
      )}${extension}`
      const res = {
        filePath: `./<PATH_TO_EXPORT_IMAGES>/${fn}`,
        url: `./<URL_TO_LINK_IMAGES>/${fn}`
      }
      // If the `alt` attribute of the image is 'thumbnail', use it as a hero image
      if (mdastNode.alt === 'thumbnail') {
        frontmatter.heroImage = res.filePath
      }
      return res
    } else return false
  },
  postProcessNote: ({ md }) => {
    // Remove the thumbnail image from the Markdown body
    const md2 = md.replace(/\!\[thumbnail\]\(.*\)\n/, '')
    return md2
  }
})

If you would like to cancel/stop exporting:

sub.stop()

And run it:

node --experimental-vm-modules import.mjs

start() parameters

bookId: string

The notebook ID to export. Required.

live?: boolean

If true, it continuously exports as you change notes in Inkdrop. If false, it performs one-time export.

false by default.

pathForNote(data)

Generate a path to export the specified note

  • data.note: Note - The note to export
  • data.frontmatter: Record<string, any> - The YAML frontmatter of the note
  • data.tags: An array of Tag - The tags of the note
  • Returns: string | false | Promise<...> - A destination path to export. If it returns false, the note will be skipped exporting.

urlForNote(data)

Generate a URL for the specified note. It is necessary to link from the note to another note.

  • data.note: Note - The note to export
  • data.frontmatter: Record<string, any> - The YAML frontmatter of the note
  • data.tags: An array of Tag - The tags of the note
  • Returns: string | false | Promise<...> - A url/relative path. If it returns false, the note will be skipped processing.

pathForFile(data)

Generate a path and URL to export the specified image file.

  • data.note: Note - The note data
  • data.mdastNode: Image - The mdast node of the image
  • data.file: File - The attached image file data to export
  • data.extension: string - The file extension of the image (e.g., '.jpg', '.png')
  • data.frontmatter: Record<string, any> - The YAML frontmatter of the note
  • data.tags: An array of Tag - The tags of the note
  • Returns: { filePath: string; url: string } | false | Promise<...> - A destination file path to export and url to link. If it returns false, the image will be skipped exporting.

preProcessNote(data)

Pre-process the specified note. It is useful to update the frontmatter information based on the note metadata.

  • data.note: Note - The note data
  • data.frontmatter: Record<string, any> - The YAML frontmatter of the note
  • data.tags: An array of Tag - The tags of the note
  • data.mdast: Root - The mdast root node of the note
  • Returns: any | Promise<any>

postProcessNote(data)

Post-process the specified note right before writing the note to a file. It is useful to tweak the Markdown data (e.g., deleting unnecessary lines).

  • data.md: string - The Markdown data
  • data.frontmatter: Record<string, any> - The YAML frontmatter of the note
  • data.tags: An array of Tag - The tags of the note
  • Returns: string | Promise<string> - Returns the processed Markdown string

Debugging

Set environment variable DEBUG='inkdrop:export:info,inkdrop:export:error' to enable console outputs

FAQ

How can I see the access logs of the local server?

Run the app with a --enable-logging flag. See the documentation for more detail.

Can I import the notes back to Inkdrop?

No. As it transforms the notes for your projects, they are no longer compatible with Inkdrop.

inkdrop-live-export's People

Contributors

craftzdog avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

jiantingfeng

inkdrop-live-export's Issues

Headers() is not defined

Hi, Takuya,

I tried to use this package, however, it seems don't compatible with Node v16 (as the Prerequisites in README.md),

When I try to use node 16:
SCR-20220921-svr

It seems that Headers() is only supported by Node v18 and later, you can check the Browser compability

image

After upgrading my node version, everything works. So I think maybe you should update the prerequisites, or use some third-party packages to manually import Headers() in order to make this plugin work with Node v16. :)

ReferenceError: Headers is not defined

Description of issue:
Upon attempting to run live-export, I receive the follow error w/ stack trace:

file:///Users/jackhall/code-personal/void/node_modules/@inkdropapp/live-export/lib/index.js:27
        const headers = new Headers();
                        ^

ReferenceError: Headers is not defined
    at LiveExporter.callApi (file:///Users/jackhall/code-personal/void/node_modules/@inkdropapp/live-export/lib/index.js:27:25)
    at LiveExporter.getTags (file:///Users/jackhall/code-personal/void/node_modules/@inkdropapp/live-export/lib/index.js:61:21)
    at LiveExporter.start (file:///Users/jackhall/code-personal/void/node_modules/@inkdropapp/live-export/lib/index.js:277:33)
    at file:///Users/jackhall/code-personal/void/tools/import.mjs:16:18
    at ModuleJob.run (node:internal/modules/esm/module_job:195:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:337:24)
    at async loadESM (node:internal/process/esm_loader:88:5)
    at async handleMainPromise (node:internal/modules/run_main:61:12)
error Command failed with exit code 1.

When looking in the compiled output in node_modules, I noticed that the Headers is in fact not found in the module:

import debug from 'debug';
import fs from 'fs';
import { unified } from 'unified';
import remarkParse from 'remark-parse';
import remarkFrontmatter from 'remark-frontmatter';
import remarkStringify from 'remark-stringify';
import { visit } from 'unist-util-visit';
import yaml from 'js-yaml';

const logger = {
    debug: debug('inkdrop:export:debug'),
    info: debug('inkdrop:export:info'),
    error: debug('inkdrop:export:error')
};
const extractDocIdFromUri = (uri) => {
    const [, fileId] = uri.match(/inkdrop:\/\/([^\/]*)/) || [];
    return fileId;
};
class LiveExporter {
    constructor(config) {
        this.fileNameMap = {};
        this.tagMap = {};
        this.config = config;
    }
    async callApi(path, query = {}) {
        const { hostname, username, password, port } = this.config;
        const headers = new Headers();

...

I guess this is because the module was written using later version of node (17.5 with --experimental-fetch) than 16 where fetch is globally available, or perhaps some flag I am not using?

I was able to fix doing:

import fetch, { Headers } from 'node-fetch'

global.fetch = fetch
global.Headers = Headers

Please let me know the right way to fix this. Thanks.

404 for tags page

Hi, thanks for wonderful app.

I have inkdrop blog up and running but still can not make tags work.

I tried to add tag in Inkdrop app, then I stopped astro server and live-import task and run both tasks (server and live-import) again, but there are no tags exported.

Where could be the problem?

Typo in description

A library for programmatically exoprting notes to local filesystem from Inkdrop

Should be:

A library for programmatically exporting notes to local filesystem from Inkdrop

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.