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

* feat(v2): allow custom heading component

* changelog
This commit is contained in:
Endi 2019-07-23 12:13:47 +07:00 committed by GitHub
parent 6287739bec
commit 84b2270039
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 62 additions and 81 deletions

View file

@ -11,11 +11,10 @@ const emoji = require('remark-emoji');
const slug = require('remark-slug');
const matter = require('gray-matter');
const stringifyObject = require('stringify-object');
const linkHeadings = require('./rehype/linkHeadings');
const rightToc = require('./remark/rightToc');
const DEFAULT_OPTIONS = {
rehypePlugins: [linkHeadings],
rehypePlugins: [],
remarkPlugins: [emoji, slug, rightToc],
};

View file

@ -1,56 +0,0 @@
/**
* Copyright (c) 2017-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
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;