Skip to content

Commit

Permalink
update dependencies + new version
Browse files Browse the repository at this point in the history
  • Loading branch information
ejrbuss committed Oct 27, 2021
1 parent 0f51959 commit 04fd78a
Show file tree
Hide file tree
Showing 15 changed files with 6,605 additions and 5,258 deletions.
3 changes: 3 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"useTabs": true
}
17 changes: 11 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,18 @@ import markdownToTxt from 'markdown-to-txt';
markdownToTxt('Some *quoted* `*code*`'); // "Some quoted *code*"
```

`markdownToTxt` allows for options object to be passed as a second argument. The following options are available:
`markdownToTxt` accepts [MarkedOptions](https://marked.js.org/using_advanced#options)
as its second argument.

| Option | Default | Description |
|-|-|-|
| escapeHtml | `true` | Escapes HTML in the final string |
| gfm | `true` | Uses GitHub flavour markdown (passed through to marked) |
| pedantic | `false` | Conform to markdown<i></i>.pl (passed through to marked) |
**NOTE:** The output of markdownToTxt is **not** sanitized. The output may contain
valid HTML, JavaScript, etc. Be sure to sanitize if the output is intended for
web use.

## Changelog

- **2.0.0**
- Options argument for `markdownToTxt` changed to align with latest version of marked.
- `escapeHtml` option removed

## Contact

Expand Down
16 changes: 16 additions & 0 deletions dist/markdown-to-txt.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { MarkedOptions } from "marked";
/**
* Converts markdown to plaintext using the marked Markdown library.
* Accepts [MarkedOptions](https://marked.js.org/using_advanced#options) as
* the second argument.
*
* NOTE: The output of markdownToTxt is NOT sanitized. The output may contain
* valid HTML, JavaScript, etc. Be sure to sanitize if the output is intended
* for web use.
*
* @param markdown the markdown text to txtify
* @param options the marked options
* @returns the unmarked text
*/
export declare function markdownToTxt(markdown: string, options?: MarkedOptions): string;
export default markdownToTxt;
61 changes: 61 additions & 0 deletions dist/markdown-to-txt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.markdownToTxt = void 0;
const marked_1 = __importDefault(require("marked"));
const lodash_1 = require("lodash");
const block = (text) => text + "\n\n";
const escapeBlock = (text) => (0, lodash_1.escape)(text) + "\n\n";
const line = (text) => text + "\n";
const inline = (text) => text;
const newline = () => "\n";
const empty = () => "";
const TxtRenderer = {
// Block elements
code: escapeBlock,
blockquote: block,
html: empty,
heading: block,
hr: newline,
list: (text) => block(text.trim()),
listitem: line,
checkbox: empty,
paragraph: block,
table: (header, body) => line(header + body),
tablerow: (text) => line(text.trim()),
tablecell: (text) => text + " ",
// Inline elements
strong: inline,
em: inline,
codespan: inline,
br: newline,
del: inline,
link: (_0, _1, text) => text,
image: (_0, _1, text) => text,
text: inline,
// etc.
options: {},
};
/**
* Converts markdown to plaintext using the marked Markdown library.
* Accepts [MarkedOptions](https://marked.js.org/using_advanced#options) as
* the second argument.
*
* NOTE: The output of markdownToTxt is NOT sanitized. The output may contain
* valid HTML, JavaScript, etc. Be sure to sanitize if the output is intended
* for web use.
*
* @param markdown the markdown text to txtify
* @param options the marked options
* @returns the unmarked text
*/
function markdownToTxt(markdown, options) {
const unmarked = (0, marked_1.default)(markdown, Object.assign(Object.assign({}, options), { renderer: TxtRenderer }));
const unescaped = (0, lodash_1.unescape)(unmarked);
const trimmed = unescaped.trim();
return trimmed;
}
exports.markdownToTxt = markdownToTxt;
exports.default = markdownToTxt;
File renamed without changes.
144 changes: 144 additions & 0 deletions dist/markdown-to-txt.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const markdown_to_txt_1 = __importDefault(require("./markdown-to-txt"));
// Test cases derived from https://guides.github.com/features/mastering-markdown/
test("Headers", () => {
const original = `
# This is an \`<h1>\` tag
## This is an \`<h2>\` tag
###### This is an \`<h6>\` tag`;
const expected = `
This is an <h1> tag
This is an <h2> tag
This is an <h6> tag`;
expect((0, markdown_to_txt_1.default)(original)).toEqual(expected.trim());
});
test("Emphasis", () => {
const original = `
*This text will be italic*
_This will also be italic_
**This text will be bold**
__This will also be bold__
_You **can** combine them_`;
const expected = `
This text will be italic
This will also be italic
This text will be bold
This will also be bold
You can combine them
`;
expect((0, markdown_to_txt_1.default)(original)).toEqual(expected.trim());
});
test("Lists", () => {
const original = `
* Item 1
* Item 2
- Item 1
- Item 2`;
const expected = `
Item 1
Item 2
Item 1
Item 2`;
expect((0, markdown_to_txt_1.default)(original)).toEqual(expected.trim());
});
test("Images", () => {
const original = `
![GitHub Logo](/images/logo.png)
Format: ![Alt Text](url)`;
const expected = `
GitHub Logo
Format: Alt Text`;
expect((0, markdown_to_txt_1.default)(original)).toEqual(expected.trim());
});
test("Links", () => {
const original = `
http://github.com - automatic!
[GitHub](http://github.com)`;
const expected = `
http://github.com - automatic!
GitHub`;
expect((0, markdown_to_txt_1.default)(original)).toEqual(expected.trim());
});
test("Blockquotes", () => {
const original = `
As Kanye West said:
> We're living the future so
> the present is our past.`;
const expected = `
As Kanye West said:
We're living the future so
the present is our past.`;
expect((0, markdown_to_txt_1.default)(original)).toEqual(expected.trim());
});
test("Inline code", () => {
const original = `
I think you should use an
\`<addr>\` element here instead.`;
const expected = `
I think you should use an
<addr> element here instead.`;
expect((0, markdown_to_txt_1.default)(original)).toEqual(expected.trim());
});
test("Code block", () => {
const original = `
\`\`\`javascript
function fancyAlert(arg) {
if(arg) {
$.facebox({div:'#foo'})
}
}
\`\`\`
function fancyAlert(arg) {
if(arg) {
$.facebox({div:'#foo'})
}
}`;
const expected = `
function fancyAlert(arg) {
if(arg) {
$.facebox({div:'#foo'})
}
}
function fancyAlert(arg) {
if(arg) {
$.facebox({div:'#foo'})
}
}
`;
expect((0, markdown_to_txt_1.default)(original)).toEqual(expected.trim());
});
test("Tables", () => {
const original = `
First Header | Second Header
------------ | -------------
Content from cell 1 | Content from cell 2
Content in the first column | Content in the second column`;
const expected = `
First Header Second Header
Content from cell 1 Content from cell 2
Content in the first column Content in the second column`;
expect((0, markdown_to_txt_1.default)(original)).toEqual(expected.trim());
});
test("Strikethrough", () => {
const original = `
~~this~~`;
const expected = `
this`;
expect((0, markdown_to_txt_1.default)(original)).toEqual(expected.trim());
});
9 changes: 3 additions & 6 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
"roots": [
"<rootDir>/src"
],
"transform": {
"^.+\\.tsx?$": "ts-jest"
},
preset: 'ts-jest',
testEnvironment: 'node',
};
19 changes: 0 additions & 19 deletions lib/markdown-to-txt.d.ts

This file was deleted.

83 changes: 0 additions & 83 deletions lib/markdown-to-txt.js

This file was deleted.

Loading

0 comments on commit 04fd78a

Please sign in to comment.