-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
| [![PR App][icn]][demo] | RM-9817 | | :--------------------: | :-----: | ## 🧰 Changes Updates `plain` and `hast`. These are used in the main app for indexing. ## 🧬 QA & Testing - [Broken on production][prod]. - [Working in this PR app][demo]. [demo]: https://markdown-pr-PR_NUMBER.herokuapp.com [prod]: https://SUBDOMAIN.readme.io [icn]: https://user-images.githubusercontent.com/886627/160426047-1bee9488-305a-4145-bb2b-09d8b757d38a.svg
- Loading branch information
1 parent
67cae3b
commit 0b60259
Showing
13 changed files
with
164 additions
and
164 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,24 @@ | ||
import { hast } from '../../lib'; | ||
import { h } from 'hastscript'; | ||
|
||
describe('hast transformer', () => { | ||
it('parses components into the tree', () => { | ||
const md = ` | ||
## Test | ||
<Example /> | ||
`; | ||
const components = { | ||
Example: "## It's coming from within the component!", | ||
}; | ||
|
||
const expected = h( | ||
undefined, | ||
h('h2', undefined, 'Test'), | ||
'\n', | ||
h('h2', undefined, "It's coming from within the component!"), | ||
); | ||
|
||
expect(hast(md, { components })).toStrictEqualExceptPosition(expected); | ||
}); | ||
}); |
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,56 @@ | ||
import { hast, plain } from '../../index'; | ||
|
||
describe('plain compiler', () => { | ||
it('returns plain text of markdown components', () => { | ||
const md = ` | ||
## Hello! | ||
Is it _me_ you're looking for? | ||
`; | ||
|
||
const tree = hast(md); | ||
expect(plain(tree)).toEqual("Hello! Is it me you're looking for?"); | ||
}); | ||
|
||
it("compiles br's to ''", () => { | ||
const txt = '<br />'; | ||
|
||
expect(plain(hast(txt))).toBe(''); | ||
}); | ||
|
||
it("compiles hr's to ''", () => { | ||
const txt = '<hr />'; | ||
|
||
expect(plain(hast(txt))).toBe(''); | ||
}); | ||
|
||
it('compiles callouts', () => { | ||
const txt = ` | ||
> 📘 Title | ||
> | ||
> Some body | ||
`; | ||
const tree = hast(txt); | ||
|
||
expect(plain(tree)).toBe('Title Some body'); | ||
}); | ||
|
||
it('compiles markdown tables', () => { | ||
const txt = ` | ||
| Header 1 | Header 2 | | ||
| :------- | :------- | | ||
| Cell 1 | Cell 2 | | ||
`; | ||
|
||
expect(plain(hast(txt))).toBe('Header 1 Header 2 Cell 1 Cell 2'); | ||
}); | ||
|
||
it('compiles images to their title', () => { | ||
const txt = ` | ||
![image **label**](http://placekitten.com/600/600 "entitled kittens") | ||
`; | ||
const tree = hast(txt); | ||
|
||
expect(plain(tree)).toBe('entitled kittens'); | ||
}); | ||
}); |
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,22 @@ | ||
import astProcessor from './ast-processor'; | ||
import remarkRehype from 'remark-rehype'; | ||
import { injectComponents } from '../processor/transform'; | ||
import { MdastComponents } from '../types'; | ||
import mdast from './mdast'; | ||
|
||
interface Options { | ||
components?: Record<string, string>; | ||
} | ||
|
||
const hast = (text: string, opts: Options = {}) => { | ||
const components: MdastComponents = Object.entries(opts.components || {}).reduce((memo, [name, doc]) => { | ||
memo[name] = mdast(doc); | ||
return memo; | ||
}, {}); | ||
|
||
const processor = astProcessor(opts).use(injectComponents({ components })).use(remarkRehype); | ||
|
||
return processor.runSync(processor.parse(text)); | ||
}; | ||
|
||
export default hast; |
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,7 +1,10 @@ | ||
import astProcessor, { MdastOpts, remarkPlugins } from './ast-processor'; | ||
import compile from './compile' | ||
import compile from './compile'; | ||
import hast from './hast'; | ||
import mdast from './mdast'; | ||
import mdx from './mdx'; | ||
import plain from './plain'; | ||
import run from './run'; | ||
|
||
export type { MdastOpts }; | ||
export { astProcessor, compile, mdx, run, remarkPlugins } | ||
export { astProcessor, compile, hast, mdast, mdx, plain, run, remarkPlugins }; |
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,11 @@ | ||
import { readmeComponentsTransformer } from '../processor/transform'; | ||
import astProcessor, { MdastOpts } from './ast-processor'; | ||
|
||
const mdast: any = (text: string, opts: MdastOpts = {}) => { | ||
const processor = astProcessor(opts).use(readmeComponentsTransformer({ components: opts.components })); | ||
|
||
const tree = processor.parse(text); | ||
return processor.runSync(tree); | ||
}; | ||
|
||
export default mdast; |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Oops, something went wrong.