feat(package): modernize package metadata, typings, and test setup for ESM builds
This commit is contained in:
@@ -1,129 +1,189 @@
|
||||
# @push.rocks/smartmarkdown
|
||||
do more with markdown files
|
||||
|
||||
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
|
||||
To install `@push.rocks/smartmarkdown`, you can use npm or yarn. Run one of the following commands in your terminal:
|
||||
|
||||
```bash
|
||||
npm install @push.rocks/smartmarkdown --save
|
||||
pnpm add @push.rocks/smartmarkdown
|
||||
```
|
||||
|
||||
or if you use yarn:
|
||||
## What It Does
|
||||
|
||||
```bash
|
||||
yarn add @push.rocks/smartmarkdown
|
||||
```
|
||||
`@push.rocks/smartmarkdown` is intentionally focused:
|
||||
|
||||
This module is designed to be used in a Node.js environment or in a frontend project that supports ES Modules.
|
||||
- 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`.
|
||||
|
||||
## Usage
|
||||
`@push.rocks/smartmarkdown` offers powerful tools to work with markdown files, including parsing markdown to HTML, extracting frontmatter data, and converting HTML back to markdown. Below, we'll explore how to utilize these features effectively.
|
||||
|
||||
### **Parsing Markdown to HTML**
|
||||
|
||||
Let's start by converting a simple Markdown string to HTML:
|
||||
## Quick Start
|
||||
|
||||
```typescript
|
||||
import { SmartMarkdown } from '@push.rocks/smartmarkdown';
|
||||
|
||||
async function parseMarkdown() {
|
||||
const mdString = `# Hello World
|
||||
|
||||
This is a simple markdown string.`;
|
||||
const htmlString = await SmartMarkdown.easyMarkdownToHtml(mdString);
|
||||
console.log(htmlString); // Logs the HTML string
|
||||
}
|
||||
const html = await SmartMarkdown.easyMarkdownToHtml(`# Hello Markdown
|
||||
|
||||
parseMarkdown();
|
||||
- fast
|
||||
- typed
|
||||
- practical
|
||||
`);
|
||||
|
||||
console.log(html);
|
||||
// <h1>Hello Markdown</h1>
|
||||
// <ul>
|
||||
// <li>fast</li>
|
||||
// <li>typed</li>
|
||||
// <li>practical</li>
|
||||
// </ul>
|
||||
```
|
||||
|
||||
In this example, `SmartMarkdown.easyMarkdownToHtml` is a convenient static method that takes a markdown string and returns its HTML representation.
|
||||
## API
|
||||
|
||||
### **Working with Frontmatter**
|
||||
### `SmartMarkdown.easyMarkdownToHtml(markdown)`
|
||||
|
||||
Markdown files often contain frontmatter, which is metadata specified at the top of the file. `@push.rocks/smartmarkdown` can extract this data along with converting the content.
|
||||
Static convenience method for the most common path: Markdown in, HTML out.
|
||||
|
||||
```typescript
|
||||
import { SmartMarkdown } from '@push.rocks/smartmarkdown';
|
||||
|
||||
async function parseMarkdownWithFrontmatter() {
|
||||
const markdownWithFrontmatter = `---
|
||||
title: "My First Post"
|
||||
date: 2023-01-01
|
||||
tags: ["blog", "post"]
|
||||
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
|
||||
---
|
||||
|
||||
# Hello World
|
||||
|
||||
This is a post with frontmatter.`;
|
||||
|
||||
const smartmarkdownInstance = new SmartMarkdown();
|
||||
const mdParsedResult = await smartmarkdownInstance.getMdParsedResultFromMarkdown(markdownWithFrontmatter);
|
||||
console.log(mdParsedResult.frontmatterData); // Logs: { title: "My First Post", date: "2023-01-01", tags: ["blog", "post"] }
|
||||
console.log(mdParsedResult.html); // Logs: HTML content
|
||||
}
|
||||
|
||||
parseMarkdownWithFrontmatter();
|
||||
# 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'] }
|
||||
```
|
||||
|
||||
In the code above, we manually create an instance of `SmartMarkdown` to access the `getMdParsedResultFromMarkdown` method, which returns an object containing both the HTML and the frontmatter data.
|
||||
The returned object exposes:
|
||||
|
||||
### **HTML to Markdown Conversion**
|
||||
- `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.
|
||||
|
||||
Sometimes, you may need to convert HTML back to Markdown. Here's how you can do it:
|
||||
### `new SmartMarkdown().htmlToMarkdown(html)`
|
||||
|
||||
Converts HTML back to Markdown using Turndown with GitHub-flavored Markdown support.
|
||||
|
||||
```typescript
|
||||
import { SmartMarkdown } from '@push.rocks/smartmarkdown';
|
||||
|
||||
const smartmarkdownInstance = new SmartMarkdown();
|
||||
const htmlString = '<h1>Hello World</h1><p>This is a simple HTML string.</p>';
|
||||
const markdownString = smartmarkdownInstance.htmlToMarkdown(htmlString);
|
||||
console.log(markdownString);
|
||||
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.
|
||||
```
|
||||
|
||||
In this example, we use the `htmlToMarkdown` method to convert an HTML string back to Markdown. This is particularly useful when working with content editing interfaces that support both formats.
|
||||
## Frontmatter
|
||||
|
||||
### **Advanced Usage: Parsing Markdown with Custom Plugins**
|
||||
|
||||
`@push.rocks/smartmarkdown` leverages Unified, Remark, and Turndown to provide its functionality. You can extend its capabilities by using custom plugins.
|
||||
|
||||
For instance, if you wanted to use a custom Remark plugin to highlight code blocks, you would:
|
||||
|
||||
1. Create a new SmartMarkdown instance.
|
||||
2. Configure it to use your custom plugins.
|
||||
3. Process your markdown content.
|
||||
Frontmatter is detected with `remark-frontmatter` and parsed through `@push.rocks/smartyaml`.
|
||||
|
||||
```typescript
|
||||
// This is a hypothetical example. Please refer to the respective plugin documentation for actual implementation details.
|
||||
|
||||
import { SmartMarkdown } from '@push.rocks/smartmarkdown';
|
||||
import myRemarkPlugin from 'remark-myplugin';
|
||||
|
||||
async function parseMarkdownWithPlugin(mdString: string) {
|
||||
const smartMarkdownInstance = new SmartMarkdown();
|
||||
smartMarkdownInstance.useRemarkPlugin(myRemarkPlugin, pluginOptions);
|
||||
const result = await smartMarkdownInstance.getMdParsedResultFromMarkdown(mdString);
|
||||
// Now `result` will include transformations done by your custom plugin.
|
||||
}
|
||||
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
|
||||
```
|
||||
|
||||
`@push.rocks/smartmarkdown` provides a solid base for performing various markdown related tasks in your projects. Whether parsing, transforming, or exporting content, its utilities and integrations offer flexibility for most needs. By leveraging ES Modules and TypeScript, it ensures type safety and enhances developer experience with excellent IntelliSense support.
|
||||
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 that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository.
|
||||
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 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, and any usage must be approved in writing by Task Venture Capital GmbH.
|
||||
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
|
||||
Registered at District Court Bremen HRB 35230 HB, Germany
|
||||
|
||||
For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.
|
||||
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.
|
||||
|
||||
Reference in New Issue
Block a user