dom2md
v0.2.0
Published
Convert the live DOM of a page to Markdown. Heuristic, dependency-free, works in the browser and as a content script.
Maintainers
Readme
dom2md
Turn the live DOM of a page into Markdown. Heuristic, dependency-free, and
tiny — designed to run in the browser (as a <script> tag or a content script),
not on a server.
It walks the DOM, collects leaf text blocks plus structural elements (tables,
lists with nesting/numbering, <hr>, <audio>/<video>), drops boilerplate
(nav/footer/aside, ads, chat/cookie widgets), and derives heading levels
(#–######) from each heading's font size relative to the page's base font.
Install
npm install dom2mdUsage
ESM / CJS
import { dom2md } from 'dom2md';
// const { dom2md } = require('dom2md'); // CommonJS
const markdown = dom2md(); // converts document.body
const partial = dom2md({ root: document.querySelector('article') });Browser <script> tag
The package ships a standalone build at dom2md/global (dist/dom2md.global.js).
Include it and call the global dom2md():
<script src="node_modules/dom2md/dist/dom2md.global.js"></script>
<script>
const md = dom2md();
</script>(It also exposes window.dom2md.)
Content script (browser extension)
Because the standalone build defines window.dom2md, you can drop it into
a content_scripts entry exactly like the original extension:
{
"content_scripts": [
{ "matches": ["<all_urls>"], "js": ["dom2md.global.js", "content.js"] }
]
}// content.js — dom2md() is now a global
button.onclick = () => {
const markdown = dom2md();
// ...
};API
dom2md(options?)
Returns the page (or subtree) as a Markdown string.
interface Options {
root?: Node | null; // root to convert, defaults to document.body
minTextLength?: number; // minimum text length to keep a block, defaults to 2
}How it works
- Collect leaf text blocks (elements with text and no block-level children) and structural elements.
- Filter hidden / zero-sized blocks and boilerplate (
nav/footer/aside, ad/chat/cookie widgets by id/class). - Classify text blocks: headings (level from font size vs. the page's base font), quotes, code, paragraphs.
- Render Markdown with inline formatting (bold/italic/code/links/images), escaping literal special characters.
Build
npm install
npm run build # emits dist/ (esm + cjs + iife + types)dist/ is committed so consumers can grab dist/dom2md.global.js directly from
the package without building.
License
MIT
