@inkdropapp/protocol-utils
v1.0.1
Published
Protocol utils for inkdrop
Downloads
611
Readme
Inkdrop Protocol Utils
Install
npm install @inkdropapp/protocol-utilsUsage
Parse any Inkdrop URI
parseInkdropUri handles every Inkdrop URI type in one call: note, file, plugin, command, action and oauth.
import { parseInkdropUri } from '@inkdropapp/protocol-utils'
parseInkdropUri('inkdrop://note/foobar#L5')
// { resourceType: 'note', resourceId: 'note:foobar', anchor: { type: 'lines', startLine: 5, endLine: 5 } }
parseInkdropUri('inkdrop://file:xyz') // { resourceType: 'file', resourceId: 'file:xyz' }
parseInkdropUri('inkdrop://plugin/vim') // { resourceType: 'plugin', resourceId: 'vim' }
parseInkdropUri('inkdrop://command/core/open-note?noteId=abc')
// { resourceType: 'command', resourceId: 'core:open-note', query: { noteId: 'abc' } }
parseInkdropUri('command://core:new-note') // { resourceType: 'command', resourceId: 'core:new-note', query: {} }
parseInkdropUri('inkdrop://actions/save-link?url=x')
// { resourceType: 'action', resourceId: 'save-link', query: { url: 'x' } }
parseInkdropUri('inkdrop://oauth/github?nonce=abc')
// { resourceType: 'oauth', resourceId: 'github', query: { nonce: 'abc' } }
parseInkdropUri('https://example.com') // undefinedExtract note ID from URI
import { extractNoteIdFromURI } from '@inkdropapp/protocol-utils'
const uri = `inkdrop://note/foobar`
const noteId = extractNoteIdFromURI(uri)Get note URI
import { getNoteUri } from '@inkdropapp/protocol-utils'
const uri = getNoteUri('note:foobar')Note anchors (lines and headings)
A note URI can point to a location inside the note with a fragment:
inkdrop://note/foobar#L5— line 5inkdrop://note/foobar#L5-L8— lines 5 to 8inkdrop://note/foobar#my-heading— the heading whose slug ismy-heading(as generated byrehype-slug)
import { formatNoteAnchor, getNoteUri, parseNoteAnchor } from '@inkdropapp/protocol-utils'
parseNoteAnchor('inkdrop://note/foobar#L5-L8') // { type: 'lines', startLine: 5, endLine: 8 }
parseNoteAnchor('inkdrop://note/foobar#setup') // { type: 'heading', slug: 'setup' }
parseNoteAnchor('inkdrop://note/foobar') // undefined
getNoteUri('note:foobar', { anchor: { type: 'lines', startLine: 5, endLine: 8 } })
// 'inkdrop://note/foobar#L5-L8'
formatNoteAnchor({ type: 'heading', slug: 'setup' }) // 'setup'Extract plugin name from URI
import { extractPluginFromURI } from '@inkdropapp/protocol-utils'
const uri = `inkdrop://plugin/mermaid`
const plugin = extractPluginFromURI(uri)File (attachment) URIs
import {
getFileDocIdFromUri,
isFileUri,
getFileUri,
extractFileIdsFromMarkdown,
resolveFileProtocolUri
} from '@inkdropapp/protocol-utils'
isFileUri('inkdrop://file:foobar') // true
getFileDocIdFromUri('inkdrop://file:foobar') // 'file:foobar'
getFileUri('file:foobar') // 'inkdrop://file:foobar'
extractFileIdsFromMarkdown('') // ['file:foobar']
resolveFileProtocolUri('inkdrop://file:foobar') // 'inkdrop-file://foobar'
resolveFileProtocolUri('https://example.com/a.png') // unchanged