forked from ejrbuss/markdown-to-txt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
6,605 additions
and
5,258 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"useTabs": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = ` | ||
 | ||
Format: `; | ||
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()); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.