Skip to content

Node API

The MDNMan Node API allows developers to integrate MDN Web Docs into their own Node.js applications. This API provides programmatic access to query, parse, and render MDN documentation in a customizable format — perfect for building dev tools, chat bot integrations, or command-line utilities.

These methods retrieve content from the MDN reference library.

Get an MDN reference document based on an explicit filepath to the MDNMan library.

filepath

Full path to the MDNMan library. Starts with ‘lib’ and ends with ‘index.md’. If ‘index.md’ is not attached, the function will attempt to add it to the end of the provided path.

example: ‘lib/javascript/global_objects/string/split/index.md’

Either the MDN file that was found, or null if the path was invalid.

const file = getMDNFile('lib/javascript/global_objects/map/index.md');
if (file) {
console.log(file);
}

Searches the MDNMan library for a directory with a name containing the search query. If there is a single match, that document is returned. If there are multiple matches, the user is prompted to select on.

technology

‘javascript’, ‘html’, or ‘css’

See SupportedLanguages type

query

The search term used to query the MDNMan library

Either the MDN file that was found, or null if no file was found with the given query.

const file = await findMDNFile('javascript', 'foreach');
if (file) {
console.log(file);
}

Search the MDNMan library for a directory with a name containing the users search. Return the contents of the file in that directory. If multiple files are found, return the first one.

technology

‘javascript’, ‘html’, or ‘css’

See SupportedLanguages type

query

The search term used to query the MDNMan library

Either the MDN file that was found, or null if no file was found with the given query.

const file = optimisticallyFindMDNFile('css', 'font-family');
if (file) {
console.log(file);
}

These methods modify or extract parts of MDN content to make it easier to render, display, or transform into different formats.

Returns a specified section of a provided MDN reference document.

document

The MDN reference document

inputSection

An object containing defining information about the desired section

See (MDNSection type)

Either the MDN section that was found, or null if no section was found in the provided document.

const file = await findMDNFile('foreach');
if (!file) return;
const section = getSection(file, {
name: 'Syntax',
level: 2,
position: 1,
});
if (section) {
console.log(section);
}

Returns the first section of a provided MDN reference document that matches the provided name.

document

The MDN reference document

sectionName

Name of the section to retrieve

Either the MDN section that was found, or null if no section was found in the provided document.

const file = await findMDNFile('foreach');
if (!file) return;
const section = getNamedSection(file, 'Syntax');
if (section) {
console.log(section);
}

Returns a list of all sections in a provided MDN reference document.

document

The MDN reference document to be parsed

A list of all sections in the provided document. If no sections are found, an empty list is returned.

See (#MDNSection type)

const file = await findMDNFile('foreach');
if (!file) return;
const sections = getAllSections(file);
sections.forEach(section => console.log(section.name))

Returns the introduction text of a provided MDN reference document. This is all the text until the first section heading.

document

The MDN reference document

All the text in the MDN reference document up to the first section heading. Does not include the page header.

const file = await findMDNFile('foreach');
if (!file) return;
const intro = getIntroSection(file);
console.log(intro);

Removes a single section for the provided MDN reference document and returns the updated document.

document

The MDN reference document

sectionName

The name of the section to remove

The MDN reference document with the named section removed. If the section is not found, the document is returned un-changed.

const file = await findMDNFile('foreach');
if (!file) return;
const trimmedFile = removeSection(file, 'See Also');
console.log(trimmedFile);

Removes all sections that have no inner content from an MDN reference document and returns the updated document.

document

The MDN reference document

The MDN reference document with empty sections removed. If no empty sections are found, the document is returned un-changed.

const file = await findMDNFile('foreach');
if (!file) return;
const trimmedFile = removeEmptySections(file);
console.log(trimmedFile);

Parses the markdown style header from a provided MDN reference document and returns an object containing the useful information from it.

document

The MDN reference document

An object containing useful information from the documents header. If no header is found, null is returned.

See (#MDNHeader type)

const file = await findMDNFile('foreach');
if (!file) return;
const header = getHeader(file);
if (header) {
console.log(header.title)
}

Removes the markdown style header from a provided MDN reference document

document

The MDN reference document

addHeading

Adds an H1 markdown heading with the header title when true. Defaults to true.

The MDN reference document with the header removed. If no header is found, the document is returned un-changed.

const file = await findMDNFile('foreach');
if (!file) return;
const trimmedFile = stripHeader(file, false);
console.log(trimmedFile)

Replaces Kumascript macros with human readable text and/or links. Most basic macros such as internal MDN links are handled. If they are not, the macro is removed from the document.

example: {{cssxref(“div”)}} → div

document

The MDN reference document

addLinks

Replaces Kumascript macros with a markdown link when true. Replaces with text when false.

The MDN reference document with Kumascript macros transformed.

const file = await findMDNFile('foreach');
if (!file) return;
const parsedFile = transformKumascript(file, false);
console.log(parsedFile)

Converts all shortened MDN links into full URLs

document

The MDN reference document

slug

The document’s header slug. Can be retrieved with getHeader

The updated MDN reference document

const file = await findMDNFile('foreach');
if (!file) return;
const header = getHeader(file);
if (!header) return;
const parsedFile = expandLinks(file, header.slug);
console.log(parsedFile)

Converts markdown style emoji tags into real emojis.

example: [!Warning] → ⚠️

document

The MDN reference document

The updated MDN reference document

const file = await findMDNFile('foreach');
if (!file) return;
const parsedFile = convertEmojiTags(file);
console.log(parsedFile)

Cuts a provided MDN reference document down to the provided character length. If the length of the document is longer than the provided length, ’…’ will be appended to the string. If the truncation happens in the middle of a link, the entire link is removed.

document

The MDN reference document

length

The number of character the final document should be

The truncated MDN reference document

const file = await findMDNFile('foreach');
if (!file) return;
const truncatedFile = truncateString(file, 1024);
console.log(truncatedFile)

Removes triple newlines from the document. This prevent more than one empty line in a row.

document

The MDN reference document

The modified MDN reference document

const file = await findMDNFile('foreach');
if (!file) return;
const trimmedFile = removeEmptyLines(file);
console.log(trimmedFile)

Removes any codeblocks from the document that have a “hidden” label.

document

The MDN reference document

The updated MDN reference document

const file = await findMDNFile('foreach');
if (!file) return;
const trimmedFile = removeHiddenCodeblocks(file);
console.log(trimmedFile)

Replaces codeblock fences with standard languages. These supported languages are: js, css, html, json and txt.

document

The MDN reference document

The modified MDN reference document

const file = await findMDNFile('foreach');
if (!file) return;
const transformedFile = transformCodeblockLangs(file);
console.log(transformedFile)

Combines several parsing functions into a single command for ease of use. The following commands are run:

document

The MDN reference document

slug

The document’s header slug. Can be retrieved with getHeader

expandKumaLinks

Whether to turn Kumascript macros into links or text. Defaults to true

The parsed MDN reference document

const file = await findMDNFile('foreach');
if (!file) return;
const header = getHeader(file);
if (!header) return;
const parsedFile = completeParse(file, header.slug, false);
console.log(parsedFile)