From f2bcb6c30a1c122b0ddd8cd05f557d1836d9a108 Mon Sep 17 00:00:00 2001 From: Sergey Makhnatkin Date: Mon, 3 Jun 2024 10:31:58 +0200 Subject: [PATCH] Added directive --- package.json | 6 +- support/demo_template/index.mjs | 102 +++++++++++++++++------- support/demo_template/rollup.config.mjs | 2 + 3 files changed, 79 insertions(+), 31 deletions(-) diff --git a/package.json b/package.json index ce97fce..8371e5f 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,7 @@ }, "scripts": { "lint": "eslint .", + "lint:fix": "eslint . --fix", "test": "npm run lint && CJS_ONLY=1 npm run build && c8 --exclude dist --exclude test -r text -r html -r lcov mocha && node support/specsplit.mjs", "doc": "node support/build_doc.mjs", "gh-doc": "npm run doc && gh-pages -d apidoc -f", @@ -38,7 +39,8 @@ "specsplit": "support/specsplit.mjs good -o test/fixtures/commonmark/good.txt && support/specsplit.mjs bad -o test/fixtures/commonmark/bad.txt && support/specsplit.mjs", "todo": "grep 'TODO' -n -r ./lib 2>/dev/null", "prepublishOnly": "npm test && npm run build && npm run gh-demo && npm run gh-doc", - "dev": "node server/server.js" + "server": "node server/server.js", + "dev": "npm run lint:fix && node support/build_demo.mjs && npm run server" }, "files": [ "index.mjs", @@ -51,6 +53,7 @@ "entities": "^4.4.0", "linkify-it": "^5.0.0", "markdown-it": "^14.1.0", + "markdown-it-directive": "^1.0.1", "mdurl": "^2.0.0", "node-fetch": "^3.3.2", "punycode.js": "^2.3.1", @@ -59,6 +62,7 @@ "devDependencies": { "@rollup/plugin-babel": "^6.0.4", "@rollup/plugin-commonjs": "^25.0.7", + "@rollup/plugin-json": "^6.1.0", "@rollup/plugin-node-resolve": "^15.2.3", "@rollup/plugin-terser": "^0.4.4", "ansi": "^0.3.0", diff --git a/support/demo_template/index.mjs b/support/demo_template/index.mjs index 4b634c2..17a6e18 100644 --- a/support/demo_template/index.mjs +++ b/support/demo_template/index.mjs @@ -5,15 +5,16 @@ import * as mdurl from 'mdurl' import hljs from 'highlight.js' // plugins -import md_abbr from 'markdown-it-abbr' -import md_container from 'markdown-it-container' -import md_deflist from 'markdown-it-deflist' -import { full as md_emoji } from 'markdown-it-emoji' -import md_footnote from 'markdown-it-footnote' -import md_ins from 'markdown-it-ins' -import md_mark from 'markdown-it-mark' -import md_sub from 'markdown-it-sub' -import md_sup from 'markdown-it-sup' +// import md_abbr from 'markdown-it-abbr' +// import md_container from 'markdown-it-container' +// import md_deflist from 'markdown-it-deflist' +// import { full as md_emoji } from 'markdown-it-emoji' +// import md_footnote from 'markdown-it-footnote' +// import md_ins from 'markdown-it-ins' +// import md_mark from 'markdown-it-mark' +// import md_sub from 'markdown-it-sub' +// import md_sup from 'markdown-it-sup' +import md_directive from 'markdown-it-directive' let mdHtml, mdSrc, permalink, scrollMap @@ -31,13 +32,13 @@ const defaults = { langPrefix: 'language-', // autoconvert URL-like texts to links - linkify: true, + linkify: false, // Enable smartypants and other sweet transforms - typographer: true, + typographer: false, // options below are for demo only - _highlight: true, + _highlight: false, _strict: false, _view: 'html' // html / src / debug } @@ -91,25 +92,66 @@ function mdInit () { mdSrc = window.markdownit('commonmark') } else { mdHtml = window.markdownit(defaults) - .use(md_abbr) - .use(md_container, 'warning') - .use(md_deflist) - .use(md_emoji) - .use(md_footnote) - .use(md_ins) - .use(md_mark) - .use(md_sub) - .use(md_sup) + // .use(md_abbr) + // .use(md_container, 'warning') + // .use(md_deflist) + // .use(md_emoji) + // .use(md_footnote) + // .use(md_ins) + // .use(md_mark) + // .use(md_sub) + // .use(md_sup) + .use(md_directive) + // .use((md) => { + // md.inlineDirectives['directive-name'] = (state, content, dests, attrs, contentStart, contentEnd, directiveStart, directiveEnd) => { + // const token = state.push('html_inline', '', 0) + // token.content = JSON.stringify({ directive: 'directive-name', content, dests, attrs }) + '\n' + // } + // md.blockDirectives['directive-name'] = ( + // state, content, contentTitle, inlineContent, dests, attrs, + // contentStartLine, contentEndLine, + // contentTitleStart, contentTitleEnd, + // inlineContentStart, inlineContentEnd, + // directiveStartLine, directiveEndLine + // ) => { + // const token = state.push('html_block', '', 0) + // token.map = [directiveStartLine, directiveEndLine] + // token.content = JSON.stringify({ + // directive: 'directive-name (block)', content, contentTitle, inlineContent, dests, attrs + // }) + '\n' + // } + // }) + mdSrc = window.markdownit(defaults) - .use(md_abbr) - .use(md_container, 'warning') - .use(md_deflist) - .use(md_emoji) - .use(md_footnote) - .use(md_ins) - .use(md_mark) - .use(md_sub) - .use(md_sup) + // .use(md_abbr) + // .use(md_container, 'warning') + // .use(md_deflist) + // .use(md_emoji) + // .use(md_footnote) + // .use(md_ins) + // .use(md_mark) + // .use(md_sub) + // .use(md_sup) + .use(md_directive) + // .use((md) => { + // md.inlineDirectives['directive-name'] = (state, content, dests, attrs, contentStart, contentEnd, directiveStart, directiveEnd) => { + // const token = state.push('html_inline', '', 0) + // token.content = JSON.stringify({ directive: 'directive-name', content, dests, attrs }) + '\n' + // } + // md.blockDirectives['directive-name'] = ( + // state, content, contentTitle, inlineContent, dests, attrs, + // contentStartLine, contentEndLine, + // contentTitleStart, contentTitleEnd, + // inlineContentStart, inlineContentEnd, + // directiveStartLine, directiveEndLine + // ) => { + // const token = state.push('html_block', '', 0) + // token.map = [directiveStartLine, directiveEndLine] + // token.content = JSON.stringify({ + // directive: 'directive-name (block)', content, contentTitle, inlineContent, dests, attrs + // }) + '\n' + // } + // }) } // Beautify output of parser for html content diff --git a/support/demo_template/rollup.config.mjs b/support/demo_template/rollup.config.mjs index 2f3f3a6..ee459b7 100644 --- a/support/demo_template/rollup.config.mjs +++ b/support/demo_template/rollup.config.mjs @@ -1,10 +1,12 @@ import nodeResolve from '@rollup/plugin-node-resolve' import commonjs from '@rollup/plugin-commonjs' import terser from '@rollup/plugin-terser' +import json from '@rollup/plugin-json' const plugins = [ nodeResolve({ preferBuiltins: true }), commonjs(), + json(), // Here terser is used only to force ascii output terser({ mangle: false,