Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
anchorlinks
  • Loading branch information
kellyjosephprice committed May 29, 2024
commit 3dcd5952ae28d9ca7a17f6cd64e95f031c23b9b9
56 changes: 0 additions & 56 deletions components/Heading/index.jsx

This file was deleted.

31 changes: 31 additions & 0 deletions components/Heading/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import React from 'react';

export type Depth = 1 | 2 | 3 | 4 | 5 | 6;

const CreateHeading = (depth: Depth) => {
const Tag: 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6' = `h${depth}`;

const Heading = ({ id, children, ...attrs }: React.PropsWithChildren<HTMLHeadingElement>) => {
if (!children) return '';

return (
// @ts-expect-error
<Tag {...attrs} className={`heading heading-${depth} header-scroll`}>
<div key={`heading-anchor-${id}`} className="heading-anchor anchor waypoint" id={id} />
<div key={`heading-text-${id}`} className="heading-text">
{children}
</div>
<a
key={`heading-anchor-icon-${id}`}
aria-label={`Skip link to ${children[1]}`}
Copy link
Contributor

@rafegoldberg rafegoldberg Jun 2, 2024

Choose a reason for hiding this comment

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

Not sure this children[1] logic still makes sense now that we're getting rid of the showAnchorIcons option?1

Maybe it should just be:

aria-label={`Skip link to ${children}`}

Footnotes

  1. side note: why are we getting rid of this? is it just not used anywhere?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I didn't see it used anywhere, so I didn't bother porting it?

className="heading-anchor-icon fa fa-anchor"
href={`#${id}`}
/>
</Tag>
);
};

return Heading;
};

export default CreateHeading;
2 changes: 1 addition & 1 deletion contexts/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import GlossaryContext from './GlossaryTerms';
import BaseUrlContext from './BaseUrl';
import { VariablesContext } from '@readme/variable';
import { RunOpts } from '../index';
import { RunOpts } from '../lib/run';

type Props = React.PropsWithChildren & Pick<RunOpts, 'baseUrl' | 'terms' | 'variables'>;

Expand Down
42 changes: 25 additions & 17 deletions docs/headings.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
---
title: "Headings"
title: 'Headings'
category: 5fdf7610134322007389a6ed
hidden: false
---

## Examples

> ### Heading Block 3
>
> #### Heading Block 4
>
> ##### Heading Block 5
>
> ###### Heading Block 6
> #### Heading Block 4
>
> ##### Heading Block 5
>
> ###### Heading Block 6

## Edge Cases

Expand All @@ -20,33 +21,40 @@ hidden: false
####Compact Notation

Headers are denoted using a space-separated `#` prefix. While the space is technically required in most standard Markdown implementations, some processors allow for a compact notation as well. ReadMe supports this style, so writing this

###A Valid Heading

Lorem ipsum dolor etc.

> 🛑
```
###A Valid Heading

Lorem ipsum dolor etc.
```
Comment on lines +25 to +29
Copy link
Contributor

Choose a reason for hiding this comment

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

(Not to be picky since I know this isn't technically within the scope of this PR, but I did notice that this compact header notation isn't working with the new MDX processor!)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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


> 🛑
> Compact headings must be followed by two line breaks before the following block.

#### ATX-Style Notation ####

If you prefer, you can "wrap" headers with hashes rather than simply prefixing them:

## ATX Headings are Valid ##
```
## ATX Headings are Valid ##
```

#### Underline Notation

For top-level headings, the underline notation is valid:

Heading One
===========

Heading Two
---
```
Heading One
===========

Heading Two
---
```

### Incremented Anchors

Occasionally, a single doc might contain multiple headings with the same text, which can cause the generated anchor links to conflict. ReadMe's new markdown processor normalizes heading anchors by auto-incrementing similar heading's IDs. Try it out by clicking on this section header _or_ the following sub-section title:

#### Incremented Heading Anchors

#### Incremented Heading Anchors
10 changes: 10 additions & 0 deletions lib/run.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import * as Components from '../components';
import Contexts from '../contexts';
import { VFileWithToc } from '../types';
import { GlossaryTerm } from '../contexts/GlossaryTerms';
import { Depth } from '../components/Heading';

interface Variables {
user: Record<string, string>;
Expand All @@ -26,6 +27,11 @@ export type RunOpts = Omit<RunOptions, 'Fragment'> & {
type ComponentOpts = Record<string, (props: any) => React.ReactNode>;

const makeUseMDXComponents = (more: RunOpts['components']): (() => ComponentOpts) => {
const headings = Array.from({ length: 6 }).reduce((map, _, index) => {
map[`h${index + 1}`] = Components.Heading((index + 1) as Depth);
return map;
}, {});

const components = {
...more,
...Components,
Expand All @@ -37,8 +43,12 @@ const makeUseMDXComponents = (more: RunOpts['components']): (() => ComponentOpts
img: Components.Image,
table: Components.Table,
'table-of-contents': Components.TableOfContents,
// @ts-expect-error
...headings,
};

console.log(headings);

return () => components;
};

Expand Down