-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@hashicorp/platform-remark-plugins': minor | ||
--- | ||
|
||
Add `jump-to-section` plugin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import flatMap from 'unist-util-flatmap' | ||
import { is } from 'unist-util-is' | ||
import { u } from 'unist-builder' | ||
|
||
export default function jumpToSection() { | ||
return function transformer(tree) { | ||
flatMap(tree, (node, index) => { | ||
if (is(node, 'heading') && index === 0) { | ||
return [ | ||
node, | ||
u('mdxJsxFlowElement', { | ||
name: 'JumpToSection', | ||
attributes: [ | ||
u('mdxJsxAttribute', { | ||
name: 'headings', | ||
value: u( | ||
'mdxJsxAttributeValueExpression', | ||
{ data: { estree: undefined } }, | ||
'headings' | ||
), | ||
}), | ||
], | ||
}), | ||
] | ||
} | ||
|
||
// No change | ||
return [node] | ||
}) | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
packages/remark-plugins/plugins/jump-to-section/index.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { remark } from 'remark' | ||
import remarkMdx from 'remark-mdx' | ||
|
||
import jumpToSection from '.' | ||
|
||
describe('jump-to', () => { | ||
it('should insert a jump-to component after the first h1', () => { | ||
const md = ` | ||
# Hello World | ||
## Hello World | ||
### Hello World | ||
#### Hello World | ||
##### Hello World | ||
###### Hello World | ||
` | ||
|
||
const result = execute(md) | ||
|
||
const expected = `# Hello World | ||
<JumpToSection headings={headings}/> | ||
## Hello World | ||
### Hello World | ||
#### Hello World | ||
##### Hello World | ||
###### Hello World | ||
` | ||
expect(result).toEqual(expected) | ||
}) | ||
}) | ||
|
||
function execute(input, options = {}) { | ||
return remark() | ||
.use(jumpToSection, options) | ||
.use(remarkMdx) | ||
.processSync(input) | ||
.toString() | ||
} |