-
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.
feat: write out reusable content as tags (#827)
* feat: write out reusable content as tags * fix: test
- Loading branch information
1 parent
44e20c5
commit 5b3e0e2
Showing
7 changed files
with
62 additions
and
30 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 |
---|---|---|
@@ -1,32 +1,44 @@ | ||
import { mdast, md } from '../../index'; | ||
|
||
describe('reusable content compiler', () => { | ||
it('writes an undefined reusable content block back to markdown', () => { | ||
it('writes an undefined reusable content block as a tag', () => { | ||
const doc = '<Undefined />'; | ||
const tree = mdast(doc); | ||
|
||
expect(md(tree)).toMatch(doc); | ||
}); | ||
|
||
it('writes a defined reusable content block back to markdown', () => { | ||
const reusableContent = { | ||
it('writes a defined reusable content block as a tag', () => { | ||
const tags = { | ||
Defined: '# Whoa', | ||
}; | ||
const doc = '<Defined />'; | ||
const tree = mdast(doc, { reusableContent }); | ||
const tree = mdast(doc, { reusableContent: { tags } }); | ||
|
||
expect(tree.children[0].children[0].type).toBe('heading'); | ||
expect(md(tree)).toMatch(doc); | ||
}); | ||
|
||
it('writes a defined reusable content block with multiple words back to markdown', () => { | ||
const reusableContent = { | ||
it('writes a defined reusable content block with multiple words as a tag', () => { | ||
const tags = { | ||
MyCustomComponent: '# Whoa', | ||
}; | ||
const doc = '<MyCustomComponent />'; | ||
const tree = mdast(doc, { reusableContent }); | ||
const tree = mdast(doc, { reusableContent: { tags } }); | ||
|
||
expect(tree.children[0].children[0].type).toBe('heading'); | ||
expect(md(tree)).toMatch(doc); | ||
}); | ||
|
||
describe('writeTags = false', () => { | ||
it('writes a reusable content block as content', () => { | ||
const tags = { | ||
Defined: '# Whoa', | ||
}; | ||
const doc = '<Defined />'; | ||
const string = md(doc, { reusableContent: { tags, writeTags: false } }); | ||
|
||
expect(string).toBe('# Whoa\n'); | ||
}); | ||
}); | ||
}); |
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
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
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,11 @@ | ||
import { type } from '../transform/reusable-content'; | ||
|
||
export default function ReusableContentCompiler() { | ||
const { writeTags = true } = this.data('reusableContent') || {}; | ||
const { Compiler } = this; | ||
const { visitors } = Compiler.prototype; | ||
|
||
visitors[type] = node => `<${node.tag} />`; | ||
visitors[type] = function (node) { | ||
return writeTags ? `<${node.tag} />` : this.block(node); | ||
}; | ||
} |
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