Skip to content

Repository files navigation

nbff-parser

A simple parser for the Netscape Bookmark File Format (NBFF), commonly generated by browsers when exporting bookmarks.

Parses NBFF into structured data (with customizable formats), serializes structured data to NBFF, and merges multiple NBFF files.

Features

  • Small. From 280 B (minified + Brotli compressed), with no dependencies.
  • Modern. ES modules and tree shaking support.
  • TypeScript-ready. Full type definitions included.

Contents

Install

# npm
npm install nbff-parser
# pnpm
pnpm add nbff-parser
# yarn
yarn add nbff-parser

API

The parser expects HTML file content to be provided as a string.

Returns bookmark attributes with lowercase names and normalized values. See definition.

parse

Type definition

Parses bookmarks into a nested tree structure.

import{parse}from'nbff-parser'constbookmarks=parse(html)
Result schema
{
"title": "Folder",
"items": [
{
"title": "Bookmark"
},
{
"title": "Nested Folder",
"items": [
{
"title": "Another Bookmark"
}
]
}
]
}

Options:

OptionTypeDescription
excludeAttrsAllAttrKeys[]Excludes specified attributes from output.
withIdbooleanAdds hierarchical identifiers id and pid to each item.
transform(x: Bookmark) => TTransforms bookmarks, omitting falsy returns.
noEmptybooleanSkips empty folders in the result.

flatParse

Type definition

Parses bookmarks into a flat list, where each bookmark includes a folder stack representing its location path. Empty folders are omitted.

import{flatParse}from'nbff-parser'constbookmarks=flatParse(html)
Result schema
[
{
"title": "Bookmark",
"folder": [
{
"title": "Folder"
}
]
},
{
"title": "Another Bookmark",
"folder": [
{
"title": "Folder"
},
{
"title": "Nested Folder"
}
]
}
]

Options:

OptionTypeDescription
excludeAttrsAllAttrKeys[]Excludes specified attributes from output.
withIdbooleanAdds incremental numeric id to items.
transform(x: FlatBookmark) => TTransforms bookmarks, omitting falsy returns.

customParse

Type definition

Allows you to define handlers for elements during parsing.

Use this to build custom data structures or implement custom logic.

The methods described above rely on it internally.

import{customParse}from'nbff-parser'consthandlers={
openFolder,// <H1>, <H3>
addBookmark,// <A>
describeBookmark,// <DD>
closeFolder // </DL>}constbookmarks=customParse(html,handlers)

stringify

Type definition

Converts the parsed tree structure from parse back into an HTML string.

import{parse,stringify}from'nbff-parser'constparsed=parse(html)conststringified=stringify(parsed)// `stringified` matches the original `html`

flatStringify

Type definition

Converts the flat list from flatParse back into an HTML string.

It requires using flatParse with { withId: true } to ensure unique items.

import{flatParse,flatStringify}from'nbff-parser'constparsed=flatParse(html,{withId: true})conststringified=flatStringify(parsed)// `stringified` matches the original `html`

merge

Type definition

Merges parsed files into a single HTML string.

import{merge}from'nbff-parser'constmerged=merge(html1,html2, ...)

CLI

CLI methods work with actual files.

Usage:

npx nbff-parser <command> [options]

exclude

Removes specified attributes from the HTML file.

ArgumentDescriptionStatus
file=path/to/filePath to the input HTML fileRequired
attrs=attr1,attr2,...Comma-separated list of attributes to excludeRequired
output=path/to/outputPath to save the output file; defaults to input fileOptional

Example:

npx nbff-parser exclude \
file=index.html \
attrs=add_date,icon \
output=cleaned.html

merge

Merges multiple files into a single output file.

ArgumentsDescriptionStatus
files=path/to/file,...Comma-separated list of input file paths to mergeRequired
output=path/to/output Path to save the merged output fileRequired

Example:

npx nbff-parser merge \
files=foo.html,bar.html \
output=merged.html

Acknowledgments

About

A Netscape Bookmarks parser supporting multi-format export, HTML re-conversion, and file merging

Topics

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages