npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ts-markdown-parser

v1.2.0

Published

TypeScript library that converts markdown to HTML (with code support).

Downloads

275

Readme

ts-markdown-parser

ts-markdown-parser is a TypeScript-based library for converting Markdown to HTML with built-in syntax highlighting for various programming languages. Currently, it supports HTML, JavaScript, TypeScript, and Python code highlighting, with plans to expand to more languages in the future.

Installation

To install the package, run:

npm install ts-markdown-parser

Usage

Here's a basic example of how to use ts-markdown-parser to convert Markdown to HTML:

const { markdownToHtml } = require("ts-markdown-parser");

// Your Markdown content
const markdown = `
# Sample Heading

\`\`\`javascript
console.log("Hello, world!");
\`\`\`

## Subheading

Some text.

![Alt text](https://example.com/image.png)

[Link text](https://example.com)
`;

// Convert Markdown to HTML
const htmlContent = markdownToHtml(markdown);

// Basic HTML template
const htmlTemplate = `
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Markdown Test</title>
<style>
  body {
    font-family: Arial, sans-serif;
    margin: 20px;
    line-height: 1.6;
    color: #333;
    background-color: #f9f9f9;
  }

  img {
    max-width: 3rem;
  }

  h1, h2, h3, h4 {
    color: #444;
    margin-bottom: 15px;
    border-bottom: 2px solid #ddd;
    padding-bottom: 5px;
  }

  h1 { font-size: 2em; }
  h2 { font-size: 1.75em; }
  h3 { font-size: 1.5em; }
  h4 { font-size: 1.25em; }

  p {
    margin: 15px 0;
  }

  ul, ol {
    margin: 15px 0;
    padding-left: 40px;
  }

  li {
    margin-bottom: 10px;
  }

  .md-inline-code {
    background: #eaeaea;
    color: #d63384;
    padding: 2px 4px;
    border-radius: 4px;
    font-family: 'Courier New', Courier, monospace;
    font-size: 0.9em;
  }

  pre {
    background: #2d2d2d;
    padding: 15px;
    border-radius: 8px;
    color: #f8f8f2;
    overflow-x: auto;
    font-size: 0.9em;
  }

  .md-code {
    background: #2d2d2d;
    color: #f8f8f2;
    padding: 5px 8px;
    border-radius: 4px;
    display: block;
    white-space: pre-wrap;
    font-family: 'Courier New', Courier, monospace;
  }

  .md-code-container {
    margin-bottom: 20px;
    position: relative;
  }

  .md-code-container button {
    position: absolute;
    top: 10px;
    right: 10px;
    background-color: #5a5a5a;
    color: #f8f8f2;
    border: none;
    padding: 5px 10px;
    border-radius: 5px;
    cursor: pointer;
    font-size: 0.85em;
  }

  .md-code-container button:hover {
    background-color: #444;
  }

  /* Syntax highlighting */
  .md-keyword { color: #66d9ef; font-weight: bold; }
  .md-string { color: #a6e22e; }
  .md-number { color: #ae81ff; }
  .md-comment { color: #909090; font-style: italic; }
  .md-special { color: #FF69B4; font-style: bold; }
  .md-class { color: #39FF14; font-style: bold; }
  .md-decorator { color: magenta; font-style: normal; }
  .md-regex { color: red; font-style: normal; }
  .md-call-method { color: yellow; font-style: bold; }
</style>
</head>
<body>
  ${htmlContent}
</body>
</html>
`;

// Output HTML to a file or use as needed

TypeScript Usage

Here's how you might use it in your TypeScript code:

import * as fs from "fs";
import { markdownToHtml, getMarkdownMetadata } from "ts-markdown-parser";
// ...
const file = "/path/to/my-markdown.md";
const markdownString = fs.readFileSync(file, "utf8");
const htmlString: string | undefined = markdownToHtml(markdownString);

if (!htmlString) {
  console.error(`Failed to generate HTML from: ${file}.`);
}

const metadata: Record<string, any> = getMarkdownMetadata(markdownString);
if (!metadata?.title) {
  console.error(`Markdown file '${file}' is missing metadata.`);
}

npx Command to Test Code

Use npx ts-node to test a TypeScript usage of this package:

npx ts-node test.ts

Metadata Parsing

You can also extract metadata from your Markdown files using YAML front matter.

Here's how to use the getMarkdownMetadata() function:

const { getMarkdownMetadata } = require("ts-markdown-parser");

// Your Markdown content with YAML front matter
const markdown = `
---
title: "Example Markdown Article for HTML"
author: "ChatGPT"
date: 2024-08-19
keywords: scripting, html, web development, markdown, blog
slug: markdown-article-test-for-html
---

# Sample Heading

Some text.
`;

// Extract metadata
const metadata = getMarkdownMetadata(markdown);

console.log(metadata);

The metadata object literal response should look something like this:

{
  title: 'Example Markdown Article for HTML',
  author: 'ChatGPT',
  date: '2024-08-19', // Date formatted as string
  keywords: [ 'scripting', 'html', 'web development', 'markdown', 'blog' ],
  slug: 'markdown-article-test-for-html'
}

Example YAML Front Matter

YAML front matter is a way to include metadata at the top of your Markdown files. Here's another example:

---
title: "Example Markdown Article for HTML"
author: "ChatGPT"
date: 2024-08-19
id: "1234"
keywords: scripting, html, web development, markdown, blog
slug: markdown-article-test-for-html
---

Metadata Extraction Result

Given the YAML front matter above, getMarkdownMetadata would return:

{
  "title": "Example Markdown Article for HTML",
  "author": "ChatGPT",
  "date": "2024-08-19", // Date formatted as string
  "id": 1234, // String representation of number parsed and casted as number
  "keywords": ["scripting", "html", "web development", "markdown", "blog"],
  "slug": "markdown-article-test-for-html"
}

Features

  • Markdown Elements Supported:

    • Headers (1-5)
    • Ordered and unordered lists
    • Bold and italic text
    • Inline code and code blocks
    • Images (![Alt text](url))
    • Links ([Link text](url))
  • Code Highlighting Supported For:

    • HTML
    • CSS/SCSS
    • JavaScript
    • TypeScript
    • Python
    • Golang
  • Metadata Parsing:

    • Extract metadata from YAML front matter.
    • Handles title, author, date, keywords, slug, and more.
  • Note: Table/grid support is not yet available.

Screenshots

When you properly apply styles to the generated md-* CSS classes, the final HTML should look something like this:

Python Markdown-to-HTML Example

Markdown Sample Markdown-to-HTML with Image

HTML Code Block Markdown-to-HTML Example

Contributing

We welcome suggestions and feedback, but all code contributions require prior permission. Please submit issues or feature requests, and if you would like to contribute code, contact the repository owner for permission.

License

This project is licensed under the MIT License. Feel free to use the code in your own projects or fork it, but contributions to the repository are managed by the project owner and require permission.