Skip to content

Commit

Permalink
feat: add wrappers for styling
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamincharity committed Feb 3, 2024
1 parent dfc3a2e commit 4e2f1e0
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 57 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ unified()
.use(rehypeParse, {fragment: true})
.use(rehypeScrollToTop)
.process('<h1>Title</h1><p>Content</p>')

// Or with options:
unified()
.use(rehypeParse, {fragment: true})
.use(rehypeScrollToTop, {
topLink: { disabled: true },
bottomLink: {
text: `Back to top ↑`,
classes: 'animated-link-underline',
},
})
.process('<h1>Title</h1><p>Content</p>')
```

### Input
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@benjc/rehype-scroll-to-top",
"version": "0.1.3",
"version": "0.1.4",
"description": "A Rehype plugin that adds a 'scroll to top' & 'scroll to bottom' links to the page.",
"type": "module",
"main": "dist/index.js",
Expand Down Expand Up @@ -44,7 +44,7 @@
"vitest": "^1.2.2"
},
"peerDependencies": {
"rehype": "^13.0.1",
"rehype": "^11.0.0",
"unified": "^11.0.4"
},
"engines": {
Expand Down
83 changes: 46 additions & 37 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ export type RehypeScrollToLink = {
};

export type RehypeScrollToTopOptions = {
topLink: Partial<RehypeScrollToLink>;
bottomLink: Partial<RehypeScrollToLink>;
topLink?: Partial<RehypeScrollToLink>;
bottomLink?: Partial<RehypeScrollToLink>;
};

export type RehypeScrollToTopOptionsRequired = {
Expand All @@ -36,45 +36,11 @@ const defaultOptions: RehypeScrollToTopOptionsRequired = {
},
};

function mergeOptions(userOptions: Partial<RehypeScrollToTopOptions> = {}): RehypeScrollToTopOptionsRequired {
return {
bottomLink: {
ariaLabel: userOptions.bottomLink?.ariaLabel ?? defaultOptions.bottomLink.ariaLabel,
classes: userOptions.bottomLink?.classes ?? defaultOptions.bottomLink.classes,
disabled: userOptions.bottomLink?.disabled ?? defaultOptions.bottomLink.disabled,
id: userOptions.bottomLink?.id ?? defaultOptions.bottomLink.id,
text: userOptions.bottomLink?.text ?? defaultOptions.bottomLink.text,
},
topLink: {
ariaLabel: userOptions.topLink?.ariaLabel ?? defaultOptions.topLink.ariaLabel,
classes: userOptions.topLink?.classes ?? defaultOptions.topLink.classes,
disabled: userOptions.topLink?.disabled ?? defaultOptions.topLink.disabled,
id: userOptions.topLink?.id ?? defaultOptions.topLink.id,
text: userOptions.topLink?.text ?? defaultOptions.topLink.text,
},
};
}

const baseClass = "scroll-to";
const bottomClass = "scroll-to--bottom";
const topClass = "scroll-to--top";

const createLinkElement = (props: RehypeScrollToLink & { destinationId: string }): Element => {
const { ariaLabel, classes, disabled, id, text, destinationId } = props;
return {
type: "element",
tagName: "a",
properties: {
"aria-label": ariaLabel,
href: `#${destinationId}`,
id,
className: [classes],
},
children: [{ type: "text", value: text }],
};
};

const rehypeScrollToTop: Plugin<[Partial<RehypeScrollToTopOptions>?], Root> = (options = {}) => {
const rehypeScrollToTop: Plugin<[RehypeScrollToTopOptions?], Root> = (options = {}) => {
const opts = mergeOptions(options);

return (tree) => {
Expand Down Expand Up @@ -115,3 +81,46 @@ const rehypeScrollToTop: Plugin<[Partial<RehypeScrollToTopOptions>?], Root> = (o
};

export default rehypeScrollToTop;

function mergeOptions(userOptions: Partial<RehypeScrollToTopOptions> = {}): RehypeScrollToTopOptionsRequired {
return {
bottomLink: {
ariaLabel: userOptions.bottomLink?.ariaLabel ?? defaultOptions.bottomLink.ariaLabel,
classes: userOptions.bottomLink?.classes ?? defaultOptions.bottomLink.classes,
disabled: userOptions.bottomLink?.disabled ?? defaultOptions.bottomLink.disabled,
id: userOptions.bottomLink?.id ?? defaultOptions.bottomLink.id,
text: userOptions.bottomLink?.text ?? defaultOptions.bottomLink.text,
},
topLink: {
ariaLabel: userOptions.topLink?.ariaLabel ?? defaultOptions.topLink.ariaLabel,
classes: userOptions.topLink?.classes ?? defaultOptions.topLink.classes,
disabled: userOptions.topLink?.disabled ?? defaultOptions.topLink.disabled,
id: userOptions.topLink?.id ?? defaultOptions.topLink.id,
text: userOptions.topLink?.text ?? defaultOptions.topLink.text,
},
};
}

function createLinkElement(props: RehypeScrollToLink & { destinationId: string }): Element {
const { ariaLabel, classes, disabled, id, text, destinationId } = props;
const linkElement: Element = {
type: "element",
tagName: "a",
properties: {
"aria-label": ariaLabel,
href: `#${destinationId}`,
id,
className: [classes],
},
children: [{ type: "text", value: text }],
};

return {
type: "element",
tagName: "div",
properties: {
className: ["scroll-to-wrapper"],
},
children: [linkElement],
};
}
4 changes: 2 additions & 2 deletions tests/fixtures/additionalClasses/input.html
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<h1>h1</h1>
<p>test</p>
<h1>Title</h1>
<p>Content</p>
12 changes: 8 additions & 4 deletions tests/fixtures/additionalClasses/output.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
<a aria-label="Scroll to bottom" href="#bottom" id="top" class="foo scroll-to scroll-to--top">Scroll to bottom</a>
<h1>h1</h1>
<p>test</p>
<a aria-label="Scroll to top" href="#top" id="bottom" class="bar scroll-to scroll-to--bottom">Scroll to top</a>
<div class="scroll-to-wrapper">
<a aria-label="Scroll to bottom" href="#bottom" id="top" class="foo scroll-to scroll-to--top">Scroll to bottom</a>
</div>
<h1>Title</h1>
<p>Content</p>
<div class="scroll-to-wrapper">
<a aria-label="Scroll to top" href="#top" id="bottom" class="bar scroll-to scroll-to--bottom">Scroll to top</a>
</div>
8 changes: 6 additions & 2 deletions tests/fixtures/basic/output.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
<a aria-label="Scroll to bottom" href="#bottom" id="top" class="scroll-to scroll-to--top">Scroll to bottom</a>
<div class="scroll-to-wrapper">
<a aria-label="Scroll to bottom" href="#bottom" id="top" class="scroll-to scroll-to--top">Scroll to bottom</a>
</div>
<h1>Title</h1>
<p>Content</p>
<a aria-label="Scroll to top" href="#top" id="bottom" class="scroll-to scroll-to--bottom">Scroll to top</a>
<div class="scroll-to-wrapper">
<a aria-label="Scroll to top" href="#top" id="bottom" class="scroll-to scroll-to--bottom">Scroll to top</a>
</div>
8 changes: 6 additions & 2 deletions tests/fixtures/customAriaLabels/output.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
<a aria-label="Custom aria top" href="#bottom" id="top" class="scroll-to scroll-to--top">Scroll to bottom</a>
<div class="scroll-to-wrapper">
<a aria-label="Custom aria top" href="#bottom" id="top" class="scroll-to scroll-to--top">Scroll to bottom</a>
</div>
<h1>Title</h1>
<p>Content</p>
<a aria-label="Custom aria bottom" href="#top" id="bottom" class="scroll-to scroll-to--bottom">Scroll to top</a>
<div class="scroll-to-wrapper">
<a aria-label="Custom aria bottom" href="#top" id="bottom" class="scroll-to scroll-to--bottom">Scroll to top</a>
</div>
12 changes: 8 additions & 4 deletions tests/fixtures/customIDs/output.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
<a aria-label="Scroll to bottom" href="#custom-bottom" id="custom-top" class="scroll-to scroll-to--top"
<div class="scroll-to-wrapper">
<a aria-label="Scroll to bottom" href="#custom-bottom" id="custom-top" class="scroll-to scroll-to--top"
>Scroll to bottom</a
>
>
</div>
<h1>Title</h1>
<p>Content</p>
<a aria-label="Scroll to top" href="#custom-top" id="custom-bottom" class="scroll-to scroll-to--bottom"
<div class="scroll-to-wrapper">
<a aria-label="Scroll to top" href="#custom-top" id="custom-bottom" class="scroll-to scroll-to--bottom"
>Scroll to top</a
>
>
</div>
8 changes: 6 additions & 2 deletions tests/fixtures/customText/output.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
<a aria-label="Scroll to bottom" href="#bottom" id="top" class="scroll-to scroll-to--top">Custom top text</a>
<div class="scroll-to-wrapper">
<a aria-label="Scroll to bottom" href="#bottom" id="top" class="scroll-to scroll-to--top">Custom top text</a>
</div>
<h1>Title</h1>
<p>Content</p>
<a aria-label="Scroll to top" href="#top" id="bottom" class="scroll-to scroll-to--bottom">Custom bottom text</a>
<div class="scroll-to-wrapper">
<a aria-label="Scroll to top" href="#top" id="bottom" class="scroll-to scroll-to--bottom">Custom bottom text</a>
</div>
4 changes: 3 additions & 1 deletion tests/fixtures/disableBottomLink/output.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<a aria-label="Scroll to bottom" href="#bottom" id="top" class="scroll-to scroll-to--top">Scroll to bottom</a>
<div class="scroll-to-wrapper">
<a aria-label="Scroll to bottom" href="#bottom" id="top" class="scroll-to scroll-to--top">Scroll to bottom</a>
</div>
<h1>Title</h1>
<p>Content</p>
4 changes: 3 additions & 1 deletion tests/fixtures/disableTopLink/output.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<h1>Title</h1>
<p>Content</p>
<a aria-label="Scroll to top" href="#top" id="bottom" class="scroll-to scroll-to--bottom">Scroll to top</a>
<div class="scroll-to-wrapper">
<a aria-label="Scroll to top" href="#top" id="bottom" class="scroll-to scroll-to--bottom">Scroll to top</a>
</div>

0 comments on commit 4e2f1e0

Please sign in to comment.