-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make query param switch a generic util
- Loading branch information
1 parent
e80bd7c
commit 8d4cfa0
Showing
4 changed files
with
46 additions
and
15 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,32 @@ | ||
/** | ||
* Show certain elements only when certain query parameters are available. This | ||
* function takes an array of queryKey to element selector mappings and shows | ||
* the element corresponding to the first queryKey that is present in the URL. | ||
* | ||
* They are mutually exclusive, so only one element will be shown at a time. | ||
* | ||
* A key of empty string will represent the default case, which will be shown | ||
* when no other query parameters are present. | ||
* | ||
* @param cases - A mapping of query parameter values to element selectors. | ||
*/ | ||
export function initializeSwitch( | ||
cases: Array<[queryKey: string, selector: string]>, | ||
): void { | ||
const searchParams = new URLSearchParams(location.search) | ||
const queryKey = | ||
cases.map(([key]) => key).find(key => searchParams.has(key)) || '' | ||
|
||
for (const [key, selector] of cases) { | ||
const elements = document.querySelectorAll<HTMLElement>(selector) | ||
for (const element of elements) { | ||
if (key === queryKey) { | ||
element.removeAttribute('hidden') | ||
element.style.display = 'block' | ||
} else { | ||
element.setAttribute('hidden', '') | ||
element.style.display = 'none' | ||
} | ||
} | ||
} | ||
} |
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,5 @@ | ||
import { marked } from 'marked' | ||
|
||
export function toHTML(markdown: string): string { | ||
return marked(markdown, { async: false }) | ||
} |