-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add map option to typography plugin (#15)
* Add custom className map option to typography plugin * Visit listItem nodes directly, rather than through listNode.children * Update typography plugin readme * Add release:pre cmd, bump to expected release * 3.1.0-canary.0 * Expose typography options in allPlugins * 3.1.0-canary.1
- Loading branch information
Showing
6 changed files
with
136 additions
and
44 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
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
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,46 +1,43 @@ | ||
const visit = require('unist-util-visit') | ||
|
||
module.exports = function typographyPlugin() { | ||
module.exports = function typographyPlugin(options = {}) { | ||
function getClassName(elemKey) { | ||
const defaultMap = { | ||
h1: 'g-type-display-2', | ||
h2: 'g-type-display-3', | ||
h3: 'g-type-display-4', | ||
h4: 'g-type-display-5', | ||
h5: 'g-type-display-6', | ||
h6: 'g-type-label', | ||
p: 'g-type-long-body', | ||
li: 'g-type-long-body', | ||
} | ||
const customMap = options.map || {} | ||
return customMap[elemKey] || defaultMap[elemKey] | ||
} | ||
|
||
function addClassName(node, className) { | ||
if (!className) return true | ||
const data = node.data || (node.data = {}) | ||
const props = data.hProperties || (data.hProperties = {}) | ||
data.id = className | ||
props.className = className | ||
} | ||
|
||
return function transformer(tree) { | ||
// Add typography classes to headings | ||
visit(tree, 'heading', node => { | ||
const data = node.data || (node.data = {}) | ||
const props = data.hProperties || (data.hProperties = {}) | ||
visit(tree, 'heading', (node) => { | ||
addClassName(node, getClassName(`h${node.depth}`)) | ||
}) | ||
|
||
if (node.depth === 1) { | ||
data.id = 'g-type-display-2' | ||
props.className = 'g-type-display-2' | ||
} | ||
if (node.depth === 2) { | ||
data.id = 'g-type-display-3' | ||
props.className = 'g-type-display-3' | ||
} | ||
if (node.depth === 3) { | ||
data.id = 'g-type-display-4' | ||
props.className = 'g-type-display-4' | ||
} | ||
if (node.depth === 4) { | ||
data.id = 'g-type-display-5' | ||
props.className = 'g-type-display-5' | ||
} | ||
if (node.depth === 5) { | ||
data.id = 'g-type-display-6' | ||
props.className = 'g-type-display-6' | ||
} | ||
if (node.depth === 6) { | ||
data.id = 'g-type-label' | ||
props.className = 'g-type-label' | ||
} | ||
// Add typography classes to paragraph text | ||
visit(tree, 'paragraph', (node) => { | ||
addClassName(node, getClassName('p')) | ||
}) | ||
|
||
// Add typography classes to list items | ||
visit(tree, 'list', node => { | ||
node.children.map(li => { | ||
const data = li.data || (li.data = {}) | ||
const props = data.hProperties || (data.hProperties = {}) | ||
data.id = 'g-type-long-body' | ||
props.className = 'g-type-long-body' | ||
}) | ||
visit(tree, 'listItem', (node) => { | ||
addClassName(node, getClassName('li')) | ||
}) | ||
} | ||
} |
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