Giter Club home page Giter Club logo

markdown-it-plugin-template's Introduction

Markdown-It Plugin Template

ci-badge npm-badge

A template for creating a markdown-it plugin.

See https://executablebooks.github.io/markdown-it-plugin-template/ for a demonstration!

Usage

As a Node module:

import MarkdownIt from "markdown-it"
import examplePlugin from "markdown-it-plugin-template"

const text = MarkdownIt().use(examplePlugin).render("*a*")

In the browser:

<!DOCTYPE html>
<html>
    <head>
        <title>Example Page</title>
        <script src="https://cdn.jsdelivr.net/npm/markdown-it@12/dist/markdown-it.min.js"></script>
        <script src="https://unpkg.com/markdown-it-plugin-template"></script>
    </head>
    <body>
        <div id="demo"></div>
        <script>
            const text = window.markdownit().use(window.markdownitExample).render("*a*");
            document.getElementById("demo").innerHTML = text
        </script>
    </body>
</html>

Development

Features

Getting Started

  1. Create a GitHub repository from the template.
  2. Replace package details in the following files:
    • package.json
    • LICENSE
    • README.md
    • rollup.config.js
  3. Install the node_module dependencies: npm install or npm ci (see Install a project with a clean slate).
  4. Run code formatting; npm run format, and linting: npm run lint:fix.
  5. Run the unit tests; npm test, or with coverage; npm test -- --coverage.

Now you can start to adapt the code in src/index.ts for your plugin, starting with the markdown-it development recommendations.

Modify the test in tests/fixtures.spec.ts, to load your plugin, then the "fixtures" in tests/fixtures, to provide a set of potential Markdown inputs and expected HTML outputs.

On commits/PRs to the master branch, the GH actions will trigger, running the linting, unit tests, and build tests. Additionally setup and uncomment the codecov action in .github/workflows/ci.yml, to provide automated CI coverage.

Finally, you can update the version of your package, e.g.: npm version patch -m "๐Ÿš€ RELEASE: v%s", push to GitHub; git push --follow-tags, build; npm run build, and publish; npm publish.

Finally, you can adapt the HTML document in docs/, to load both markdown-it and the plugin (from unpkg), then render text from an input area. This can be deployed by GitHub Pages.

Design choices

Why is markdown-it only in devDependencies?

From the markdown-it development recommendations:

Plugins should not require the markdown-it package as a dependency in package.json.

Note, for typing, we import this package with import type, to ensure the imports are not present in the compiled JavaScript.

Why Jest?

There are a number of JavaScript unit testing frameworks (see this comparison, but jest was chosen because of it is easy to setup/use, flexible, and well used in large projects.

Why Rollup?

The three main bundlers are; Webpack, Rollup and Parcel, with the functionality gap between all of these bundlers narrowing over the years. Essentially, Rollup provides a middle ground between features and complexity, and is good for bundling libraries (it is what markdown-it itself uses).

See for example:

markdown-it-plugin-template's People

Contributors

adamwolf avatar chrisjsewell avatar rowanc1 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

markdown-it-plugin-template's Issues

Add documentation

Quite possibly using sphinx + myst-parser + RTD.

It would also be quite nice to have a simple way of "showcasing" the markdown source -> rendered HTML from the plugin.

npm run lint:format

Add prettier to our lint test in the ci.

I have done this as npm run lint:format in the past (as well as a npm run lint:format:fix)

See example.

Happy to make the various PRs for this and #5.

Module Resolution Node

I think that we should add this to the tsconfig:
"moduleResolution": "node"

Ran into some places where we had to change this in a downstream package.

Utility to run local demo

Right now I believe the process is commenting on/off the local script and just opening this up in any webserver or file://.

It would be great to add a npm run start or dev to bring up a live reloading server on file changes (including styles where appropriate). This is more helpful for the more complicated repos like docutils.

This also means the deploy might need to go into a github action (e.g. have done that here), that would allow us to rely on the locally built package rather than the deployed npm package (e.g. not ready to release, but still want the demo updated).

Master to Main

As per a discussion in the EBP slack, I have opened a PR #3, that moves this to main. The default branch/branch protection has been updated.

I will let @chrisjsewell merge that in after review of the (minor) changes.

What distribution(s) to target (CJS, ESM, ...)?

Certainly compared to Python (one distribution works for all v3.x), JS/TS is a bit of a mess when it comes to packaging libraries: not only do you need to think about the version/format of javascript (and compatibility with third-party modules), but also node vs browser compatibility.

Essentially CommonJS modules are a legacy format, that many third party packages still use, whereas ES modules are now the ~official standard and "future" of javascript (and are now basically supported in all browsers: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules, see also https://hacks.mozilla.org/2018/03/es-modules-a-cartoon-deep-dive/).
Again comparing to Python, it feels like JS/TS is in a similar situation to when python packages were transitioning from v2 to v3 (and so support was necessary for both).

Using rollup you can actually target multiple build formats:

Currently, I have it set to do a "hybrid" build with: https://github.com/executablebooks/markdown-it-plugin-template/blob/master/rollup.config.js, and in package.json pointing to either with:

"main": "dist/cjs/index.js",
"module": "dist/mjs/index.js",
"types": "dist/index.d.ts",
"files": [
"dist"
],
"exports": {
".": {
"import": "./dist/mjs/index.js",
"require": "./dist/cjs/index.js"
}
},

This is essentially what is recommended here, but then here it mentions a danger in "double transpiling".

Additionally confusing, is what to target in the tsconfig:

"target": "es6", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', 'ES2021', or 'ESNEXT'. */
"module": "es2015", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */

and also how babel fits in with rollup, and if @rollup/plugin-babel is actually needed (if you remove this plugin, it still builds fine at present):

babel({ babelHelpers: "bundled" }), // transpile ES6/7 code

It is of note that markdown-it itself uses the umd format (amd, cjs and iife), in its rollup config: https://github.com/markdown-it/markdown-it/blob/064d602c6890715277978af810a903ab014efc73/support/rollup.config.js#L13

Also a consideration of what is required for unpkg: mjackson/unpkg#34

Here is also a bunch of links about CommonJS vs ES Modules and building hybrid packages:

`npm i` fails

Describe the bug

When you git clone the template, npm i fails due to eslint-config-standard.

Removing eslint-config-standard from package.json appears to be a quick fix, but I am sure that is not the intended solution.

Most likely the solution is a version bump, but I'm not great at node/js in gerneral

Reproduce the bug

acn@jy-virtual-machine ~/acn> ls
deployment  markdown-it-question-answer  note_plugin  notes  start_ssh_tunnel.sh  start_vms.sh  svm0860
acn@jy-virtual-machine ~/acn> git clone [email protected]:executablebooks/markdown-it-plugin-template.git
Cloning into 'markdown-it-plugin-template'...
The authenticity of host 'github.com (140.82.121.4)' can't be established.
ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'github.com' (ED25519) to the list of known hosts.
remote: Enumerating objects: 157, done.
remote: Counting objects: 100% (157/157), done.
remote: Compressing objects: 100% (112/112), done.
remote: Total 157 (delta 77), reused 105 (delta 33), pack-reused 0
Receiving objects: 100% (157/157), 211.87 KiB | 704.00 KiB/s, done.
Resolving deltas: 100% (77/77), done.
acn@jy-virtual-machine ~/acn> cd markdown-it-plugin-template/
acn@jy-virtual-machine ~/a/markdown-it-plugin-template (main)> npm i
Command 'npm' not found, but can be installed with:
sudo apt install npm
acn@jy-virtual-machine ~/a/markdown-it-plugin-template (main) [127]> nvm use lts
Now using Node v18.12.1 (npm 8.19.2) ~/.local/share/nvm/v18.12.1/bin/node
acn@jy-virtual-machine ~/a/markdown-it-plugin-template (main)> npm i
npm ERR! code ERESOLVE
npm ERR! ERESOLVE could not resolve
npm ERR! 
npm ERR! While resolving: [email protected]
npm ERR! Found: [email protected]
npm ERR! node_modules/eslint-plugin-promise
npm ERR!   dev eslint-plugin-promise@"^6.0.0" from the root project
npm ERR! 
npm ERR! Could not resolve dependency:
npm ERR! peer eslint-plugin-promise@"^4.2.1 || ^5.0.0" from [email protected]
npm ERR! node_modules/eslint-config-standard
npm ERR!   dev eslint-config-standard@"^16.0.3" from the root project
npm ERR! 
npm ERR! Conflicting peer dependency: [email protected]
npm ERR! node_modules/eslint-plugin-promise
npm ERR!   peer eslint-plugin-promise@"^4.2.1 || ^5.0.0" from [email protected]
npm ERR!   node_modules/eslint-config-standard
npm ERR!     dev eslint-config-standard@"^16.0.3" from the root project
npm ERR! 
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force, or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution.
npm ERR! 
npm ERR! See /home/acn/.npm/eresolve-report.txt for a full report.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/acn/.npm/_logs/2022-11-08T12_47_32_108Z-debug-0.log

List your environment

Using Fish on Ubuntu

acn@jy-virtual-machine ~/a/markdown-it-plugin-template (main)> uname -a
Linux jy-virtual-machine 5.15.0-52-generic #58-Ubuntu SMP Thu Oct 13 08:03:55 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
acn@jy-virtual-machine ~/a/markdown-it-plugin-template (main) [1]> npm --version
8.19.2
acn@jy-virtual-machine ~/a/markdown-it-plugin-template (main)> node --version
v18.12.1

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.