Files
smartmarkdown/readme.md
T

190 lines
6.1 KiB
Markdown
Raw Permalink Normal View History

2024-01-19 21:09:27 +01:00
# @push.rocks/smartmarkdown
2019-05-05 17:43:44 +02:00
Markdown utilities for modern TypeScript projects: parse Markdown into HTML, read YAML frontmatter, and convert HTML back into clean GitHub-flavored Markdown.
2024-04-14 17:57:02 +02:00
`@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.
2024-04-14 17:57:02 +02:00
## Install
2024-04-14 17:57:02 +02:00
```bash
pnpm add @push.rocks/smartmarkdown
2024-04-14 17:57:02 +02:00
```
## What It Does
2019-05-05 17:43:44 +02:00
`@push.rocks/smartmarkdown` is intentionally focused:
2024-04-14 17:57:02 +02:00
- 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`.
2024-04-14 17:57:02 +02:00
## Quick Start
2024-04-14 17:57:02 +02:00
```typescript
import { SmartMarkdown } from '@push.rocks/smartmarkdown';
const html = await SmartMarkdown.easyMarkdownToHtml(`# Hello Markdown
- fast
- typed
- practical
`);
2024-04-14 17:57:02 +02:00
console.log(html);
// <h1>Hello Markdown</h1>
// <ul>
// <li>fast</li>
// <li>typed</li>
// <li>practical</li>
// </ul>
2024-04-14 17:57:02 +02:00
```
## API
2024-04-14 17:57:02 +02:00
### `SmartMarkdown.easyMarkdownToHtml(markdown)`
2024-04-14 17:57:02 +02:00
Static convenience method for the most common path: Markdown in, HTML out.
2024-04-14 17:57:02 +02:00
```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
2024-04-14 17:57:02 +02:00
---
# 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'] }
2024-04-14 17:57:02 +02:00
```
The returned object exposes:
2024-04-14 17:57:02 +02:00
- `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.
2024-04-14 17:57:02 +02:00
### `new SmartMarkdown().htmlToMarkdown(html)`
Converts HTML back to Markdown using Turndown with GitHub-flavored Markdown support.
2024-04-14 17:57:02 +02:00
```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.
2024-04-14 17:57:02 +02:00
```
## Frontmatter
Frontmatter is detected with `remark-frontmatter` and parsed through `@push.rocks/smartyaml`.
```typescript
import { SmartMarkdown } from '@push.rocks/smartmarkdown';
2024-04-14 17:57:02 +02:00
const smartMarkdown = new SmartMarkdown();
2024-04-14 17:57:02 +02:00
const result = await smartMarkdown.getMdParsedResultFromMarkdown(`---
layout: guide
draft: false
order: 10
---
2024-04-14 17:57:02 +02:00
# Guide
`);
2024-04-14 17:57:02 +02:00
console.log(result.frontmatterData.layout); // 'guide'
console.log(result.frontmatterData.draft); // false
console.log(result.frontmatterData.order); // 10
```
2024-04-14 17:57:02 +02:00
If no frontmatter block is present, `frontmatterData` is an empty object.
2024-04-14 17:57:02 +02:00
## 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
2024-04-14 17:57:02 +02:00
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.
2024-04-14 17:57:02 +02:00
## 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.
2024-04-14 17:57:02 +02:00
**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.
2019-05-05 17:43:44 +02:00
2024-04-14 17:57:02 +02:00
### Trademarks
2020-01-18 16:55:29 +00:00
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.
2020-01-18 16:55:29 +00:00
2024-04-14 17:57:02 +02:00
### Company Information
2020-01-18 16:55:29 +00:00
2024-04-14 17:57:02 +02:00
Task Venture Capital GmbH
Registered at District Court Bremen HRB 35230 HB, Germany
2019-05-05 17:43:44 +02:00
For any legal inquiries or further information, please contact us via email at hello@task.vc.
2019-05-05 17:43:44 +02:00
2024-04-14 17:57:02 +02:00
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.