-
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add copy button to code blocks, add setup page (#990)
- Loading branch information
1 parent
c412501
commit 1195c09
Showing
29 changed files
with
288 additions
and
129 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
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
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 5 additions & 6 deletions
11
ark/docs/src/components/TsBlock.astro → ark/docs/src/components/Code.astro
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,19 +1,18 @@ | ||
--- | ||
import { arkHighlight } from "./highlight.ts" | ||
import { arkHighlight, type HighlightArgs, type BuiltinLang } from "./highlight.ts" | ||
// ideally we could just import { Code } from "astro:components" instead of | ||
// using this custom component, but as of now, the `Code` component imported from | ||
// `astro:components` does not apply our shikiConfig. | ||
// If it does in the future, this component can be deleted. | ||
interface Props { | ||
interface Props extends HighlightArgs { | ||
code: string | ||
lang?: BuiltinLang | ||
} | ||
const { code } = Astro.props | ||
const html = await arkHighlight(code) | ||
const html = await arkHighlight(Astro.props) | ||
--- | ||
|
||
<Fragment set:html={html} /> | ||
<Fragment set:html={html} /> |
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,45 @@ | ||
const hoverSelector = ".twoslash-popup-code" | ||
const errorSelector = ".twoslash-error-line" | ||
const completionSelector = ".twoslash-completion-cursor" | ||
const metaSelector = `${hoverSelector}, ${errorSelector}, ${completionSelector}` | ||
|
||
const distillTwoslashCode = container => { | ||
const walker = document.createTreeWalker(container, NodeFilter.SHOW_TEXT) | ||
|
||
let src = "" | ||
while (walker.nextNode()) { | ||
/** @type { Element } */ | ||
const parentNode = walker.currentNode.parentNode | ||
if (parentNode.closest(errorSelector)) { | ||
// if a twoslash error was rendered in this position, we need an additional newline | ||
src += "\n" | ||
} | ||
if (!parentNode?.closest(metaSelector)) { | ||
// if the node is not a meta node (hover, error or completion) add its textContent | ||
src += walker.currentNode.textContent ?? "" | ||
} | ||
} | ||
|
||
return src.trim() | ||
} | ||
|
||
globalThis.addCopyButtonListeners = () => { | ||
document.querySelectorAll(".code-container").forEach(codeContainer => { | ||
const copyButton = codeContainer.querySelector(".copy-button") | ||
const icon = codeContainer.querySelector(".copy-icon") | ||
|
||
copyButton.addEventListener("click", async () => { | ||
const textToCopy = distillTwoslashCode(codeContainer) | ||
await navigator.clipboard.writeText(textToCopy) | ||
|
||
icon.setAttribute("src", "/src/assets/check.svg") | ||
copyButton.setAttribute("disabled", "1") | ||
copyButton.setAttribute("style", "opacity: .6;") | ||
setTimeout(() => { | ||
icon.setAttribute("src", "/src/assets/copy.svg") | ||
copyButton.removeAttribute("disabled") | ||
copyButton.removeAttribute("style") | ||
}, 2000) | ||
}) | ||
}) | ||
} |
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,18 +1,25 @@ | ||
import arkdarkColors from "arkdark/color-theme.json" | ||
import arktypeTextmate from "arkdark/tsWithArkType.tmLanguage.json" | ||
import { getHighlighter } from "shiki" | ||
import { twoslash } from "./shiki.config.js" | ||
import { addCopyButton, twoslash } from "./shiki.config.js" | ||
|
||
let highlighter: Awaited<ReturnType<typeof getHighlighter>> | undefined | ||
|
||
export const arkHighlight = async (code: string) => { | ||
export type BuiltinLang = "ts" | "bash" | "jsonc" | ||
|
||
export type HighlightArgs = { | ||
code: string | ||
lang?: BuiltinLang | ||
} | ||
|
||
export const arkHighlight = async (args: HighlightArgs) => { | ||
highlighter ??= await getHighlighter({ | ||
themes: [arkdarkColors], | ||
langs: [{ ...arktypeTextmate, name: "ts" } as never] | ||
langs: [{ ...arktypeTextmate, name: "ts" } as never, "bash", "jsonc"] | ||
}) | ||
return highlighter.codeToHtml(code, { | ||
lang: "ts", | ||
return highlighter.codeToHtml(args.code, { | ||
lang: args.lang ?? "ts", | ||
theme: "ArkDark", | ||
transformers: [twoslash] | ||
transformers: [twoslash, addCopyButton] | ||
}) | ||
} |
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
File renamed without changes.
2 changes: 2 additions & 0 deletions
2
.../content/docs/clearAndConcise.twoslash.ts → ...tent/docs/clarityAndConcision.twoslash.js
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
19 changes: 19 additions & 0 deletions
19
ark/docs/src/content/docs/deepIntrospectability.twoslash.js
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,19 @@ | ||
import { type } from "arktype" | ||
|
||
const user = type({ | ||
name: "string", | ||
luckyNumbers: "(number | bigint)[]", | ||
"isAdmin?": "boolean | null" | ||
}) | ||
|
||
// ---cut--- | ||
user.extends("object") // true | ||
user.extends("string") // false | ||
// true (number | bigint is narrower than unknown) | ||
user.extends({ | ||
luckyNumbers: "unknown[]" | ||
}) | ||
// false (number | bigint is wider than number) | ||
user.extends({ | ||
luckyNumbers: "number[]" | ||
}) |
26 changes: 0 additions & 26 deletions
26
ark/docs/src/content/docs/deepIntrospectability.twoslash.ts
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.