Skip to content

feat(v2): allow custom heading component - #1687

Merged
endiliey merged 2 commits into
masterfrom
endiliey/heading
Jul 23, 2019
Merged

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

Conversation

@endiliey

@endiliey endiliey commented Jul 22, 2019

Copy link
Copy Markdown
Contributor

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

docusaurus-bot commented Jul 22, 2019

Copy link
Copy Markdown
Contributor

Deploy preview for docusaurus-2 ready!

Built with commit 277480e

https://deploy-preview-1687--docusaurus-2.netlify.com

@docusaurus-bot

docusaurus-bot commented Jul 22, 2019

Copy link
Copy Markdown
Contributor

Deploy preview for docusaurus-preview ready!

Built with commit 277480e

https://deploy-preview-1687--docusaurus-preview.netlify.com

@wgao19 wgao19 mentioned this pull request Jul 22, 2019

@yangshun yangshun left a comment

Copy link
Copy Markdown
Contributor

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
Copy Markdown
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
mrizwanashiq pushed a commit to mrizwanashiq/docusaurus that referenced this pull request Jun 25, 2026
* feat(v2): allow custom heading component

* changelog
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