@amoklab/bookmark-parser
v0.0.3
Published
Bookmark parser
Readme
bookmark-parser
Zero dependency bookmark parser exports into a shared bookmark node array.
Install
pnpm add @amoklab/bookmark-parserAPI
The package exports:
htmlBookmarkParser(text, options?)jsonBookmarkParser(text, options?)flattenBookmark(nodes, options?)BookmarkEntryBookmarkFolderBookmarkNodeParserOptions
Data model
All parsers return the same node shape:
BookmarkFolderfor foldersBookmarkEntryfor linksBookmarkNodefor either one
Folders keep their nested children. Both folders and bookmarks can keep a prevNode chain for tree navigation.
The parsers return only the export's bookmark children, not a synthetic root node.
jsonBookmarkParser
Parses the Firefox JSON export format.
import { jsonBookmarkParser } from '@amoklab/bookmark-parser';
const bookmarks = jsonBookmarkParser(firefoxJsonText);
const flattened = jsonBookmarkParser(firefoxJsonText, {
flatten: true,
});The parser expects a Firefox bookmarks JSON export and returns the top-level bookmark nodes.
htmlBookmarkParser
Parses Netscape bookmark HTML exports produced by both Chrome and Firefox.
import { htmlBookmarkParser } from '@amoklab/bookmark-parser';
const bookmarks = htmlBookmarkParser(bookmarksHtmlText);
const flattened = htmlBookmarkParser(bookmarksHtmlText, {
flatten: true,
});Options
ParserOptions is accepted by the parsers:
export interface ParserOptions {
flatten?: boolean;
setPrevNode?: (node: BookmarkNode) => any;
}flatten: whentrue, parsers return a flattened bookmark arraysetPrevNode: optional hook that transforms theprevNodeobject used during parsing flattening
flattenBookmark(nodes, options?) accepts the setPrevNode hook only and still returns a flat preorder list.
