Git Product home page Git Product logo

md-lite-js's Introduction

Croct
MD Lite
A minimalist Markdown parser and render for basic formatting.

Version Build Gzipped bundle size

πŸ“¦ Releases Β· 🐞 Report Bug Β· ✨ Request Feature

Introduction

This library provides a fast and simple Markdown parser with zero dependencies. Perfect for those who need to handle basic Markdown syntax without the overhead of a full-featured Markdown parser.

Features

  • πŸͺΆ Lightweight: Zero dependencies, about 1.5 KB gzipped.
  • 🌐 Cross-environment: Works in Node.js and browsers.
  • ✍️ Minimalist: Supports only italic, bold, strikethrough, inline code, links, πŸ–ΌοΈ images, and ΒΆ paragraphs.
  • πŸ›  Flexible: Render whatever you want, from HTML to JSX.

Who is this library for?

If you're working on a project that requires rendering Markdown for short texts like titles, subtitles, and descriptions, but you don't need a full-featured Markdown parser, this library is for you.

Installation

We recommend using NPM to install the package:

npm install @croct/md-lite

Alternatively, you can use Yarn:

yarn add @croct/md-lite

Basic usage

The following sections show how to parse and render Markdown using this library.

Parsing Markdown

To parse a Markdown string into an AST, use the parse function:

import {parse} from '@croct/md-lite';

const markdown = '**Hello**, [World](https://example.com)';

const ast = parse(markdown);

The parse function returns a tree-like structure called an Abstract Syntax Tree (AST). You can find the full AST definition here.

Rendering Markdown

To render an AST into whatever you want, use the render function. It accepts as input either a Markdown string or an AST:

import {render} from '@croct/md-lite';

// Passing a string as input is equivalent to calling `parse` first
const markdown = '**Hello**, [World](https://example.com)';

const html = render(markdown, {
    text: node => node.content,
    bold: node => `<b>${node.children}</b>`,
    italic: node => `<i>${node.children}</i>`,
    strike: node => `<s>${node.children}</s>`,
    code: node => `<code>${node.content}</code>`,
    link: node => `<a href="${node.href}">${node.children}</a>`,
    image: node => `<img src="${node.src}" alt="${node.alt}">`,
    paragraph: node => `<p>${node.children.join('')}</p>`,
    fragment: node => node.children.join(''),
});

Here is an example of how to render the Markdown string above into JSX:

import {render} from '@croct/md-lite';

// Passing a string as input is equivalent to calling `parse` first
const markdown = '**Hello**, [World](https://example.com)';

const jsx = render(markdown, {
    text: node => node.content,
    bold: node => <b>{node.children}</b>,
    italic: node => <i>{node.children}</i>,
    strike: node => <s>{node.children}</s>,
    code: node => <code>{node.content}</code>,
    link: node => <a href={node.href}>{node.children}</a>,
    image: node => <img src={node.src} alt={node.alt} />,
    paragraph: node => <p>{node.children}</p>,
    fragment: node => node.children.map(
        (child, index) => <Fragment key={index}>{child}</Fragment>
    ),
});

Handling unsupported features

In some cases, you might want to intentionally omit certain features from your rendered Markdown. For instance, if your platform doesn't support image rendering, ou can simply return the original source text instead of trying to display the image.

import {render, unescape} from '@croct/md-lite';

render(markdown, {
    // ... other render functions
    image: node => unescape(node.source),
});

This code snippet will simply return the raw source code of the image node instead of trying to render it as an image. You can adapt this approach to handle any other unsupported feature by defining appropriate render functions and accessing the relevant data from the AST.

Contributing

Contributions to the package are always welcome!

  • Report any bugs or issues on the issue tracker.
  • For major changes, please open an issue first to discuss what you would like to change.
  • Please make sure to update tests as appropriate.

Testing

Before running the test suites, the development dependencies must be installed:

npm install

Then, to run all tests:

npm run test

Run the following command to check the code against the style guide:

npm run lint

Building

Before building the project, the dependencies must be installed:

npm install

Then, to build the project:

npm run build

License

Copyright Β© 2015-2023 Croct Limited, All Rights Reserved.

All information contained herein is, and remains the property of Croct Limited. The intellectual, design and technical concepts contained herein are proprietary to Croct Limited s and may be covered by U.S. and Foreign Patents, patents in process, and are protected by trade secret or copyright law. Dissemination of this information or reproduction of this material is strictly forbidden unless prior written permission is obtained from Croct Limited.

md-lite-js's People

Contributors

fryuni avatar georgekaran avatar marcospassos avatar renovate[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

md-lite-js's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Repository problems

These problems occurred while renovating this repository. View logs.

  • WARN: File contents are invalid JSON but parse using JSON5. Support for this will be removed in a future release so please change to a support .json5 file name or ensure correct JSON syntax.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/check-commit-style.yml
  • mristin/opinionated-commit-message v3.1.0
.github/workflows/check-required-labels.yaml
.github/workflows/deploy-release.yaml
  • actions/checkout v4
  • actions/setup-node v4
  • actions/cache v4
.github/workflows/send-guidelines.yaml
  • wow-actions/auto-comment v1
.github/workflows/update-release-notes.yaml
  • release-drafter/release-drafter v6
.github/workflows/validate-branch.yaml
  • actions/checkout v4
  • actions/setup-node v4
  • actions/cache v4
  • actions/checkout v4
  • actions/setup-node v4
  • actions/cache v4
  • actions/checkout v4
  • actions/setup-node v4
  • actions/cache v4
  • actions/checkout v4
  • actions/setup-node v4
  • actions/cache v4
  • paambaati/codeclimate-action v5
npm
package.json
  • @croct/eslint-plugin ^0.7.0
  • @swc/jest ^0.2.24
  • @types/jest ^29.0.0
  • @typescript-eslint/parser ^6.0.0
  • eslint ^8.22
  • jest ^29.0.0
  • typescript ^5.0.0

  • Check this box to trigger a request for Renovate to run again on this repository

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Error type: Cannot find preset's package (github>croct-tech/renovate-presets:js)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.