This repository has been archived by the owner on Feb 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(html pipe): add support for anchors on headings
Fixes #26
- Loading branch information
Showing
7 changed files
with
207 additions
and
5 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
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,64 @@ | ||
/* | ||
* Copyright 2019 Adobe. All rights reserved. | ||
* This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. You may obtain a copy | ||
* of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under | ||
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
* OF ANY KIND, either express or implied. See the License for the specific language | ||
* governing permissions and limitations under the License. | ||
*/ | ||
const fallback = require('mdast-util-to-hast/lib/handlers/heading'); | ||
const GithubSlugger = require('github-slugger'); | ||
|
||
/** | ||
* Utility class injects heading identifiers during the MDAST to VDOM transformation. | ||
*/ | ||
class HeadingHandler { | ||
/** | ||
* Initializes the handler | ||
*/ | ||
constructor() { | ||
// scoping the slugger instance to the current transform operation | ||
// so that heading uniqueness is guaranteed for each transformation separately | ||
this.slugger = new GithubSlugger(); | ||
} | ||
|
||
/** | ||
* Gets the text content for the specified heading. | ||
* @param {UnistParent~Heading} heading The heading node | ||
* @returns {string} The text content for the heading | ||
*/ | ||
static getTextContent(heading) { | ||
return heading.children | ||
.filter(el => el.type === 'text') | ||
.map(el => el.value) | ||
.join('').trim(); | ||
} | ||
|
||
/** | ||
* Reset the heading counter | ||
*/ | ||
reset() { | ||
this.slugger.reset(); | ||
} | ||
|
||
/** | ||
* Returns the handler function | ||
*/ | ||
handler() { | ||
return (h, node) => { | ||
// Prepare the heading id | ||
const headingIdentifier = this.slugger.slug(HeadingHandler.getTextContent(node)); | ||
|
||
// Inject the id after transformation | ||
const n = Object.assign({}, node); | ||
const el = fallback(h, n); | ||
el.properties.id = headingIdentifier; | ||
return el; | ||
}; | ||
} | ||
} | ||
|
||
module.exports = HeadingHandler; |
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,64 @@ | ||
{ | ||
"type": "root", | ||
"children": [{ | ||
"children": [ | ||
{ | ||
"type": "text", | ||
"value": "Foo" | ||
} | ||
], | ||
"depth": 1, | ||
"type": "heading" | ||
}, | ||
{ | ||
"children": [ | ||
{ | ||
"type": "text", | ||
"value": "Bar" | ||
} | ||
], | ||
"depth": 2, | ||
"type": "heading" | ||
}, | ||
{ | ||
"children": [ | ||
{ | ||
"type": "text", | ||
"value": "Baz" | ||
} | ||
], | ||
"depth": 3, | ||
"type": "heading" | ||
}, | ||
{ | ||
"children": [ | ||
{ | ||
"type": "text", | ||
"value": "Qux" | ||
} | ||
], | ||
"depth": 2, | ||
"type": "heading" | ||
}, | ||
{ | ||
"children": [ | ||
{ | ||
"type": "text", | ||
"value": "Bar" | ||
} | ||
], | ||
"depth": 3, | ||
"type": "heading" | ||
}, | ||
{ | ||
"children": [ | ||
{ | ||
"type": "text", | ||
"value": "Bar-1" | ||
} | ||
], | ||
"depth": 4, | ||
"type": "heading" | ||
} | ||
] | ||
} |
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,11 @@ | ||
# Foo | ||
|
||
## Bar | ||
|
||
### Baz | ||
|
||
## Qux | ||
|
||
### Bar | ||
|
||
#### Bar-1 |
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