Skip to content

Commit

Permalink
Merge pull request #10 from instantish/marked
Browse files Browse the repository at this point in the history
feat: rewrite using `marked`
  • Loading branch information
marissamarym authored Mar 29, 2022
2 parents 5e942a5 + 74efe6d commit 14b350e
Show file tree
Hide file tree
Showing 10 changed files with 1,044 additions and 4,982 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Text is truncated to fit within the Slack API's limits.

### Not Yet Supported Markdown Elements

- - Block quotes (limited functionality; does not support lists, headings, or images within the block quote)
- Block quotes (limited functionality; does not support lists, headings, or images within the block quote)

## Installation

Expand Down
5,307 changes: 868 additions & 4,439 deletions package-lock.json

Large diffs are not rendered by default.

14 changes: 3 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,13 @@
"license": "ISC",
"dependencies": {
"@slack/types": "^2.1.0",
"rehype-raw": "^5.1.0",
"rehype-stringify": "^8.0.0",
"remark-gfm": "^1.0.0",
"remark-inline-links": "^5.0.0",
"remark-parse": "^9.0.0",
"remark-rehype": "^8.1.0",
"remark-unwrap-images": "^2.1.0",
"turndown": "^7.0.0",
"turndown-plugin-gfm": "^1.0.2",
"unified": "^9.2.1"
"fast-xml-parser": "^4.0.6",
"marked": "^4.0.12"
},
"devDependencies": {
"@types/jest": "^26.0.23",
"@types/marked": "^4.0.2",
"@types/node": "^15.12.4",
"@types/turndown": "^5.0.0",
"gts": "^3.1.0",
"jest": "^27.0.5",
"release-it": "^14.11.6",
Expand Down
32 changes: 5 additions & 27 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
import unified from 'unified';
import markdown from 'remark-parse';
import gfm from 'remark-gfm';
import type {KnownBlock} from '@slack/types';
import TurndownService from 'turndown';
import remark2rehype from 'remark-rehype';
import html from 'rehype-stringify';
import raw from 'rehype-raw';
import {parseBlocks} from './parser/internal';
import type {Root} from './markdown';
import type {ParsingOptions} from './types';
const {gfm: turndownGfm} = require('turndown-plugin-gfm');
import {marked} from 'marked';

/**
* Parses Markdown content into Slack BlockKit Blocks.
Expand All @@ -19,8 +11,8 @@ const {gfm: turndownGfm} = require('turndown-plugin-gfm');
* - Supports images
* - Supports thematic breaks / dividers
*
* Per Slack limitations, these markdown attributes are not supported:
* - Tables (removed)
* Per Slack limitations, these markdown attributes are not completely supported:
* - Tables: they will be copied but Slack will render them as text
* - Block quotes (limited functionality; does not support lists, headings, or images within the block quote)
*
* Supports GitHub-flavoured Markdown.
Expand All @@ -32,21 +24,7 @@ export async function markdownToBlocks(
body: string,
options: ParsingOptions = {}
): Promise<KnownBlock[]> {
// TODO: Make this more efficient by using less intermediary parsers
const tokens = marked.lexer(body);

const turndownService = new TurndownService().use(turndownGfm);

const rawHtml = await unified()
.use(markdown)
.use(gfm)
.use(remark2rehype, {allowDangerousHtml: true})
.use(raw)
.use(html)
.process(body);

const rawMarkdown = turndownService.turndown(String(rawHtml));

const root = unified().use(markdown).use(gfm).parse(rawMarkdown);

return parseBlocks(root as unknown as Root, options);
return parseBlocks(tokens, options);
}
184 changes: 0 additions & 184 deletions src/markdown/ast.ts

This file was deleted.

2 changes: 0 additions & 2 deletions src/markdown/index.ts

This file was deleted.

Loading

0 comments on commit 14b350e

Please sign in to comment.