├── .gitignore ├── test ├── cjs.js ├── test.mjs └── fixtures │ └── sup.txt ├── .github ├── dependabot.yml └── workflows │ └── ci.yml ├── CHANGELOG.md ├── .eslintrc.yml ├── LICENSE ├── package.json ├── README.md ├── index.mjs └── rollup.config.mjs /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | coverage/ 3 | dist/ 4 | -------------------------------------------------------------------------------- /test/cjs.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | /* eslint-env mocha */ 3 | 4 | const assert = require('node:assert') 5 | const fn = require('../') 6 | 7 | describe('CJS', () => { 8 | it('require', () => { 9 | assert.ok(typeof fn === 'function') 10 | }) 11 | }) 12 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: github-actions 4 | directory: / 5 | schedule: 6 | interval: daily 7 | 8 | - package-ecosystem: npm 9 | directory: / 10 | schedule: 11 | interval: daily 12 | allow: 13 | - dependency-type: production 14 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | 2.0.0 / 2023-12-03 2 | ------------------ 3 | 4 | - Rewrite to ESM. 5 | - Remove `dist/` from repo (build on package publish). 6 | 7 | 8 | 1.0.0 / 2015-03-12 9 | ------------------ 10 | 11 | - Markdown-it 4.0.0 support. Use previous version for 2.x-3.x. 12 | 13 | 14 | 0.1.0 / 2015-01-04 15 | ------------------ 16 | 17 | - First release. 18 | -------------------------------------------------------------------------------- /test/test.mjs: -------------------------------------------------------------------------------- 1 | import { fileURLToPath } from 'node:url' 2 | import markdownit from 'markdown-it' 3 | import generate from 'markdown-it-testgen' 4 | 5 | import sup from '../index.mjs' 6 | 7 | describe('markdown-it-sup', function () { 8 | const md = markdownit().use(sup) 9 | 10 | generate(fileURLToPath(new URL('fixtures/sup.txt', import.meta.url)), md) 11 | }) 12 | -------------------------------------------------------------------------------- /.eslintrc.yml: -------------------------------------------------------------------------------- 1 | extends: standard 2 | 3 | overrides: 4 | - 5 | files: [ '*.mjs' ] 6 | rules: 7 | no-restricted-globals: [ 2, require, __dirname ] 8 | - 9 | files: [ 'test/**' ] 10 | env: { mocha: true } 11 | - 12 | files: [ 'lib/**', 'index.mjs' ] 13 | parserOptions: { ecmaVersion: 2015 } 14 | 15 | ignorePatterns: 16 | - demo/ 17 | - dist/ 18 | - benchmark/extra/ 19 | 20 | rules: 21 | camelcase: 0 22 | no-multi-spaces: 0 23 | -------------------------------------------------------------------------------- /test/fixtures/sup.txt: -------------------------------------------------------------------------------- 1 | . 2 | ^test^ 3 | . 4 |

test

5 | . 6 | 7 | . 8 | ^foo\^ 9 | . 10 |

^foo^

11 | . 12 | 13 | . 14 | 2^4 + 3^5 15 | . 16 |

2^4 + 3^5

17 | . 18 | 19 | . 20 | ^foo~bar^baz^bar~foo^ 21 | . 22 |

foo~barbazbar~foo

23 | . 24 | 25 | . 26 | ^\ foo\ ^ 27 | . 28 |

foo

29 | . 30 | 31 | . 32 | ^foo\\\\\\\ bar^ 33 | . 34 |

foo\\\ bar

35 | . 36 | 37 | . 38 | ^foo\\\\\\ bar^ 39 | . 40 |

^foo\\\ bar^

41 | . 42 | 43 | . 44 | **^foo^ bar** 45 | . 46 |

foo bar

47 | . 48 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | push: 5 | pull_request: 6 | schedule: 7 | - cron: '0 0 * * 3' 8 | 9 | jobs: 10 | test: 11 | 12 | runs-on: ubuntu-latest 13 | 14 | strategy: 15 | matrix: 16 | node-version: [ '18' ] 17 | 18 | steps: 19 | - uses: actions/checkout@v4 20 | 21 | - name: Use Node.js ${{ matrix.node-version }} 22 | uses: actions/setup-node@v4 23 | with: 24 | node-version: ${{ matrix.node-version }} 25 | 26 | - run: npm install 27 | 28 | - name: Test 29 | run: npm test 30 | 31 | - name: Upload coverage report to coveralls.io 32 | uses: coverallsapp/github-action@master 33 | with: 34 | github-token: ${{ secrets.GITHUB_TOKEN }} 35 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014-2015 Vitaly Puzrin, Alex Kocharin. 2 | 3 | Permission is hereby granted, free of charge, to any person 4 | obtaining a copy of this software and associated documentation 5 | files (the "Software"), to deal in the Software without 6 | restriction, including without limitation the rights to use, 7 | copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | copies of the Software, and to permit persons to whom the 9 | Software is furnished to do so, subject to the following 10 | conditions: 11 | 12 | The above copyright notice and this permission notice shall be 13 | included in all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 17 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 19 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 20 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "markdown-it-sup", 3 | "version": "2.0.0", 4 | "description": " tag for markdown-it markdown parser.", 5 | "keywords": [ 6 | "markdown-it-plugin", 7 | "markdown-it", 8 | "markdown", 9 | "superscript", 10 | "sup" 11 | ], 12 | "repository": "markdown-it/markdown-it-sup", 13 | "license": "MIT", 14 | "main": "dist/index.cjs.js", 15 | "module": "index.mjs", 16 | "exports": { 17 | ".": { 18 | "require": "./dist/index.cjs.js", 19 | "import": "./index.mjs" 20 | }, 21 | "./*": { 22 | "require": "./*", 23 | "import": "./*" 24 | } 25 | }, 26 | "files": [ 27 | "index.mjs", 28 | "lib/", 29 | "dist/" 30 | ], 31 | "scripts": { 32 | "lint": "eslint .", 33 | "build": "rollup -c", 34 | "test": "npm run lint && npm run build && c8 --exclude dist --exclude test -r text -r html -r lcov mocha", 35 | "prepublishOnly": "npm run lint && npm run build" 36 | }, 37 | "devDependencies": { 38 | "@rollup/plugin-babel": "^6.0.4", 39 | "@rollup/plugin-node-resolve": "^15.2.3", 40 | "@rollup/plugin-terser": "^0.4.4", 41 | "c8": "^8.0.1", 42 | "eslint": "^8.55.0", 43 | "eslint-config-standard": "^17.1.0", 44 | "markdown-it": "^13.0.2", 45 | "markdown-it-testgen": "^0.1.6", 46 | "mocha": "^10.2.0", 47 | "rollup": "^4.6.1" 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # markdown-it-sup 2 | 3 | [![CI](https://github.com/markdown-it/markdown-it-sup/actions/workflows/ci.yml/badge.svg)](https://github.com/markdown-it/markdown-it-sup/actions/workflows/ci.yml) 4 | [![NPM version](https://img.shields.io/npm/v/markdown-it-sup.svg?style=flat)](https://www.npmjs.org/package/markdown-it-sup) 5 | [![Coverage Status](https://img.shields.io/coveralls/markdown-it/markdown-it-sup/master.svg?style=flat)](https://coveralls.io/r/markdown-it/markdown-it-sup?branch=master) 6 | 7 | > Superscript (``) tag plugin for [markdown-it](https://github.com/markdown-it/markdown-it) markdown parser. 8 | 9 | __v1.+ requires `markdown-it` v4.+, see changelog.__ 10 | 11 | `29^th^` => `29th` 12 | 13 | Markup is based on [pandoc](http://johnmacfarlane.net/pandoc/README.html#superscripts-and-subscripts) definition. But nested markup is currently not supported. 14 | 15 | 16 | ## Install 17 | 18 | node.js, browser: 19 | 20 | ```bash 21 | npm install markdown-it-sup --save 22 | bower install markdown-it-sup --save 23 | ``` 24 | 25 | ## Use 26 | 27 | ```js 28 | var md = require('markdown-it')() 29 | .use(require('markdown-it-sup')); 30 | 31 | md.render('29^th^') // => '

29th

' 32 | ``` 33 | 34 | _Differences in browser._ If you load script directly into the page, without 35 | package system, module will add itself globally as `window.markdownitSup`. 36 | 37 | 38 | ## License 39 | 40 | [MIT](https://github.com/markdown-it/markdown-it-sup/blob/master/LICENSE) 41 | -------------------------------------------------------------------------------- /index.mjs: -------------------------------------------------------------------------------- 1 | // Process ^superscript^ 2 | 3 | // same as UNESCAPE_MD_RE plus a space 4 | const UNESCAPE_RE = /\\([ \\!"#$%&'()*+,./:;<=>?@[\]^_`{|}~-])/g 5 | 6 | function superscript (state, silent) { 7 | const max = state.posMax 8 | const start = state.pos 9 | 10 | if (state.src.charCodeAt(start) !== 0x5E/* ^ */) { return false } 11 | if (silent) { return false } // don't run any pairs in validation mode 12 | if (start + 2 >= max) { return false } 13 | 14 | state.pos = start + 1 15 | let found = false 16 | 17 | while (state.pos < max) { 18 | if (state.src.charCodeAt(state.pos) === 0x5E/* ^ */) { 19 | found = true 20 | break 21 | } 22 | 23 | state.md.inline.skipToken(state) 24 | } 25 | 26 | if (!found || start + 1 === state.pos) { 27 | state.pos = start 28 | return false 29 | } 30 | 31 | const content = state.src.slice(start + 1, state.pos) 32 | 33 | // don't allow unescaped spaces/newlines inside 34 | if (content.match(/(^|[^\\])(\\\\)*\s/)) { 35 | state.pos = start 36 | return false 37 | } 38 | 39 | // found! 40 | state.posMax = state.pos 41 | state.pos = start + 1 42 | 43 | // Earlier we checked !silent, but this implementation does not need it 44 | const token_so = state.push('sup_open', 'sup', 1) 45 | token_so.markup = '^' 46 | 47 | const token_t = state.push('text', '', 0) 48 | token_t.content = content.replace(UNESCAPE_RE, '$1') 49 | 50 | const token_sc = state.push('sup_close', 'sup', -1) 51 | token_sc.markup = '^' 52 | 53 | state.pos = state.posMax + 1 54 | state.posMax = max 55 | return true 56 | } 57 | 58 | export default function sup_plugin (md) { 59 | md.inline.ruler.after('emphasis', 'sup', superscript) 60 | }; 61 | -------------------------------------------------------------------------------- /rollup.config.mjs: -------------------------------------------------------------------------------- 1 | import resolve from '@rollup/plugin-node-resolve' 2 | import terser from '@rollup/plugin-terser' 3 | import { babel } from '@rollup/plugin-babel' 4 | import { readFileSync } from 'fs' 5 | 6 | const pkg = JSON.parse(readFileSync(new URL('package.json', import.meta.url))) 7 | 8 | function globalName (name) { 9 | const parts = name.split('-') 10 | for (let i = 2; i < parts.length; i++) { 11 | parts[i] = parts[i][0].toUpperCase() + parts[i].slice(1) 12 | } 13 | return parts.join('') 14 | } 15 | 16 | const config_umd_full = { 17 | input: 'index.mjs', 18 | output: [ 19 | { 20 | file: `dist/${pkg.name}.js`, 21 | format: 'umd', 22 | name: globalName(pkg.name), 23 | plugins: [ 24 | // Here terser is used only to force ascii output 25 | terser({ 26 | mangle: false, 27 | compress: false, 28 | format: { comments: 'all', beautify: true, ascii_only: true, indent_level: 2 } 29 | }) 30 | ] 31 | }, 32 | { 33 | file: `dist/${pkg.name}.min.js`, 34 | format: 'umd', 35 | name: globalName(pkg.name), 36 | plugins: [ 37 | terser({ 38 | format: { ascii_only: true } 39 | }) 40 | ] 41 | } 42 | ], 43 | plugins: [ 44 | resolve(), 45 | babel({ babelHelpers: 'bundled' }), 46 | { 47 | banner () { 48 | return `/*! ${pkg.name} ${pkg.version} https://github.com/${pkg.repository} @license ${pkg.license} */` 49 | } 50 | } 51 | ] 52 | } 53 | 54 | const config_cjs_no_deps = { 55 | input: 'index.mjs', 56 | output: { 57 | file: 'dist/index.cjs.js', 58 | format: 'cjs' 59 | }, 60 | external: Object.keys(pkg.dependencies || {}), 61 | plugins: [ 62 | resolve(), 63 | babel({ babelHelpers: 'bundled' }) 64 | ] 65 | } 66 | 67 | let config = [ 68 | config_umd_full, 69 | config_cjs_no_deps 70 | ] 71 | 72 | if (process.env.CJS_ONLY) config = [config_cjs_no_deps] 73 | 74 | export default config 75 | --------------------------------------------------------------------------------