Tiny markdown utility functions for Typescript.
Install with npm:
$ npm install --save md-utils-ts
See Implementation for details.
import md, { bold } from "md-utils-ts";
const boldText = bold("some text");
console.log(boldText); // "**some text**"
// Use function from the imported 'md'
const italicText = md.italic("Hello, world!");
console.log(italicText); // "_Hello, world!_"
Note: Table is not supported. You can use markdown-table as well.
Make the text bold.
text
(string): The input text.
const result = bold("Hello, world!");
// Output: "**Hello, world!**"
Make the text italic.
text
(string): The input text.
const result = italic("Hello, world!");
// Output: "_Hello, world!_"
Add strike-through to the text.
text
(string): The input text.
const result = del("Hello, world!");
// Output: "~~Hello, world!~~"
Add underline to the text.
text
(string): The input text.
const result = underline("Hello, world!");
// Output: "<u>Hello, world!</u>"
Create an anchor link.
text
(string): The anchor text.href
(string): The URL to link to.
const result = anchor("OpenAI", "https://www.openai.com");
// Output: "[OpenAI](https://www.openai.com)"
Create a code block or inline code.
inline
(boolean): Whether the code should be inline or in a block.language
(string): The code language for syntax highlighting.text
(string): The code content.
const tsCodeBlock = code(false)("ts");
const result = tsCodeBlock("console.log('Hello, world!');");
// Output:
// "```ts
// console.log('Hello, world!');
// ```"
Create inline code with optional syntax highlighting.
text
(string): The code content.
const code = inlineCode("console.log('Hello, world!');");
// Output:
// "`console.log('Hello, world!');`"
Create a code block with optional syntax highlighting.
language
(string, optional): The code language for syntax highlighting.text
(string): The code content.
const code = codeBlock("ts")("console.log('Hello, world!');");
// Output:
// "```ts
// console.log('Hello, world!');
// ```"
Create an equation block or inline equation.
inline
(boolean): Whether the equation should be inline or in a block.text
(string): The equation content.
const equationBlock = equation(false)("x^2 + y^2 = z^2");
// Output:
// "$$
// x^2 + y^2 = z^2
// $$"
const inlineEquation = equation(true)("E = mc^2");
// Output: "$E = mc^2$"
Create inline code with optional syntax highlighting.
text
(string): The equation content.
const result = inlineEquation("E = mc^2");
// Output: "$E = mc^2$"
Create an equation block or inline equation.
text
(string): The equation content.
const result = equationBlock("x^2 + y^2 = z^2");
// Output:
// "$$
// x^2 + y^2 = z^2
// $$"
Create a heading with the specified level.
level
(number): The level of the heading (1 to 6).text
(string): The heading text.
const heading = h(2)("Hello, world!");
// Output: "## Hello, world!"
Create a level 1 heading.
text
(string): The heading text.
const heading = h1("Title");
// Output:
// # Title
Create a level 2 heading.
text
(string): The heading text.
const heading = h2("Subtitle");
// Output:
// ## Subtitle
Create a level 3 heading.
text
(string): The heading text.
const heading = h3("Subsection");
// Output:
// ### Subsection
Create a level 4 heading.
text
(string): The heading text.
const heading = h4("Subsubsection");
// Output:
// #### Subsubsection
Create a level 5 heading.
text
(string): The heading text.
const heading = h5("Subsubsubsection");
// Output:
// ##### Subsubsubsection
Create a level 6 heading.
text
(string): The heading text.
const heading = h6("Subsubsubsubsection");
// Output:
// ###### Subsubsubsubsection
Convert text to a blockquote.
text
(string): The input text.
const quotedText = quote("This is a quoted text.");
// Output:
// > This is a quoted text.
Create a bullet point list item.
text
(string): The content of the bullet point.count
(number, optional): The optional index/count of the bullet point.
const bulletPoint = bullet("List item");
// Output:
// - List item
const numberedBulletPoint = bullet("List item", 1);
// Output:
// 1. List item
Create a todo list item.
text
(string): The content of the todo item.checked
(boolean): Whether the todo item is checked or not.
const uncheckedTodo = todo("Task to be done", false);
// Output:
// - [ ] Task to be done
const checkedTodo = todo("Completed task", true);
// Output:
// - [x] Completed task
Create an image element.
alt
(string): The alt text for the image.href
(string): The URL of the image.
const imageElement = image("Description", "https://example.com/image.jpg");
// Output:
// ![Description](https://example.com/image.jpg)
Create a horizontal divider.
const dividerElement = divider();
// Output:
// ---
Create a collapsible details element.
summary
(string): The summary text for the details element.details
(string): The details/content of the details element.
const detailsElement = details("Click to expand", "Hidden content");
// Output:
// <details>
// <summary>Click to expand</summary>
//
// Hidden content
// </details>
Create a superscript text.
text
(string): The input text.
const superscriptText = sup("2");
// Output:
// <sup>2</sup>
Create a subscript text.
text
(string): The input text.
const subscriptText = sub("2");
// Output:
// <sub>2</sub>
Indent the text with a specified number of spaces.
space
(number, default: 2): The number of spaces to indent with.text
(string): The input text.level
(number, default: 1): The level of indentation.
const indentedText = indent(4)("Indented text", 2);
// Output:
// " Indented text"