Generate a markdown TOC (table of contents) with Remarkable
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Helper functions for custom TOC rendering and text processing. These utilities are exposed as static methods on the main toc function for advanced customization.
Generate URL-safe slugs from heading text with customizable processing.
/**
* Generate URL-safe slug from heading text
* @param {string} str - Text to slugify
* @param {Object} options - Slugify options
* @returns {string} URL-safe slug
*/
function slugify(str, options);Usage Examples:
const toc = require('markdown-toc');
// Basic slugification
console.log(toc.slugify('Hello World'));
// Output: "hello-world"
// Handle special characters
console.log(toc.slugify('API & SDK Guide'));
// Output: "api--sdk-guide"
// Handle HTML tags (stripped by default)
console.log(toc.slugify('<code>Installation</code>'));
// Output: "installation"
// Custom slugify function
const result = toc('# Some Article', {
slugify: function(str) {
return '!' + str.replace(/[^\w]/g, '-') + '!';
}
});
// Slug becomes: "!Some-Article!"Convert heading tokens into markdown links.
/**
* Convert heading into markdown link
* @param {Object} token - Heading token object
* @param {Object} options - Linking options
* @returns {Object} Token with linkified content
*/
function linkify(token, options);Usage Examples:
const toc = require('markdown-toc');
// Process individual heading token
const token = {
content: 'Getting Started',
slug: 'getting-started',
lvl: 2
};
const linkedToken = toc.linkify(token);
console.log(linkedToken.content);
// Output: "[Getting Started](#getting-started)"
// Custom linkify function
const result = toc(markdown, {
linkify: function(token, text, slug, opts) {
return `• [${text}](${slug})`;
}
});Render markdown list bullets from array of heading tokens.
/**
* Render bullet list from heading tokens
* @param {Array} tokens - Array of heading tokens
* @param {Object} options - Bullet formatting options
* @returns {string} Formatted bullet list markdown
*/
function bullets(tokens, options);Usage Examples:
const toc = require('markdown-toc');
// Generate TOC and use custom bullet rendering
const result = toc(markdown);
const customBullets = toc.bullets(result.json, {
bullets: ['-', '+', '*'],
indent: ' '
});
console.log(customBullets);
// Uses different bullets for different levels
// Numeric bullets
const numericBullets = result.json
.map((token, index) => {
const indent = ' '.repeat(token.lvl - result.highest);
return `${indent}${index + 1}. ${token.content}`;
})
.join('\n');Process and clean heading text for display.
/**
* Process and clean heading text for display
* @param {string} str - Heading text to process
* @param {Object} options - Processing options
* @returns {string} Cleaned heading text
*/
function titleize(str, options);Usage Examples:
const toc = require('markdown-toc');
// Basic titleize (removes HTML tags, cleans whitespace)
console.log(toc.titleize('<span>Hello World</span>'));
// Output: "Hello World"
// Custom titleize function
const result = toc(markdown, {
titleize: function(str) {
return str.toUpperCase();
}
});
// Disable titleize
const result2 = toc(markdown, {
titleize: false
});Strip specified words or characters from heading text.
/**
* Strip specified words or characters from heading text
* @param {string} str - Text to process
* @param {Object} options - Strip configuration
* @returns {string} Processed text
*/
function strip(str, options);Usage Examples:
const toc = require('markdown-toc');
// Strip with array of words
const result = toc(markdown, {
strip: ['API', 'Guide', 'Documentation']
});
// Strip with custom function
const result2 = toc(markdown, {
strip: function(str) {
return str.replace(/\[.*?\]/g, ''); // Remove bracketed text
}
});
// Direct usage of strip utility
console.log(toc.strip('API Documentation Guide', {
strip: ['API', 'Guide']
}));
// Output: "Documentation"Combine utility functions for completely custom TOC rendering.
const toc = require('markdown-toc');
function customTOC(markdown) {
// Generate base TOC data
const result = toc(markdown);
// Custom processing using utilities
const customEntries = result.json.map(heading => {
// Custom slug generation
const customSlug = toc.slugify(heading.content, {
slugify: str => str.toLowerCase().replace(/\s+/g, '_')
});
// Custom title processing
const customTitle = toc.titleize(heading.content, {
titleize: str => str.charAt(0).toUpperCase() + str.slice(1).toLowerCase()
});
// Custom link format
const indent = ' '.repeat(heading.lvl - result.highest);
return `${indent}- [${customTitle}](#${customSlug})`;
});
return customEntries.join('\n');
}
// Usage
const customResult = customTOC(`
# API REFERENCE
## USER management
### create USER
`);
console.log(customResult);
// Output:
// - [Api reference](#api_reference)
// - [User management](#user_management)
// - [Create user](#create_user)Handle complex slug generation scenarios.
const toc = require('markdown-toc');
// Handle duplicate headings with custom numbering
function customSlugify(str, options) {
let baseSlug = str.toLowerCase()
.replace(/[^\w\s-]/g, '')
.replace(/\s+/g, '-');
if (options.num > 0) {
baseSlug += `-v${options.num}`;
}
return baseSlug;
}
const result = toc(markdown, {
slugify: customSlugify
});
// Handles international characters
const intlSlugify = (str) => {
return str.normalize('NFD')
.replace(/[\u0300-\u036f]/g, '') // Remove diacritics
.toLowerCase()
.replace(/[^\w\s-]/g, '')
.replace(/\s+/g, '-');
};