From 43afab2173679a3dee7d55a68c7fe916f1363406 Mon Sep 17 00:00:00 2001 From: wavesheep <1196046520@qq.com> Date: Tue, 30 Jun 2020 09:54:18 +0800 Subject: [PATCH] first commit --- .github/workflows/publish_package.yml | 18 +++ .github/workflows/test.yml | 19 ++++ .gitignore | 3 + .npmignore | 5 + CHANGELOG.md | 4 + LICENSE | 22 ++++ README.md | 59 ++++++++++ dist/markdown-it-plain-text.js | 41 +++++++ dist/markdown-it-plain-text.min.js | 1 + index.js | 36 ++++++ jest.config.js | 6 + package.json | 34 ++++++ rollup.config.js | 24 ++++ test/data/default-result | 1 + test/data/default.md | 157 ++++++++++++++++++++++++++ test/index.test.js | 49 ++++++++ 16 files changed, 479 insertions(+) create mode 100644 .github/workflows/publish_package.yml create mode 100644 .github/workflows/test.yml create mode 100644 .gitignore create mode 100644 .npmignore create mode 100644 CHANGELOG.md create mode 100644 LICENSE create mode 100644 README.md create mode 100644 dist/markdown-it-plain-text.js create mode 100644 dist/markdown-it-plain-text.min.js create mode 100644 index.js create mode 100644 jest.config.js create mode 100644 package.json create mode 100644 rollup.config.js create mode 100644 test/data/default-result create mode 100644 test/data/default.md create mode 100644 test/index.test.js diff --git a/.github/workflows/publish_package.yml b/.github/workflows/publish_package.yml new file mode 100644 index 0000000..bf24d8a --- /dev/null +++ b/.github/workflows/publish_package.yml @@ -0,0 +1,18 @@ +name: publish package + +on: + release: + types: [created] + +jobs: + publish-npm: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-node@v1 + with: + node-version: 12 + registry-url: https://registry.npmjs.org/ + - run: npm publish + env: + NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}} \ No newline at end of file diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..a6d711a --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,19 @@ +name: Node.js CI + +on: [push] + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Test with Node.js 12 + uses: actions/setup-node@v1 + with: + node-version: 12 + - run: npm install + - run: npm test + - name: Upload coverage to Codecov + uses: codecov/codecov-action@v1 + with: + token: ${{ secrets.CODECOV_TOKEN }} \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9e699e8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +node_modules/ +coverage/ +.DS_Store \ No newline at end of file diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..3d9f2ab --- /dev/null +++ b/.npmignore @@ -0,0 +1,5 @@ +dist/ +test/ +.github +coverage/ +*.config.js \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..299d3f7 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,4 @@ +0.1.1 +------------------ + +- First release. \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..eaae64c --- /dev/null +++ b/LICENSE @@ -0,0 +1,22 @@ +Copyright (c) 2020 wavesheep. + +Permission is hereby granted, free of charge, to any person +obtaining a copy of this software and associated documentation +files (the "Software"), to deal in the Software without +restriction, including without limitation the rights to use, +copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..7decefe --- /dev/null +++ b/README.md @@ -0,0 +1,59 @@ +# markdown-it-plain-text + +![GitHub](https://img.shields.io/github/license/wavesheep/markdown-it-plain-text) +![GitHub Workflow Status](https://img.shields.io/github/workflow/status/wavesheep/markdown-it-plain-text/Node.js%20CI) +[![codecov](https://codecov.io/gh/wavesheep/markdown-it-plain-text/branch/master/graph/badge.svg)](https://codecov.io/gh/wavesheep/markdown-it-plain-text) +![GitHub file size in bytes](https://img.shields.io/github/size/wavesheep/markdown-it-plain-text/dist/markdown-it-plain-text.min.js) +[![npm](https://img.shields.io/npm/v/markdown-it-plain-text)](https://www.npmjs.com/package/markdown-it-plain-text) + +> plain text extractor for markdown-it + +## Install + +### nodejs + +```shell +npm install markdown-it-plain-text +``` + +### browser (CDN) + +### uncompressed +[https://cdn.jsdelivr.net/gh/wavesheep/markdown-it-plain-text@0.1.0/dist/markdown-it-plain-text.js](https://cdn.jsdelivr.net/gh/wavesheep/markdown-it-plain-text@0.1.0/dist/markdown-it-plain-text.js) +### compressed +[https://cdn.jsdelivr.net/gh/wavesheep/markdown-it-plain-text@0.1.0/dist/markdown-it-plain-text.min.js](https://cdn.jsdelivr.net/gh/wavesheep/markdown-it-plain-text@0.1.0/dist/markdown-it-plain-text.js) + +## Usage examples + +### Node.js + +```js +const MarkdownIt = require('markdown-it'); +const plainText = require('markdown-it-plain-text'); + +const md = new MarkdownIt(); +md.use(plainText); + +md.render('your markdown content') + +// Here are plain text result +md.plainText +``` + +### Browser + + If you load the script directly into the page without using a package system, the module will add itself globally with the name `markdownItPlainTextPlugin`. Init code will look a bit different in this case: + +```js +const md = window.markdownit().use(window.markdownItPlainTextPlugin); +``` + +## Generation rule + +1. extract `text`, `fence`, `html_block`, `code_block`, `html_inline`, `emoji` content directly +2. add `space` at closing tag to *prevent words from sticking together* +3. *remove redundant white spaces* from result + +## LICENSE + +[MIT](LICENSE) diff --git a/dist/markdown-it-plain-text.js b/dist/markdown-it-plain-text.js new file mode 100644 index 0000000..b6993e2 --- /dev/null +++ b/dist/markdown-it-plain-text.js @@ -0,0 +1,41 @@ +var markdownItPlainTextPlugin = (function () { + 'use strict'; + + var markdownItPlainText = function plainTextPlugin(md) { + function plainTextRule(state) { + var text = scan(state.tokens); + // remove redundant white spaces + md.plainText = text.replace(/\s+/g, " "); + } + + function scan(tokens) { + var text = ''; + for (var i = 0; i < tokens.length; i++) { + var token = tokens[i]; + if (token.children !== null) { + text += scan(token.children); + } else { + if ( + token.type === 'text' || + token.type === 'fence' || + token.type === 'html_block' || + token.type === 'code_block' || + token.type === 'html_inline' || + token.type === 'emoji' + ) { + text += token.content; + } else if (/[a-zA-Z]+_close/.test(token.type)) { // prevent words from sticking together + text += " "; + } + } + } + + return text; + } + + md.core.ruler.push('plainText', plainTextRule); + }; + + return markdownItPlainText; + +}()); diff --git a/dist/markdown-it-plain-text.min.js b/dist/markdown-it-plain-text.min.js new file mode 100644 index 0000000..6d67970 --- /dev/null +++ b/dist/markdown-it-plain-text.min.js @@ -0,0 +1 @@ +var markdownItPlainTextPlugin=function(){"use strict";return function(e){e.core.ruler.push("plainText",(function(t){var n=function e(t){for(var n="",l=0;l", + "license": "MIT", + "devDependencies": { + "@rollup/plugin-commonjs": "^13.0.0", + "@rollup/plugin-json": "^4.1.0", + "cross-env": "^7.0.2", + "jest": "^26.1.0", + "markdown-it": "^11.0.0", + "markdown-it-emoji": "^1.4.0", + "rollup": "^2.18.1", + "rollup-plugin-terser": "^6.1.0" + } +} diff --git a/rollup.config.js b/rollup.config.js new file mode 100644 index 0000000..8e02dcd --- /dev/null +++ b/rollup.config.js @@ -0,0 +1,24 @@ +import path from 'path' +import { name } from './package.json'; +import json from '@rollup/plugin-json'; +import commonjs from '@rollup/plugin-commonjs'; +import { terser } from 'rollup-plugin-terser'; + +const isDev = process.env.NODE_ENV !== 'production'; + +export default { + input: 'index.js', + output: { + file: !isDev ? path.resolve(`dist/${name}.min.js`) : path.resolve(`dist/${name}.js`), + format: 'iife', + name: 'markdownItPlainTextPlugin' + }, + plugins: [ + json(), + commonjs(), + !isDev && terser() + ] +} + + + diff --git a/test/data/default-result b/test/data/default-result new file mode 100644 index 0000000..81e6604 --- /dev/null +++ b/test/data/default-result @@ -0,0 +1 @@ +Advertisement :) pica - high quality and fast imageresize in browser. babelfish - developer friendlyi18n with plurals support and easy syntax. You will like those projects! h1 Heading 8-) h2 Heading h3 Heading h4 Heading h5 Heading h6 Heading Horizontal Rules Typographic replacements Enable typographer option to see result. (c) (C) (r) (R) (tm) (TM) (p) (P) +- test.. test... test..... test?..... test!.... !!!!!! ???? ,, -- --- "Smartypants, double quotes" and 'single quotes' Emphasis This is bold text This is bold text This is italic text This is italic text Strikethrough Blockquotes Blockquotes can also be nested... ...by using additional greater-than signs right next to each other... ...or with spaces between arrows. Lists Unordered Create a list by starting a line with , , or Sub-lists are made by indenting 2 spaces: Marker character change forces new list start: Ac tristique libero volutpat at Facilisis in pretium nisl aliquet Nulla volutpat aliquam velit Very easy! Ordered Lorem ipsum dolor sit amet Consectetur adipiscing elit Integer molestie lorem at massa You can use sequential numbers... ...or keep all the numbers as Start numbering with offset: foo bar Code Inline Indented code // Some comments line 1 of code line 2 of code line 3 of code Block code "fences" Sample text here... Syntax highlighting var foo = function (bar) { return bar++; }; console.log(foo(5)); Tables Option Description data path to data files to supply the data that will be passed into templates. engine engine to be used for processing templates. Handlebars is the default. ext extension to be used for dest files. Right aligned columns Option Description data path to data files to supply the data that will be passed into templates. engine engine to be used for processing templates. Handlebars is the default. ext extension to be used for dest files. Links link text link with title Autoconverted link https://github.com/nodeca/pica (enable linkify to see) Images MinionStormtroopocat Like links, Images also have a footnote style syntax Alt text With a reference later in the document defining the URL location: \ No newline at end of file diff --git a/test/data/default.md b/test/data/default.md new file mode 100644 index 0000000..17f094d --- /dev/null +++ b/test/data/default.md @@ -0,0 +1,157 @@ +--- +__Advertisement :)__ + +- __[pica](https://nodeca.github.io/pica/demo/)__ - high quality and fast image + resize in browser. +- __[babelfish](https://github.com/nodeca/babelfish/)__ - developer friendly + i18n with plurals support and easy syntax. + +You will like those projects! + +--- + +# h1 Heading 8-) +## h2 Heading +### h3 Heading +#### h4 Heading +##### h5 Heading +###### h6 Heading + + +## Horizontal Rules + +___ + +--- + +*** + + +## Typographic replacements + +Enable typographer option to see result. + +(c) (C) (r) (R) (tm) (TM) (p) (P) +- + +test.. test... test..... test?..... test!.... + +!!!!!! ???? ,, -- --- + +"Smartypants, double quotes" and 'single quotes' + + +## Emphasis + +**This is bold text** + +__This is bold text__ + +*This is italic text* + +_This is italic text_ + +~~Strikethrough~~ + + +## Blockquotes + + +> Blockquotes can also be nested... +>> ...by using additional greater-than signs right next to each other... +> > > ...or with spaces between arrows. + + +## Lists + +Unordered + ++ Create a list by starting a line with `+`, `-`, or `*` ++ Sub-lists are made by indenting 2 spaces: + - Marker character change forces new list start: + * Ac tristique libero volutpat at + + Facilisis in pretium nisl aliquet + - Nulla volutpat aliquam velit ++ Very easy! + +Ordered + +1. Lorem ipsum dolor sit amet +2. Consectetur adipiscing elit +3. Integer molestie lorem at massa + + +1. You can use sequential numbers... +1. ...or keep all the numbers as `1.` + +Start numbering with offset: + +57. foo +1. bar + + +## Code + +Inline `code` + +Indented code + + // Some comments + line 1 of code + line 2 of code + line 3 of code + + +Block code "fences" + +``` +Sample text here... +``` + +Syntax highlighting + +``` js +var foo = function (bar) { + return bar++; +}; + +console.log(foo(5)); +``` + +## Tables + +| Option | Description | +| ------ | ----------- | +| data | path to data files to supply the data that will be passed into templates. | +| engine | engine to be used for processing templates. Handlebars is the default. | +| ext | extension to be used for dest files. | + +Right aligned columns + +| Option | Description | +| ------:| -----------:| +| data | path to data files to supply the data that will be passed into templates. | +| engine | engine to be used for processing templates. Handlebars is the default. | +| ext | extension to be used for dest files. | + + +## Links + +[link text](http://dev.nodeca.com) + +[link with title](http://nodeca.github.io/pica/demo/ "title text!") + +Autoconverted link https://github.com/nodeca/pica (enable linkify to see) + + +## Images + +![Minion](https://octodex.github.com/images/minion.png) +![Stormtroopocat](https://octodex.github.com/images/stormtroopocat.jpg "The Stormtroopocat") + +Like links, Images also have a footnote style syntax + +![Alt text][id] + +With a reference later in the document defining the URL location: + +[id]: https://octodex.github.com/images/dojocat.jpg "The Dojocat" diff --git a/test/index.test.js b/test/index.test.js new file mode 100644 index 0000000..ecff79b --- /dev/null +++ b/test/index.test.js @@ -0,0 +1,49 @@ +const fs = require('fs'); +const path = require('path'); + +const MarkdownIt = require('markdown-it'); +const emoji = require('markdown-it-emoji'); +const plainText = require('../index.js'); + +describe('without plugin', () => { + test('default mode', () => { + const md = new MarkdownIt(); + md.use(plainText); + + const testData = fs.readFileSync(path.resolve(`${__dirname}/data/default.md`), 'utf8'); + const result = fs.readFileSync(path.resolve(`${__dirname}/data/default-result`), 'utf8'); + md.render(testData); + + expect(md.plainText).toBe(result) + }) + + test('enable html', () => { + const md = new MarkdownIt({ + html: true + }); + md.use(plainText); + + const testData = `
+ html +
`; + const result = `
html
`; + md.render(testData); + + expect(md.plainText).toBe(result) + }) +}); + +describe('with plugin', () => { + test('emoji', () => { + const md = new MarkdownIt(); + md + .use(emoji) + .use(plainText); + + const testData = ':)'; + const result = `😃 `; + md.render(testData); + + expect(md.plainText).toBe(result) + }) +}); \ No newline at end of file