fix(aio): fix window title on Home page (#20440)
Using `display: none` on the `<h1>` causes `innerText` to not work as expected and include the icon ligature (`link`) in the title. This caused the window title on the angular.io Home page to appear as "Angular - link". This commit fixes it by not generating anchors at all for headings with the `no-anchor` class. Fixes #20427 PR Close #20440
This commit is contained in:

committed by
Miško Hevery

parent
c28b52187a
commit
7e38f4fd1f
@ -1,9 +1,34 @@
|
||||
const has = require('hast-util-has-property');
|
||||
const is = require('hast-util-is-element');
|
||||
const slug = require('rehype-slug');
|
||||
const link = require('rehype-autolink-headings');
|
||||
const visit = require('unist-util-visit');
|
||||
|
||||
/**
|
||||
* Get remark to inject anchors into headings
|
||||
* Get remark to add IDs to headings and inject anchors into them.
|
||||
* This is a stripped-down equivalent of [rehype-autolink-headings](https://github.com/wooorm/rehype-autolink-headings)
|
||||
* that supports ignoring headings with the `no-anchor` class.
|
||||
*/
|
||||
const HEADINGS = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'];
|
||||
const NO_ANCHOR_CLASS = 'no-anchor';
|
||||
|
||||
const clone = obj => JSON.parse(JSON.stringify(obj));
|
||||
const hasClass = (node, cls) => {
|
||||
const className = node.properties.className;
|
||||
return className && className.includes(cls);
|
||||
};
|
||||
|
||||
const link = options =>
|
||||
tree => visit(tree, node => {
|
||||
if (is(node, HEADINGS) && has(node, 'id') && !hasClass(node, NO_ANCHOR_CLASS)) {
|
||||
node.children.unshift({
|
||||
type: 'element',
|
||||
tagName: 'a',
|
||||
properties: Object.assign(clone(options.properties), {href: `#${node.properties.id}`}),
|
||||
children: clone(options.content)
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = [
|
||||
slug,
|
||||
[link, {
|
||||
@ -12,11 +37,13 @@ module.exports = [
|
||||
className: ['header-link'],
|
||||
'aria-hidden': 'true'
|
||||
},
|
||||
content: {
|
||||
type: 'element',
|
||||
tagName: 'i',
|
||||
properties: {className: ['material-icons']},
|
||||
children: [{ type: 'text', value: 'link' }]
|
||||
}
|
||||
content: [
|
||||
{
|
||||
type: 'element',
|
||||
tagName: 'i',
|
||||
properties: {className: ['material-icons']},
|
||||
children: [{ type: 'text', value: 'link' }]
|
||||
}
|
||||
]
|
||||
}]
|
||||
];
|
||||
|
Reference in New Issue
Block a user