Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(gatsby-remark-autolink-headers): use offsetY prop as scroll-margin-top #36961

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,70 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`gatsby-remark-autolink-headers adds anchor after header and scroll-margin-top when isIconAfterHeader and offsetY props are passed 1`] = `
Object {
"children": Array [
Object {
"position": Object {
"end": Object {
"column": 14,
"line": 1,
"offset": 13,
},
"start": Object {
"column": 3,
"line": 1,
"offset": 2,
},
},
"type": "text",
"value": "Heading Uno",
},
Object {
"children": Array [],
"data": Object {
"hChildren": Array [
Object {
"type": "raw",
"value": "<svg aria-hidden=\\"true\\" focusable=\\"false\\" height=\\"16\\" version=\\"1.1\\" viewBox=\\"0 0 16 16\\" width=\\"16\\"><path fill-rule=\\"evenodd\\" d=\\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\\"></path></svg>",
},
],
"hProperties": Object {
"aria-label": "heading uno permalink",
"class": "anchor after",
},
},
"title": null,
"type": "link",
"url": "#heading-uno",
},
],
"data": Object {
"hProperties": Object {
"id": "heading-uno",
"style": "position:relative;scroll-margin-top:32px;",
},
"htmlAttributes": Object {
"id": "heading-uno",
},
"id": "heading-uno",
},
"depth": 1,
"position": Object {
"end": Object {
"column": 14,
"line": 1,
"offset": 13,
},
"start": Object {
"column": 1,
"line": 1,
"offset": 0,
},
},
"type": "heading",
}
`;

exports[`gatsby-remark-autolink-headers adds id to a markdown header 1`] = `
Object {
"children": Array [
Expand Down Expand Up @@ -371,6 +436,71 @@ Object {
}
`;

exports[`gatsby-remark-autolink-headers adds scroll-margin-top when offsetY prop is passed 1`] = `
Object {
"children": Array [
Object {
"children": Array [],
"data": Object {
"hChildren": Array [
Object {
"type": "raw",
"value": "<svg aria-hidden=\\"true\\" focusable=\\"false\\" height=\\"16\\" version=\\"1.1\\" viewBox=\\"0 0 16 16\\" width=\\"16\\"><path fill-rule=\\"evenodd\\" d=\\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\\"></path></svg>",
},
],
"hProperties": Object {
"aria-label": "heading uno permalink",
"class": "anchor before",
},
},
"title": null,
"type": "link",
"url": "#heading-uno",
},
Object {
"position": Object {
"end": Object {
"column": 14,
"line": 1,
"offset": 13,
},
"start": Object {
"column": 3,
"line": 1,
"offset": 2,
},
},
"type": "text",
"value": "Heading Uno",
},
],
"data": Object {
"hProperties": Object {
"id": "heading-uno",
"style": "position:relative;scroll-margin-top:32px;",
},
"htmlAttributes": Object {
"id": "heading-uno",
},
"id": "heading-uno",
},
"depth": 1,
"position": Object {
"end": Object {
"column": 14,
"line": 1,
"offset": 13,
},
"start": Object {
"column": 1,
"line": 1,
"offset": 0,
},
},
"type": "heading",
}
`;

exports[`gatsby-remark-autolink-headers does not add data to a markdown heading when elements prop is passed with no matching heading type 1`] = `
Object {
"children": Array [
Expand Down
37 changes: 36 additions & 1 deletion packages/gatsby-remark-autolink-headers/src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,14 +247,49 @@ describe(`gatsby-remark-autolink-headers`, () => {
const transformed = plugin({ markdownAST }, { isIconAfterHeader })

visit(transformed, `heading`, node => {
expect(node.data.hProperties.style).toContain(`position:relative`)
expect(node.data.hProperties.style).toInclude(`position:relative`)
expect(node.children).toHaveLength(2)
expect(node.children[1].data.hProperties.class).toContain(`after`)

expect(node).toMatchSnapshot()
})
})

it(`adds scroll-margin-top when offsetY prop is passed`, () => {
const markdownAST = remark.parse(`# Heading Uno`)

const offsetY = 32
const transformed = plugin({ markdownAST }, { offsetY })

visit(transformed, `heading`, node => {
expect(node.data.hProperties.style).toInclude(
`scroll-margin-top:${offsetY}px`
)

expect(node).toMatchSnapshot()
})
})

it(`adds anchor after header and scroll-margin-top when isIconAfterHeader and offsetY props are passed`, () => {
const markdownAST = remark.parse(`# Heading Uno`)

const isIconAfterHeader = true
const offsetY = 32
const transformed = plugin({ markdownAST }, { isIconAfterHeader, offsetY })

visit(transformed, `heading`, node => {
expect(node.data.hProperties.style).toInclude(`position:relative`)
expect(node.children).toHaveLength(2)
expect(node.children[1].data.hProperties.class).toContain(`after`)

expect(node.data.hProperties.style).toInclude(
`scroll-margin-top:${offsetY}px`
)

expect(node).toMatchSnapshot()
})
})

it(`adds id to a markdown heading when elements prop is passed with matching heading type`, () => {
const markdownAST = remark.parse(`# Heading Uno`)

Expand Down
48 changes: 0 additions & 48 deletions packages/gatsby-remark-autolink-headers/src/gatsby-browser.js

This file was deleted.

47 changes: 19 additions & 28 deletions packages/gatsby-remark-autolink-headers/src/gatsby-ssr.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ const pluginDefaults = {
}

exports.onRenderBody = ({ setHeadComponents }, pluginOptions) => {
const { className, icon, offsetY } = Object.assign(
pluginDefaults,
pluginOptions
)
const { className, icon } = Object.assign(pluginDefaults, pluginOptions)

const styles = `
.${className}.before {
Expand Down Expand Up @@ -53,35 +50,29 @@ exports.onRenderBody = ({ setHeadComponents }, pluginOptions) => {
// older browsers (that do not implement `let`). See:
// - https://github.com/gatsbyjs/gatsby/issues/21058
// - https://github.com/gatsbyjs/gatsby/pull/21083
const script = `
document.addEventListener("DOMContentLoaded", function(event) {
var hash = window.decodeURI(location.hash.replace('#', ''))
if (hash !== '') {
var element = document.getElementById(hash)
if (element) {
var scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop
var clientTop = document.documentElement.clientTop || document.body.clientTop || 0
var offset = element.getBoundingClientRect().top + scrollTop - clientTop
// Wait for the browser to finish rendering before scrolling.
setTimeout((function() {
window.scrollTo(0, offset - ${offsetY})
}), 0)
}
}
})
`
// const script = `
// document.addEventListener("DOMContentLoaded", function(event) {
// var hash = window.decodeURI(location.hash.replace('#', ''))
// if (hash !== '') {
// var element = document.getElementById(hash)
// if (element) {
// var scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop
// var clientTop = document.documentElement.clientTop || document.body.clientTop || 0
// var offset = element.getBoundingClientRect().top + scrollTop - clientTop
// // Wait for the browser to finish rendering before scrolling.
// setTimeout((function() {
// window.scrollTo(0, offset - ${offsetY})
// }), 0)
// }
// }
// })
// `

const style = icon ? (
<style key={`gatsby-remark-autolink-headers-style`} type="text/css">
{styles}
</style>
) : undefined

return setHeadComponents([
style,
<script
key={`gatsby-remark-autolink-headers-script`}
dangerouslySetInnerHTML={{ __html: script }}
/>,
])
return setHeadComponents([style])
}
13 changes: 12 additions & 1 deletion packages/gatsby-remark-autolink-headers/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module.exports = (
enableCustomId = false,
isIconAfterHeader = false,
elements = null,
offsetY = 0,
}
) => {
slugs.reset()
Expand Down Expand Up @@ -61,8 +62,10 @@ module.exports = (
patch(data.htmlAttributes, `id`, id)
patch(data.hProperties, `id`, id)

let style = ``

if (icon !== false) {
patch(data.hProperties, `style`, `position:relative;`)
style += `position:relative;`
const label = id.split(`-`).join(` `)
const method = isIconAfterHeader ? `push` : `unshift`
node.children[method]({
Expand All @@ -85,6 +88,14 @@ module.exports = (
},
})
}

if (offsetY !== 0) {
style += `scroll-margin-top:${offsetY}px;`
}

if (style.length > 0) {
patch(data.hProperties, `style`, style)
}
})

return markdownAST
Expand Down