-
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.
- Loading branch information
Showing
4 changed files
with
82 additions
and
9 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,55 @@ | ||
import { mdast } from '../../index'; | ||
|
||
describe('reusable content transfomer', () => { | ||
it('should unwrap the content when `wrap: false`', () => { | ||
const tags = { | ||
Test: ` | ||
# Test | ||
[link](http://example.com) | ||
`, | ||
}; | ||
const md = ` | ||
Before | ||
<Test /> | ||
After | ||
`; | ||
|
||
const tree = mdast(md, { reusableContent: { tags, wrap: false } }); | ||
|
||
expect(tree.children[0].children[0].value).toBe('Before'); | ||
expect(tree.children[1].type).toBe('heading'); | ||
expect(tree.children[2].type).toBe('paragraph'); | ||
expect(tree.children[3].children[0].value).toBe('After'); | ||
}); | ||
|
||
it('should unwrap consecutive the content when `wrap: false`', () => { | ||
const tags = { | ||
Test: ` | ||
# Test | ||
[link](http://example.com) | ||
`, | ||
}; | ||
const md = ` | ||
Before | ||
<Test /> | ||
<Test /> | ||
After | ||
`; | ||
|
||
const tree = mdast(md, { reusableContent: { tags, wrap: false } }); | ||
|
||
expect(tree.children[0].children[0].value).toBe('Before'); | ||
expect(tree.children[1].type).toBe('heading'); | ||
expect(tree.children[2].type).toBe('paragraph'); | ||
expect(tree.children[3].type).toBe('heading'); | ||
expect(tree.children[4].type).toBe('paragraph'); | ||
expect(tree.children[5].children[0].value).toBe('After'); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import reusableContent from './reusable-content'; | ||
import singleCodeTabs from './single-code-tabs'; | ||
import tableCellInlineCode from './table-cell-inline-code'; | ||
|
||
export const remarkTransformers = [singleCodeTabs]; | ||
export const remarkTransformers = [singleCodeTabs, reusableContent]; | ||
export const rehypeTransformers = [tableCellInlineCode]; |
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,19 @@ | ||
import { visit } from 'unist-util-visit'; | ||
|
||
import { type } from '../parse/reusable-content-parser'; | ||
|
||
function reusableContent() { | ||
const { wrap = true } = this.data('reusableContent'); | ||
|
||
return tree => { | ||
if (wrap) return tree; | ||
|
||
visit(tree, type, (node, index, parent) => { | ||
parent.children.splice(index, 1, ...node.children); | ||
}); | ||
|
||
return tree; | ||
}; | ||
} | ||
|
||
export default reusableContent; |