Markdown Utilities for TypeScript
`md-utils-ts` is a lightweight, TypeScript-first utility library designed for programmatic generation of common Markdown syntax elements. Currently at version 2.0.0, it provides a focused collection of functions to create bold text, italics, strikethroughs, underlines, anchor links, code blocks (inline and multi-line), equations, and headings (H1-H6). The library emphasizes simplicity and type safety, offering both individual named exports for each utility (e.g., `bold`, `h1`) and a bundled `md` object for convenience (e.g., `md.italic`). Unlike comprehensive Markdown parsers or renderers, `md-utils-ts` strictly focuses on outputting valid Markdown strings, making it ideal for scenarios where you need to construct Markdown content dynamically without heavy dependencies. Its release cadence is likely stable, with updates driven by new Markdown syntax needs or minor enhancements.
Common errors
-
TypeError: (0 , md_utils_ts__WEBPACK_IMPORTED_MODULE_0__.bold) is not a function
cause Attempting to use a named export (e.g., `bold`) directly from a default import, or incorrect CommonJS `require` syntax.fixEnsure named exports are correctly destructured: `import { bold } from 'md-utils-ts';`. If using CommonJS (not recommended for this ESM-first library), access named exports as properties: `const { bold } = require('md-utils-ts');`. -
TS2305: Module '"md-utils-ts"' has no exported member 'md'.
cause Attempting to import `md` as a named export when it is the default export of the package.fixImport the default export correctly: `import md from 'md-utils-ts';`. -
Module not found: Error: Can't resolve 'md-utils-ts'
cause The package `md-utils-ts` is not installed in the project's `node_modules` directory or the package name is misspelled in the import statement.fixRun `npm install md-utils-ts` or `yarn add md-utils-ts` to install the package. Double-check the spelling of the import path. -
TypeError: md.bold is not a function
cause Attempting to call a utility function (e.g., `bold`) directly as a property of the `md` default import when it's also available as a named export, leading to confusion or incorrect binding.fixIf you are using the `md` default import, access its properties correctly, e.g., `md.bold("text")`. If you prefer direct named imports, use `import { bold } from 'md-utils-ts'` and then `bold("text")`.
Warnings
- gotcha md-utils-ts explicitly states that it does not support Markdown table generation directly. Attempting to create complex tables will require manual string formatting or integration with another library.
- gotcha This library is strictly for *generating* Markdown strings. It does not provide functionality for *parsing* existing Markdown content or *rendering* Markdown into HTML or other formats.
- gotcha The `code` and `equation` functions are higher-order functions that require an initial boolean argument (`inline`) to determine output format, returning another function that takes the `language`/`text`.
Install
-
npm install md-utils-ts -
yarn add md-utils-ts -
pnpm add md-utils-ts
Imports
- bold
const bold = require('md-utils-ts').boldimport { bold } from 'md-utils-ts' - md
import { md } from 'md-utils-ts'import md from 'md-utils-ts'
- codeBlock
import codeBlock from 'md-utils-ts'
import { codeBlock } from 'md-utils-ts'
Quickstart
import md, { bold, h1, anchor, codeBlock, equationBlock } from "md-utils-ts";
// Create a main heading
const title = h1("Welcome to md-utils-ts!");
// Bold and italic text using individual exports and the 'md' object
const importantText = bold("This text is important.") + " " + md.italic("And this is emphasized.");
// Create an anchor link
const githubLink = anchor("md-utils-ts GitHub", "https://github.com/TomPenguin/md-utils-ts");
// Generate a TypeScript code block
const tsExample = codeBlock("typescript")(`
function greet(name: string): string {
return \`Hello, \${name}!\`;
}
console.log(greet("World"));
`);
// Create an equation block
const pythagoreanTheorem = equationBlock("a^2 + b^2 = c^2");
console.log(title);
console.log(importantText);
console.log(githubLink);
console.log("\n--- TypeScript Example ---\n" + tsExample);
console.log("\n--- Math Example ---\n" + pythagoreanTheorem);