Skip to content

Commit

Permalink
chore: strip out the search parameter in shared link (microsoft#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
mxschmitt authored May 3, 2021
1 parent feb4af6 commit 2b2b9c2
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/theme/Heading/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

/* eslint-disable jsx-a11y/anchor-has-content, jsx-a11y/anchor-is-valid */
import React from 'react';
import clsx from 'clsx';
import { useThemeConfig } from '@docusaurus/theme-common';
import './styles.css';
import styles from './styles.module.css';

// This component was modified to strip out the search parameter in the link

const Heading = Tag => function TargetComponent({
id,
...props
}) {
const {
navbar: {
hideOnScroll
}
} = useThemeConfig();

if (!id) {
return <Tag {...props} />;
}

const handleOnHeadingClick = (ev) => {
ev.preventDefault()
const url = new URL(ev.target.href)
url.search = ""
history.pushState('', document.title, url.toString());
}

return <Tag {...props}>
<a aria-hidden="true" tabIndex={-1} className={clsx('anchor', {
[styles.enhancedAnchor]: !hideOnScroll
})} id={id} />
{props.children}
<a className="hash-link" href={`#${id}`} onClick={handleOnHeadingClick} title="Direct link to heading">
#
</a>
</Tag>;
};

export default Heading;
23 changes: 23 additions & 0 deletions src/theme/Heading/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

.anchor {
display: block;
position: relative;
top: -0.5rem;
}

.hash-link {
opacity: 0;
padding-left: 0.5rem;
transition: opacity var(--ifm-transition-fast);
}

.hash-link:focus,
*:hover > .hash-link {
opacity: 1;
}
10 changes: 10 additions & 0 deletions src/theme/Heading/styles.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

.enhancedAnchor {
top: calc(var(--ifm-navbar-height) * -1);
}

0 comments on commit 2b2b9c2

Please sign in to comment.