-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tooltips when pointing at an ingredient in the instructions.
The tooltips repeat the quantity and preparation from the main ingredient list.
- Loading branch information
Showing
9 changed files
with
133 additions
and
11 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,40 @@ | ||
import slugify from '@lib/slugify'; | ||
import '@shoelace-style/shoelace/dist/components/tooltip/tooltip.js'; | ||
import { LitElement, css, html } from 'lit'; | ||
import { customElement, property, state } from 'lit/decorators.js'; | ||
|
||
@customElement('recipe-ingredient') | ||
export class RecipeIngredient extends LitElement { | ||
static override styles = css` | ||
span { | ||
background-color: var(--yellow-highlight); | ||
outline: solid thin var(--yellow-outline); | ||
border-radius: .5ex; | ||
} | ||
span:focus-visible { | ||
outline: solid var(--yellow-outline-focused); | ||
} | ||
`; | ||
|
||
@property({ attribute: 'ingredient-id' }) | ||
ingredientId: string | undefined = undefined; | ||
|
||
@state() | ||
private fullIngredient: string = ""; | ||
|
||
override connectedCallback() { | ||
super.connectedCallback() | ||
if (this.ingredientId === undefined && this.textContent) { | ||
this.ingredientId = slugify(this.textContent); | ||
} | ||
this.fullIngredient = document.getElementById(this.ingredientId ?? "")?.innerText ?? ""; | ||
} | ||
|
||
override render() { | ||
if (this.fullIngredient === "") { | ||
return html`<slot></slot>`; | ||
} else { | ||
return html`<sl-tooltip content=${this.fullIngredient}><span tabindex=0><slot></slot></span></sl-tooltip>`; | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,13 +1,44 @@ | ||
|
||
import rehypeSanitize from 'rehype-sanitize'; | ||
import type { PhrasingContent, Root } from 'mdast'; | ||
import { findAndReplace } from 'mdast-util-find-and-replace'; | ||
import rehypeSanitize, { defaultSchema } from 'rehype-sanitize'; | ||
import rehypeStringify from 'rehype-stringify'; | ||
import remarkParse from 'remark-parse'; | ||
import remarkRehype from 'remark-rehype'; | ||
import { unified } from 'unified'; | ||
import { unified, type Plugin } from 'unified'; | ||
|
||
const wrapIngredients: Plugin<[], Root> = function () { | ||
function replace(value: string): PhrasingContent { | ||
return { | ||
type: 'text', | ||
value, | ||
data: { | ||
hName: 'recipe-ingredient', | ||
hChildren: [{ type: 'text', value }], | ||
} | ||
}; | ||
} | ||
return (tree, _file) => { | ||
const ingredientNames = this.data('ingredientNames'); | ||
if (ingredientNames) { | ||
findAndReplace(tree, ingredientNames.map(ingredient => [ingredient, replace])); | ||
} | ||
}; | ||
}; | ||
|
||
declare module 'unified' { | ||
interface Data { | ||
ingredientNames?: Array<string> | undefined | ||
} | ||
} | ||
|
||
export const md = unified() | ||
.use(remarkParse) | ||
.use(wrapIngredients) | ||
.use(remarkRehype) | ||
.use(rehypeSanitize) | ||
.use(rehypeSanitize, { | ||
...defaultSchema, | ||
tagNames: [...defaultSchema.tagNames ?? [], 'recipe-ingredient'], | ||
}) | ||
.use(rehypeStringify) | ||
.freeze(); |
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