190 lines
6.1 KiB
Markdown
190 lines
6.1 KiB
Markdown
# @push.rocks/smartmarkdown
|
|
|
|
Markdown utilities for modern TypeScript projects: parse Markdown into HTML, read YAML frontmatter, and convert HTML back into clean GitHub-flavored Markdown.
|
|
|
|
`@push.rocks/smartmarkdown` wraps the Unified/Remark ecosystem for Markdown parsing and the Turndown ecosystem for HTML-to-Markdown conversion behind a small, typed API that works in Node.js and browser-oriented ESM builds.
|
|
|
|
## Issue Reporting and Security
|
|
|
|
For reporting bugs, issues, or security vulnerabilities, please visit [community.foss.global/](https://community.foss.global/). This is the central community hub for all issue reporting. Developers who sign and comply with our contribution agreement and go through identification can also get a [code.foss.global/](https://code.foss.global/) account to submit Pull Requests directly.
|
|
|
|
## Install
|
|
|
|
```bash
|
|
pnpm add @push.rocks/smartmarkdown
|
|
```
|
|
|
|
## What It Does
|
|
|
|
`@push.rocks/smartmarkdown` is intentionally focused:
|
|
|
|
- Convert Markdown strings to HTML.
|
|
- Parse YAML frontmatter into a JavaScript object.
|
|
- Preserve access to the original Markdown source.
|
|
- Convert HTML strings back to Markdown using ATX headings and fenced code blocks.
|
|
- Support GitHub-flavored Markdown through `remark-gfm` and `turndown-plugin-gfm`.
|
|
|
|
## Quick Start
|
|
|
|
```typescript
|
|
import { SmartMarkdown } from '@push.rocks/smartmarkdown';
|
|
|
|
const html = await SmartMarkdown.easyMarkdownToHtml(`# Hello Markdown
|
|
|
|
- fast
|
|
- typed
|
|
- practical
|
|
`);
|
|
|
|
console.log(html);
|
|
// <h1>Hello Markdown</h1>
|
|
// <ul>
|
|
// <li>fast</li>
|
|
// <li>typed</li>
|
|
// <li>practical</li>
|
|
// </ul>
|
|
```
|
|
|
|
## API
|
|
|
|
### `SmartMarkdown.easyMarkdownToHtml(markdown)`
|
|
|
|
Static convenience method for the most common path: Markdown in, HTML out.
|
|
|
|
```typescript
|
|
import { SmartMarkdown } from '@push.rocks/smartmarkdown';
|
|
|
|
const html = await SmartMarkdown.easyMarkdownToHtml('# Hi!');
|
|
|
|
console.log(html);
|
|
// <h1>Hi!</h1>
|
|
```
|
|
|
|
### `new SmartMarkdown().getMdParsedResultFromMarkdown(markdown)`
|
|
|
|
Parses a Markdown string into an `MdParsedResult` instance.
|
|
|
|
```typescript
|
|
import { SmartMarkdown } from '@push.rocks/smartmarkdown';
|
|
|
|
const smartMarkdown = new SmartMarkdown();
|
|
|
|
const result = await smartMarkdown.getMdParsedResultFromMarkdown(`---
|
|
title: Smart Docs
|
|
published: true
|
|
tags:
|
|
- markdown
|
|
- docs
|
|
---
|
|
|
|
# Smart Docs
|
|
|
|
Markdown with metadata.
|
|
`);
|
|
|
|
console.log(result.originalString); // the original Markdown input
|
|
console.log(result.html); // rendered HTML
|
|
console.log(result.frontmatterData); // { title: 'Smart Docs', published: true, tags: ['markdown', 'docs'] }
|
|
```
|
|
|
|
The returned object exposes:
|
|
|
|
- `originalString`: the Markdown input string.
|
|
- `html`: the rendered HTML output.
|
|
- `frontmatterData`: parsed YAML frontmatter as `Record<string, unknown>`.
|
|
- `title`: currently initialized as an empty string for consumers that want to attach title metadata.
|
|
|
|
### `new SmartMarkdown().htmlToMarkdown(html)`
|
|
|
|
Converts HTML back to Markdown using Turndown with GitHub-flavored Markdown support.
|
|
|
|
```typescript
|
|
import { SmartMarkdown } from '@push.rocks/smartmarkdown';
|
|
|
|
const smartMarkdown = new SmartMarkdown();
|
|
|
|
const markdown = smartMarkdown.htmlToMarkdown(`
|
|
<h1 id="hello">Hello</h1>
|
|
<p>This came from HTML.</p>
|
|
<ul>
|
|
<li>tables, strikethrough, and task lists are handled through GFM support</li>
|
|
</ul>
|
|
`);
|
|
|
|
console.log(markdown);
|
|
// # Hello
|
|
//
|
|
// This came from HTML.
|
|
```
|
|
|
|
## Frontmatter
|
|
|
|
Frontmatter is detected with `remark-frontmatter` and parsed through `@push.rocks/smartyaml`.
|
|
|
|
```typescript
|
|
import { SmartMarkdown } from '@push.rocks/smartmarkdown';
|
|
|
|
const smartMarkdown = new SmartMarkdown();
|
|
|
|
const result = await smartMarkdown.getMdParsedResultFromMarkdown(`---
|
|
layout: guide
|
|
draft: false
|
|
order: 10
|
|
---
|
|
|
|
# Guide
|
|
`);
|
|
|
|
console.log(result.frontmatterData.layout); // 'guide'
|
|
console.log(result.frontmatterData.draft); // false
|
|
console.log(result.frontmatterData.order); // 10
|
|
```
|
|
|
|
If no frontmatter block is present, `frontmatterData` is an empty object.
|
|
|
|
## Markdown Features
|
|
|
|
Markdown parsing uses:
|
|
|
|
- `remark-parse` for Markdown parsing.
|
|
- `remark-gfm` for GitHub-flavored Markdown.
|
|
- `remark-frontmatter` for YAML/TOML frontmatter detection.
|
|
- `remark-html` for HTML output.
|
|
|
|
HTML-to-Markdown conversion uses:
|
|
|
|
- `turndown` with `headingStyle: 'atx'`.
|
|
- `turndown` with `codeBlockStyle: 'fenced'`.
|
|
- `turndown-plugin-gfm` for GitHub-flavored Markdown output.
|
|
|
|
## TypeScript and ESM
|
|
|
|
This package ships as an ES module and includes TypeScript declarations.
|
|
|
|
```typescript
|
|
import { SmartMarkdown } from '@push.rocks/smartmarkdown';
|
|
```
|
|
|
|
The package export points to the built ESM entrypoint and is ready for TypeScript, Node.js ESM, and bundler-based frontend projects.
|
|
|
|
## License and Legal Information
|
|
|
|
This repository contains open-source code licensed under the MIT License. A copy of the license can be found in the [LICENSE](./LICENSE) file.
|
|
|
|
**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.
|
|
|
|
### Trademarks
|
|
|
|
This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH or third parties, and are not included within the scope of the MIT license granted herein.
|
|
|
|
Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines or the guidelines of the respective third-party owners, and any usage must be approved in writing. Third-party trademarks used herein are the property of their respective owners and used only in a descriptive manner, e.g. for an implementation of an API or similar.
|
|
|
|
### Company Information
|
|
|
|
Task Venture Capital GmbH
|
|
Registered at District Court Bremen HRB 35230 HB, Germany
|
|
|
|
For any legal inquiries or further information, please contact us via email at hello@task.vc.
|
|
|
|
By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.
|