Skip to content

Comments

feat(v2): allow custom heading component#1687

Merged
endiliey merged 2 commits intomasterfrom
endiliey/heading
Jul 23, 2019
Merged

feat(v2): allow custom heading component#1687
endiliey merged 2 commits intomasterfrom
endiliey/heading

Conversation

@endiliey
Copy link
Contributor

@endiliey endiliey commented Jul 22, 2019

Motivation

Rather than hardcoding/ transforming the header automatically in html, we should allow composition style. This will allow user to modify the heading as they like. Example: if they dont want hash icon

previously

const visit = require('unist-util-visit');
const createAnchor = id => ({
type: 'element',
tagName: 'a',
properties: {
ariaHidden: true,
className: 'anchor',
id,
},
});
const createLink = id => ({
type: 'element',
tagName: 'a',
properties: {
ariaHidden: true,
className: 'hash-link',
href: `#${id}`,
},
children: [
{
type: 'text',
value: '#',
},
],
});
const headings = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'];
const visitor = node => {
const {properties} = node;
if (!properties || !properties.id || !headings.includes(node.tagName)) {
return;
}
node.children.unshift(createLink(properties.id));
node.children.unshift(createAnchor(properties.id));
delete properties.id;
};
const transformer = node => {
visit(node, 'element', visitor);
};
const plugin = () => transformer;
module.exports = plugin;

For now i make it the same format as before, feel free to tweak in new pr

Have you read the Contributing Guidelines on pull requests?

yes

Test Plan

  • anchor and hash link still works. same format as before but now easily swizzleable
    image

@endiliey endiliey requested a review from yangshun July 22, 2019 08:07
@facebook-github-bot facebook-github-bot added the CLA Signed Signed Facebook CLA label Jul 22, 2019
@docusaurus-bot
Copy link
Contributor

docusaurus-bot commented Jul 22, 2019

@docusaurus-bot
Copy link
Contributor

docusaurus-bot commented Jul 22, 2019

@wgao19 wgao19 mentioned this pull request Jul 22, 2019
Copy link
Contributor

@yangshun yangshun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would people accidentally cause their headings on blog to break if they only want to modify the headings on docs?

@endiliey
Copy link
Contributor Author

@yangshun

technically theme classic use the same mdxcomponents. if they want different, swizzle the blog layout to use different comp.

anw this pr is better than previous, previously we alrd transform the heading in html 😂

@endiliey endiliey merged commit 84b2270 into master Jul 23, 2019
@endiliey endiliey deleted the endiliey/heading branch July 23, 2019 05:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed Signed Facebook CLA

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants