Upload folder using huggingface_hub
Browse files- 6.0.0/markdown-code/MarkdownCode.svelte +241 -0
- 6.0.0/markdown-code/html-tags.ts +209 -0
- 6.0.0/markdown-code/index.ts +1 -0
- 6.0.0/markdown-code/package.json +42 -0
- 6.0.0/markdown-code/utils.ts +304 -0
6.0.0/markdown-code/MarkdownCode.svelte
ADDED
|
@@ -0,0 +1,241 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<script lang="ts">
|
| 2 |
+
import { afterUpdate, tick, onMount } from "svelte";
|
| 3 |
+
import { create_marked } from "./utils";
|
| 4 |
+
import { sanitize } from "@gradio/sanitize";
|
| 5 |
+
import "./prism.css";
|
| 6 |
+
import { standardHtmlAndSvgTags } from "./html-tags";
|
| 7 |
+
import type { ThemeMode } from "@gradio/core";
|
| 8 |
+
|
| 9 |
+
export let chatbot = true;
|
| 10 |
+
export let message: string;
|
| 11 |
+
export let sanitize_html = true;
|
| 12 |
+
export let latex_delimiters: {
|
| 13 |
+
left: string;
|
| 14 |
+
right: string;
|
| 15 |
+
display: boolean;
|
| 16 |
+
}[] = [];
|
| 17 |
+
export let render_markdown = true;
|
| 18 |
+
export let line_breaks = true;
|
| 19 |
+
export let header_links = false;
|
| 20 |
+
export let allow_tags: string[] | boolean = false;
|
| 21 |
+
export let theme_mode: ThemeMode = "system";
|
| 22 |
+
let el: HTMLSpanElement;
|
| 23 |
+
let html: string;
|
| 24 |
+
let katex_loaded = false;
|
| 25 |
+
|
| 26 |
+
const marked = create_marked({
|
| 27 |
+
header_links,
|
| 28 |
+
line_breaks,
|
| 29 |
+
latex_delimiters: latex_delimiters || []
|
| 30 |
+
});
|
| 31 |
+
|
| 32 |
+
function has_math_syntax(text: string): boolean {
|
| 33 |
+
if (!latex_delimiters || latex_delimiters.length === 0) {
|
| 34 |
+
return false;
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
return latex_delimiters.some(
|
| 38 |
+
(delimiter) =>
|
| 39 |
+
text.includes(delimiter.left) && text.includes(delimiter.right)
|
| 40 |
+
);
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
function escapeRegExp(string: string): string {
|
| 44 |
+
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
function escapeTags(
|
| 48 |
+
content: string,
|
| 49 |
+
tagsToEscape: string[] | boolean
|
| 50 |
+
): string {
|
| 51 |
+
if (tagsToEscape === true) {
|
| 52 |
+
// https://www.w3schools.com/tags/
|
| 53 |
+
const tagRegex = /<\/?([a-zA-Z][a-zA-Z0-9-]*)([\s>])/g;
|
| 54 |
+
return content.replace(tagRegex, (match, tagName, endChar) => {
|
| 55 |
+
if (!standardHtmlAndSvgTags.includes(tagName.toLowerCase())) {
|
| 56 |
+
return match.replace(/</g, "<").replace(/>/g, ">");
|
| 57 |
+
}
|
| 58 |
+
return match;
|
| 59 |
+
});
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
if (Array.isArray(tagsToEscape)) {
|
| 63 |
+
const tagPattern = tagsToEscape.map((tag) => ({
|
| 64 |
+
open: new RegExp(`<(${tag})(\\s+[^>]*)?>`, "gi"),
|
| 65 |
+
close: new RegExp(`</(${tag})>`, "gi")
|
| 66 |
+
}));
|
| 67 |
+
|
| 68 |
+
let result = content;
|
| 69 |
+
|
| 70 |
+
tagPattern.forEach((pattern) => {
|
| 71 |
+
result = result.replace(pattern.open, (match) =>
|
| 72 |
+
match.replace(/</g, "<").replace(/>/g, ">")
|
| 73 |
+
);
|
| 74 |
+
result = result.replace(pattern.close, (match) =>
|
| 75 |
+
match.replace(/</g, "<").replace(/>/g, ">")
|
| 76 |
+
);
|
| 77 |
+
});
|
| 78 |
+
return result;
|
| 79 |
+
}
|
| 80 |
+
return content;
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
function process_message(value: string): string {
|
| 84 |
+
let parsedValue = value;
|
| 85 |
+
if (render_markdown) {
|
| 86 |
+
const latexBlocks: string[] = [];
|
| 87 |
+
latex_delimiters.forEach((delimiter, index) => {
|
| 88 |
+
const leftDelimiter = escapeRegExp(delimiter.left);
|
| 89 |
+
const rightDelimiter = escapeRegExp(delimiter.right);
|
| 90 |
+
const regex = new RegExp(
|
| 91 |
+
`${leftDelimiter}([\\s\\S]+?)${rightDelimiter}`,
|
| 92 |
+
"g"
|
| 93 |
+
);
|
| 94 |
+
parsedValue = parsedValue.replace(regex, (match, p1) => {
|
| 95 |
+
latexBlocks.push(match);
|
| 96 |
+
return `%%%LATEX_BLOCK_${latexBlocks.length - 1}%%%`;
|
| 97 |
+
});
|
| 98 |
+
});
|
| 99 |
+
|
| 100 |
+
parsedValue = marked.parse(parsedValue) as string;
|
| 101 |
+
|
| 102 |
+
parsedValue = parsedValue.replace(
|
| 103 |
+
/%%%LATEX_BLOCK_(\d+)%%%/g,
|
| 104 |
+
(match, p1) => latexBlocks[parseInt(p1, 10)]
|
| 105 |
+
);
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
if (allow_tags) {
|
| 109 |
+
parsedValue = escapeTags(parsedValue, allow_tags);
|
| 110 |
+
}
|
| 111 |
+
|
| 112 |
+
if (sanitize_html && sanitize) {
|
| 113 |
+
parsedValue = sanitize(parsedValue);
|
| 114 |
+
}
|
| 115 |
+
return parsedValue;
|
| 116 |
+
}
|
| 117 |
+
|
| 118 |
+
$: if (message && message.trim()) {
|
| 119 |
+
html = process_message(message);
|
| 120 |
+
} else {
|
| 121 |
+
html = "";
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
async function render_html(value: string): Promise<void> {
|
| 125 |
+
if (latex_delimiters.length > 0 && value && has_math_syntax(value)) {
|
| 126 |
+
if (!katex_loaded) {
|
| 127 |
+
await Promise.all([
|
| 128 |
+
import("katex/dist/katex.min.css"),
|
| 129 |
+
import("katex/contrib/auto-render")
|
| 130 |
+
]).then(([, { default: render_math_in_element }]) => {
|
| 131 |
+
katex_loaded = true;
|
| 132 |
+
render_math_in_element(el, {
|
| 133 |
+
delimiters: latex_delimiters,
|
| 134 |
+
throwOnError: false
|
| 135 |
+
});
|
| 136 |
+
});
|
| 137 |
+
} else {
|
| 138 |
+
const { default: render_math_in_element } = await import(
|
| 139 |
+
"katex/contrib/auto-render"
|
| 140 |
+
);
|
| 141 |
+
render_math_in_element(el, {
|
| 142 |
+
delimiters: latex_delimiters,
|
| 143 |
+
throwOnError: false
|
| 144 |
+
});
|
| 145 |
+
}
|
| 146 |
+
}
|
| 147 |
+
|
| 148 |
+
if (el) {
|
| 149 |
+
const mermaidDivs = el.querySelectorAll(".mermaid");
|
| 150 |
+
if (mermaidDivs.length > 0) {
|
| 151 |
+
await tick();
|
| 152 |
+
const { default: mermaid } = await import("mermaid");
|
| 153 |
+
|
| 154 |
+
mermaid.initialize({
|
| 155 |
+
startOnLoad: false,
|
| 156 |
+
theme: theme_mode === "dark" ? "dark" : "default",
|
| 157 |
+
securityLevel: "antiscript"
|
| 158 |
+
});
|
| 159 |
+
await mermaid.run({
|
| 160 |
+
nodes: Array.from(mermaidDivs).map((node) => node as HTMLElement)
|
| 161 |
+
});
|
| 162 |
+
}
|
| 163 |
+
}
|
| 164 |
+
}
|
| 165 |
+
|
| 166 |
+
afterUpdate(async () => {
|
| 167 |
+
if (el && document.body.contains(el)) {
|
| 168 |
+
await render_html(message);
|
| 169 |
+
} else {
|
| 170 |
+
console.error("Element is not in the DOM");
|
| 171 |
+
}
|
| 172 |
+
});
|
| 173 |
+
</script>
|
| 174 |
+
|
| 175 |
+
<span class:chatbot bind:this={el} class="md" class:prose={render_markdown}>
|
| 176 |
+
{@html html}
|
| 177 |
+
</span>
|
| 178 |
+
|
| 179 |
+
<style>
|
| 180 |
+
span :global(div[class*="code_wrap"]) {
|
| 181 |
+
position: relative;
|
| 182 |
+
}
|
| 183 |
+
|
| 184 |
+
/* KaTeX */
|
| 185 |
+
span :global(span.katex) {
|
| 186 |
+
font-size: var(--text-lg);
|
| 187 |
+
direction: ltr;
|
| 188 |
+
}
|
| 189 |
+
|
| 190 |
+
span :global(div[class*="code_wrap"] > button) {
|
| 191 |
+
z-index: 1;
|
| 192 |
+
cursor: pointer;
|
| 193 |
+
border-bottom-left-radius: var(--radius-sm);
|
| 194 |
+
padding: var(--spacing-md);
|
| 195 |
+
width: 25px;
|
| 196 |
+
height: 25px;
|
| 197 |
+
position: absolute;
|
| 198 |
+
right: 0;
|
| 199 |
+
}
|
| 200 |
+
|
| 201 |
+
span :global(.check) {
|
| 202 |
+
opacity: 0;
|
| 203 |
+
z-index: var(--layer-top);
|
| 204 |
+
transition: opacity 0.2s;
|
| 205 |
+
background: var(--code-background-fill);
|
| 206 |
+
color: var(--body-text-color);
|
| 207 |
+
position: absolute;
|
| 208 |
+
top: var(--size-1-5);
|
| 209 |
+
left: var(--size-1-5);
|
| 210 |
+
}
|
| 211 |
+
|
| 212 |
+
span :global(p:not(:first-child)) {
|
| 213 |
+
margin-top: var(--spacing-xxl);
|
| 214 |
+
}
|
| 215 |
+
|
| 216 |
+
span :global(.md-header-anchor) {
|
| 217 |
+
/* position: absolute; */
|
| 218 |
+
margin-left: -25px;
|
| 219 |
+
padding-right: 8px;
|
| 220 |
+
line-height: 1;
|
| 221 |
+
color: var(--body-text-color-subdued);
|
| 222 |
+
opacity: 0;
|
| 223 |
+
}
|
| 224 |
+
|
| 225 |
+
span :global(h1:hover .md-header-anchor),
|
| 226 |
+
span :global(h2:hover .md-header-anchor),
|
| 227 |
+
span :global(h3:hover .md-header-anchor),
|
| 228 |
+
span :global(h4:hover .md-header-anchor),
|
| 229 |
+
span :global(h5:hover .md-header-anchor),
|
| 230 |
+
span :global(h6:hover .md-header-anchor) {
|
| 231 |
+
opacity: 1;
|
| 232 |
+
}
|
| 233 |
+
|
| 234 |
+
span.md :global(.md-header-anchor > svg) {
|
| 235 |
+
color: var(--body-text-color-subdued);
|
| 236 |
+
}
|
| 237 |
+
|
| 238 |
+
span :global(table) {
|
| 239 |
+
word-break: break-word;
|
| 240 |
+
}
|
| 241 |
+
</style>
|
6.0.0/markdown-code/html-tags.ts
ADDED
|
@@ -0,0 +1,209 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// https://www.w3schools.com/tags/
|
| 2 |
+
export const standardHtmlTags = [
|
| 3 |
+
"!--",
|
| 4 |
+
"!doctype",
|
| 5 |
+
"a",
|
| 6 |
+
"abbr",
|
| 7 |
+
"acronym",
|
| 8 |
+
"address",
|
| 9 |
+
"applet",
|
| 10 |
+
"area",
|
| 11 |
+
"article",
|
| 12 |
+
"aside",
|
| 13 |
+
"audio",
|
| 14 |
+
"b",
|
| 15 |
+
"base",
|
| 16 |
+
"basefont",
|
| 17 |
+
"bdi",
|
| 18 |
+
"bdo",
|
| 19 |
+
"big",
|
| 20 |
+
"blockquote",
|
| 21 |
+
"body",
|
| 22 |
+
"br",
|
| 23 |
+
"button",
|
| 24 |
+
"canvas",
|
| 25 |
+
"caption",
|
| 26 |
+
"center",
|
| 27 |
+
"cite",
|
| 28 |
+
"code",
|
| 29 |
+
"col",
|
| 30 |
+
"colgroup",
|
| 31 |
+
"data",
|
| 32 |
+
"datalist",
|
| 33 |
+
"dd",
|
| 34 |
+
"del",
|
| 35 |
+
"details",
|
| 36 |
+
"dfn",
|
| 37 |
+
"dialog",
|
| 38 |
+
"dir",
|
| 39 |
+
"div",
|
| 40 |
+
"dl",
|
| 41 |
+
"dt",
|
| 42 |
+
"em",
|
| 43 |
+
"embed",
|
| 44 |
+
"fieldset",
|
| 45 |
+
"figcaption",
|
| 46 |
+
"figure",
|
| 47 |
+
"font",
|
| 48 |
+
"footer",
|
| 49 |
+
"form",
|
| 50 |
+
"frame",
|
| 51 |
+
"frameset",
|
| 52 |
+
"h1",
|
| 53 |
+
"h2",
|
| 54 |
+
"h3",
|
| 55 |
+
"h4",
|
| 56 |
+
"h5",
|
| 57 |
+
"h6",
|
| 58 |
+
"head",
|
| 59 |
+
"header",
|
| 60 |
+
"hgroup",
|
| 61 |
+
"hr",
|
| 62 |
+
"html",
|
| 63 |
+
"i",
|
| 64 |
+
"iframe",
|
| 65 |
+
"img",
|
| 66 |
+
"input",
|
| 67 |
+
"ins",
|
| 68 |
+
"kbd",
|
| 69 |
+
"label",
|
| 70 |
+
"legend",
|
| 71 |
+
"li",
|
| 72 |
+
"link",
|
| 73 |
+
"main",
|
| 74 |
+
"map",
|
| 75 |
+
"mark",
|
| 76 |
+
"menu",
|
| 77 |
+
"meta",
|
| 78 |
+
"meter",
|
| 79 |
+
"nav",
|
| 80 |
+
"noframes",
|
| 81 |
+
"noscript",
|
| 82 |
+
"object",
|
| 83 |
+
"ol",
|
| 84 |
+
"optgroup",
|
| 85 |
+
"option",
|
| 86 |
+
"output",
|
| 87 |
+
"p",
|
| 88 |
+
"param",
|
| 89 |
+
"picture",
|
| 90 |
+
"pre",
|
| 91 |
+
"progress",
|
| 92 |
+
"q",
|
| 93 |
+
"rp",
|
| 94 |
+
"rt",
|
| 95 |
+
"ruby",
|
| 96 |
+
"s",
|
| 97 |
+
"samp",
|
| 98 |
+
"script",
|
| 99 |
+
"search",
|
| 100 |
+
"section",
|
| 101 |
+
"select",
|
| 102 |
+
"small",
|
| 103 |
+
"source",
|
| 104 |
+
"span",
|
| 105 |
+
"strike",
|
| 106 |
+
"strong",
|
| 107 |
+
"style",
|
| 108 |
+
"sub",
|
| 109 |
+
"summary",
|
| 110 |
+
"sup",
|
| 111 |
+
"svg",
|
| 112 |
+
"table",
|
| 113 |
+
"tbody",
|
| 114 |
+
"td",
|
| 115 |
+
"template",
|
| 116 |
+
"textarea",
|
| 117 |
+
"tfoot",
|
| 118 |
+
"th",
|
| 119 |
+
"thead",
|
| 120 |
+
"time",
|
| 121 |
+
"title",
|
| 122 |
+
"tr",
|
| 123 |
+
"track",
|
| 124 |
+
"tt",
|
| 125 |
+
"u",
|
| 126 |
+
"ul",
|
| 127 |
+
"var",
|
| 128 |
+
"video",
|
| 129 |
+
"wbr"
|
| 130 |
+
];
|
| 131 |
+
|
| 132 |
+
// SVG tags
|
| 133 |
+
export const svgTags = [
|
| 134 |
+
// Base structural elements
|
| 135 |
+
"g",
|
| 136 |
+
"defs",
|
| 137 |
+
"use",
|
| 138 |
+
"symbol",
|
| 139 |
+
|
| 140 |
+
// Shape elements
|
| 141 |
+
"rect",
|
| 142 |
+
"circle",
|
| 143 |
+
"ellipse",
|
| 144 |
+
"line",
|
| 145 |
+
"polyline",
|
| 146 |
+
"polygon",
|
| 147 |
+
"path",
|
| 148 |
+
"image",
|
| 149 |
+
|
| 150 |
+
// Text elements
|
| 151 |
+
"text",
|
| 152 |
+
"tspan",
|
| 153 |
+
"textPath",
|
| 154 |
+
|
| 155 |
+
// Gradient and effects
|
| 156 |
+
"linearGradient",
|
| 157 |
+
"radialGradient",
|
| 158 |
+
"stop",
|
| 159 |
+
"pattern",
|
| 160 |
+
"clipPath",
|
| 161 |
+
"mask",
|
| 162 |
+
"filter",
|
| 163 |
+
|
| 164 |
+
// Filter effects
|
| 165 |
+
"feBlend",
|
| 166 |
+
"feColorMatrix",
|
| 167 |
+
"feComponentTransfer",
|
| 168 |
+
"feComposite",
|
| 169 |
+
"feConvolveMatrix",
|
| 170 |
+
"feDiffuseLighting",
|
| 171 |
+
"feDisplacementMap",
|
| 172 |
+
"feGaussianBlur",
|
| 173 |
+
"feMerge",
|
| 174 |
+
"feMorphology",
|
| 175 |
+
"feOffset",
|
| 176 |
+
"feSpecularLighting",
|
| 177 |
+
"feTurbulence",
|
| 178 |
+
"feMergeNode",
|
| 179 |
+
"feFuncR",
|
| 180 |
+
"feFuncG",
|
| 181 |
+
"feFuncB",
|
| 182 |
+
"feFuncA",
|
| 183 |
+
"feDistantLight",
|
| 184 |
+
"fePointLight",
|
| 185 |
+
"feSpotLight",
|
| 186 |
+
"feFlood",
|
| 187 |
+
"feTile",
|
| 188 |
+
|
| 189 |
+
// Animation elements
|
| 190 |
+
"animate",
|
| 191 |
+
"animateTransform",
|
| 192 |
+
"animateMotion",
|
| 193 |
+
"mpath",
|
| 194 |
+
"set",
|
| 195 |
+
|
| 196 |
+
// Interactive and other elements
|
| 197 |
+
"view",
|
| 198 |
+
"cursor",
|
| 199 |
+
"foreignObject",
|
| 200 |
+
"desc",
|
| 201 |
+
"title",
|
| 202 |
+
"metadata",
|
| 203 |
+
"switch"
|
| 204 |
+
];
|
| 205 |
+
|
| 206 |
+
export const standardHtmlAndSvgTags = [
|
| 207 |
+
...standardHtmlTags,
|
| 208 |
+
...svgTags.filter((tag) => !standardHtmlTags.includes(tag))
|
| 209 |
+
];
|
6.0.0/markdown-code/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
export { default as MarkdownCode } from "./MarkdownCode.svelte";
|
6.0.0/markdown-code/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "@gradio/markdown-code",
|
| 3 |
+
"version": "0.6.0",
|
| 4 |
+
"description": "Gradio UI packages",
|
| 5 |
+
"type": "module",
|
| 6 |
+
"author": "",
|
| 7 |
+
"license": "ISC",
|
| 8 |
+
"private": false,
|
| 9 |
+
"main_changeset": true,
|
| 10 |
+
"main": "Index.svelte",
|
| 11 |
+
"exports": {
|
| 12 |
+
".": {
|
| 13 |
+
"gradio": "./index.ts",
|
| 14 |
+
"svelte": "./dist/index.js",
|
| 15 |
+
"types": "./dist/index.d.ts"
|
| 16 |
+
},
|
| 17 |
+
"./package.json": "./package.json"
|
| 18 |
+
},
|
| 19 |
+
"dependencies": {
|
| 20 |
+
"@gradio/sanitize": "workspace:^",
|
| 21 |
+
"@types/katex": "^0.16.7",
|
| 22 |
+
"@types/prismjs": "1.26.5",
|
| 23 |
+
"github-slugger": "^2.0.0",
|
| 24 |
+
"katex": "^0.16.23",
|
| 25 |
+
"marked": "^12.0.0",
|
| 26 |
+
"marked-gfm-heading-id": "^3.1.2",
|
| 27 |
+
"marked-highlight": "^2.0.1",
|
| 28 |
+
"mermaid": "^11.12.0",
|
| 29 |
+
"prismjs": "^1.30.0"
|
| 30 |
+
},
|
| 31 |
+
"devDependencies": {
|
| 32 |
+
"@gradio/preview": "workspace:^"
|
| 33 |
+
},
|
| 34 |
+
"peerDependencies": {
|
| 35 |
+
"svelte": "^5.43.4"
|
| 36 |
+
},
|
| 37 |
+
"repository": {
|
| 38 |
+
"type": "git",
|
| 39 |
+
"url": "git+https://github.com/gradio-app/gradio.git",
|
| 40 |
+
"directory": "js/markdown"
|
| 41 |
+
}
|
| 42 |
+
}
|
6.0.0/markdown-code/utils.ts
ADDED
|
@@ -0,0 +1,304 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { type Renderer, Marked } from "marked";
|
| 2 |
+
import { markedHighlight } from "marked-highlight";
|
| 3 |
+
import { gfmHeadingId } from "marked-gfm-heading-id";
|
| 4 |
+
import * as Prism from "prismjs";
|
| 5 |
+
import "prismjs/components/prism-python";
|
| 6 |
+
import "prismjs/components/prism-latex";
|
| 7 |
+
import "prismjs/components/prism-bash";
|
| 8 |
+
// Add additional Prism languages here
|
| 9 |
+
import "prismjs/components/prism-c";
|
| 10 |
+
import "prismjs/components/prism-cpp";
|
| 11 |
+
import "prismjs/components/prism-json";
|
| 12 |
+
import "prismjs/components/prism-sql";
|
| 13 |
+
import "prismjs/components/prism-java";
|
| 14 |
+
import "prismjs/components/prism-go";
|
| 15 |
+
import "prismjs/components/prism-rust";
|
| 16 |
+
import "prismjs/components/prism-php";
|
| 17 |
+
import "prismjs/components/prism-yaml";
|
| 18 |
+
import "prismjs/components/prism-markup-templating";
|
| 19 |
+
import GithubSlugger from "github-slugger";
|
| 20 |
+
|
| 21 |
+
const LINK_ICON_CODE = `<svg class="md-link-icon" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true" fill="currentColor"><path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"></path></svg>`;
|
| 22 |
+
|
| 23 |
+
const COPY_ICON_CODE = `
|
| 24 |
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 15 15" color="currentColor" aria-hidden="true" aria-label="Copy" stroke-width="1.3" width="15" height="15">
|
| 25 |
+
<path fill="currentColor" d="M12.728 4.545v8.182H4.545V4.545zm0 -0.909H4.545a0.909 0.909 0 0 0 -0.909 0.909v8.182a0.909 0.909 0 0 0 0.909 0.909h8.182a0.909 0.909 0 0 0 0.909 -0.909V4.545a0.909 0.909 0 0 0 -0.909 -0.909"/>
|
| 26 |
+
<path fill="currentColor" d="M1.818 8.182H0.909V1.818a0.909 0.909 0 0 1 0.909 -0.909h6.364v0.909H1.818Z"/>
|
| 27 |
+
</svg>
|
| 28 |
+
|
| 29 |
+
`;
|
| 30 |
+
|
| 31 |
+
const CHECK_ICON_CODE = `<svg xmlns="http://www.w3.org/2000/svg" width="17" height="17" viewBox="0 0 17 17" aria-hidden="true" aria-label="Copied" fill="none" stroke="currentColor" stroke-width="1.3">
|
| 32 |
+
<path d="m13.813 4.781 -7.438 7.438 -3.188 -3.188"/>
|
| 33 |
+
</svg>
|
| 34 |
+
`;
|
| 35 |
+
|
| 36 |
+
const COPY_BUTTON_CODE = `<button title="copy" class="copy_code_button">
|
| 37 |
+
<span class="copy-text">${COPY_ICON_CODE}</span>
|
| 38 |
+
<span class="check">${CHECK_ICON_CODE}</span>
|
| 39 |
+
</button>`;
|
| 40 |
+
|
| 41 |
+
const escape_test = /[&<>"']/;
|
| 42 |
+
const escape_replace = new RegExp(escape_test.source, "g");
|
| 43 |
+
const escape_test_no_encode =
|
| 44 |
+
/[<>"']|&(?!(#\d{1,7}|#[Xx][a-fA-F0-9]{1,6}|\w+);)/;
|
| 45 |
+
const escape_replace_no_encode = new RegExp(escape_test_no_encode.source, "g");
|
| 46 |
+
const escape_replacements: Record<string, any> = {
|
| 47 |
+
"&": "&",
|
| 48 |
+
"<": "<",
|
| 49 |
+
">": ">",
|
| 50 |
+
'"': """,
|
| 51 |
+
"'": "'"
|
| 52 |
+
};
|
| 53 |
+
|
| 54 |
+
const get_escape_replacement = (ch: string): string =>
|
| 55 |
+
escape_replacements[ch] || "";
|
| 56 |
+
|
| 57 |
+
function escape(html: string, encode?: boolean): string {
|
| 58 |
+
if (encode) {
|
| 59 |
+
if (escape_test.test(html)) {
|
| 60 |
+
return html.replace(escape_replace, get_escape_replacement);
|
| 61 |
+
}
|
| 62 |
+
} else {
|
| 63 |
+
if (escape_test_no_encode.test(html)) {
|
| 64 |
+
return html.replace(escape_replace_no_encode, get_escape_replacement);
|
| 65 |
+
}
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
return html;
|
| 69 |
+
}
|
| 70 |
+
interface Tokenizer {
|
| 71 |
+
name: string;
|
| 72 |
+
level: string;
|
| 73 |
+
start: (src: string) => number | undefined;
|
| 74 |
+
tokenizer: (src: string, tokens: any) => any;
|
| 75 |
+
renderer: (token: any) => string;
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
function createLatexTokenizer(
|
| 79 |
+
delimiters: { left: string; right: string; display: boolean }[]
|
| 80 |
+
): Tokenizer {
|
| 81 |
+
const delimiterPatterns = delimiters.map((delimiter) => ({
|
| 82 |
+
start: new RegExp(delimiter.left.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&")),
|
| 83 |
+
end: new RegExp(delimiter.right.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&"))
|
| 84 |
+
}));
|
| 85 |
+
|
| 86 |
+
return {
|
| 87 |
+
name: "latex",
|
| 88 |
+
level: "block",
|
| 89 |
+
start(src: string) {
|
| 90 |
+
for (const pattern of delimiterPatterns) {
|
| 91 |
+
const match = src.match(pattern.start);
|
| 92 |
+
if (match) {
|
| 93 |
+
return match.index;
|
| 94 |
+
}
|
| 95 |
+
}
|
| 96 |
+
return -1;
|
| 97 |
+
},
|
| 98 |
+
tokenizer(src: string, tokens: any) {
|
| 99 |
+
for (const pattern of delimiterPatterns) {
|
| 100 |
+
const match = new RegExp(
|
| 101 |
+
`${pattern.start.source}([\\s\\S]+?)${pattern.end.source}`
|
| 102 |
+
).exec(src);
|
| 103 |
+
if (match) {
|
| 104 |
+
return {
|
| 105 |
+
type: "latex",
|
| 106 |
+
raw: match[0],
|
| 107 |
+
text: match[1].trim()
|
| 108 |
+
};
|
| 109 |
+
}
|
| 110 |
+
}
|
| 111 |
+
},
|
| 112 |
+
renderer(token: any) {
|
| 113 |
+
return `<div class="latex-block">${token.text}</div>`;
|
| 114 |
+
}
|
| 115 |
+
};
|
| 116 |
+
}
|
| 117 |
+
|
| 118 |
+
function createMermaidTokenizer(): Tokenizer {
|
| 119 |
+
return {
|
| 120 |
+
name: "mermaid",
|
| 121 |
+
level: "block",
|
| 122 |
+
start(src) {
|
| 123 |
+
return src.match(/^```mermaid\s*\n/)?.index;
|
| 124 |
+
},
|
| 125 |
+
tokenizer(src) {
|
| 126 |
+
const match = /^```mermaid\s*\n([\s\S]*?)```\s*(?:\n|$)/.exec(src);
|
| 127 |
+
if (match) {
|
| 128 |
+
return {
|
| 129 |
+
type: "mermaid",
|
| 130 |
+
raw: match[0],
|
| 131 |
+
text: match[1].trim()
|
| 132 |
+
};
|
| 133 |
+
}
|
| 134 |
+
return undefined;
|
| 135 |
+
},
|
| 136 |
+
renderer(token) {
|
| 137 |
+
return `<div class="mermaid">${token.text}</div>\n`;
|
| 138 |
+
}
|
| 139 |
+
};
|
| 140 |
+
}
|
| 141 |
+
|
| 142 |
+
const renderer: Partial<Omit<Renderer, "constructor" | "options">> = {
|
| 143 |
+
code(
|
| 144 |
+
this: Renderer,
|
| 145 |
+
code: string,
|
| 146 |
+
infostring: string | undefined,
|
| 147 |
+
escaped: boolean
|
| 148 |
+
) {
|
| 149 |
+
const lang = (infostring ?? "").match(/\S*/)?.[0] ?? "";
|
| 150 |
+
code = code.replace(/\n$/, "") + "\n";
|
| 151 |
+
|
| 152 |
+
if (!lang || lang === "mermaid") {
|
| 153 |
+
// We include lang === "mermaid" to handle mermaid blocks that don't match our custom tokenizer
|
| 154 |
+
// (i.e., those without closing ```). This handles mermaid blocks that have started streaming
|
| 155 |
+
// but haven't finished yet.
|
| 156 |
+
return (
|
| 157 |
+
'<div class="code_wrap">' +
|
| 158 |
+
COPY_BUTTON_CODE +
|
| 159 |
+
"<pre><code>" +
|
| 160 |
+
(escaped ? code : escape(code, true)) +
|
| 161 |
+
"</code></pre></div>\n"
|
| 162 |
+
);
|
| 163 |
+
}
|
| 164 |
+
return (
|
| 165 |
+
'<div class="code_wrap">' +
|
| 166 |
+
COPY_BUTTON_CODE +
|
| 167 |
+
'<pre><code class="' +
|
| 168 |
+
"language-" +
|
| 169 |
+
escape(lang) +
|
| 170 |
+
'">' +
|
| 171 |
+
(escaped ? code : escape(code, true)) +
|
| 172 |
+
"</code></pre></div>\n"
|
| 173 |
+
);
|
| 174 |
+
}
|
| 175 |
+
};
|
| 176 |
+
|
| 177 |
+
const slugger = new GithubSlugger();
|
| 178 |
+
|
| 179 |
+
export function create_marked({
|
| 180 |
+
header_links,
|
| 181 |
+
line_breaks,
|
| 182 |
+
latex_delimiters
|
| 183 |
+
}: {
|
| 184 |
+
header_links: boolean;
|
| 185 |
+
line_breaks: boolean;
|
| 186 |
+
latex_delimiters: { left: string; right: string; display: boolean }[];
|
| 187 |
+
}): typeof marked {
|
| 188 |
+
const marked = new Marked();
|
| 189 |
+
marked.use(
|
| 190 |
+
{
|
| 191 |
+
gfm: true,
|
| 192 |
+
pedantic: false,
|
| 193 |
+
breaks: line_breaks
|
| 194 |
+
},
|
| 195 |
+
markedHighlight({
|
| 196 |
+
highlight: (code: string, lang: string) => {
|
| 197 |
+
if (Prism?.languages?.[lang]) {
|
| 198 |
+
return Prism.highlight(code, Prism.languages[lang], lang);
|
| 199 |
+
}
|
| 200 |
+
return code;
|
| 201 |
+
}
|
| 202 |
+
}),
|
| 203 |
+
{ renderer }
|
| 204 |
+
);
|
| 205 |
+
|
| 206 |
+
if (header_links) {
|
| 207 |
+
marked.use(gfmHeadingId());
|
| 208 |
+
marked.use({
|
| 209 |
+
extensions: [
|
| 210 |
+
{
|
| 211 |
+
name: "heading",
|
| 212 |
+
level: "block",
|
| 213 |
+
renderer(token) {
|
| 214 |
+
const raw = token.raw
|
| 215 |
+
.toLowerCase()
|
| 216 |
+
.trim()
|
| 217 |
+
.replace(/<[!\/a-z].*?>/gi, "");
|
| 218 |
+
const id = "h" + slugger.slug(raw);
|
| 219 |
+
const level = token.depth;
|
| 220 |
+
const text = this.parser.parseInline(token.tokens!);
|
| 221 |
+
|
| 222 |
+
return `<h${level} id="${id}"><a class="md-header-anchor" href="#${id}">${LINK_ICON_CODE}</a>${text}</h${level}>\n`;
|
| 223 |
+
}
|
| 224 |
+
}
|
| 225 |
+
]
|
| 226 |
+
});
|
| 227 |
+
}
|
| 228 |
+
|
| 229 |
+
const mermaidTokenizer = createMermaidTokenizer();
|
| 230 |
+
const latexTokenizer = createLatexTokenizer(latex_delimiters);
|
| 231 |
+
|
| 232 |
+
marked.use({
|
| 233 |
+
extensions: [mermaidTokenizer, latexTokenizer]
|
| 234 |
+
});
|
| 235 |
+
|
| 236 |
+
return marked;
|
| 237 |
+
}
|
| 238 |
+
|
| 239 |
+
export function copy(node: HTMLDivElement): any {
|
| 240 |
+
node.addEventListener("click", handle_copy);
|
| 241 |
+
|
| 242 |
+
async function handle_copy(event: MouseEvent): Promise<void> {
|
| 243 |
+
const path = event.composedPath() as HTMLButtonElement[];
|
| 244 |
+
|
| 245 |
+
const [copy_button] = path.filter(
|
| 246 |
+
(e) => e?.tagName === "BUTTON" && e.classList.contains("copy_code_button")
|
| 247 |
+
);
|
| 248 |
+
|
| 249 |
+
if (copy_button) {
|
| 250 |
+
event.stopImmediatePropagation();
|
| 251 |
+
|
| 252 |
+
const copy_text = copy_button.parentElement!.innerText.trim();
|
| 253 |
+
const copy_sucess_button = Array.from(
|
| 254 |
+
copy_button.children
|
| 255 |
+
)[1] as HTMLDivElement;
|
| 256 |
+
|
| 257 |
+
const copied = await copy_to_clipboard(copy_text);
|
| 258 |
+
|
| 259 |
+
if (copied) copy_feedback(copy_sucess_button);
|
| 260 |
+
|
| 261 |
+
function copy_feedback(_copy_sucess_button: HTMLDivElement): void {
|
| 262 |
+
_copy_sucess_button.style.opacity = "1";
|
| 263 |
+
setTimeout(() => {
|
| 264 |
+
_copy_sucess_button.style.opacity = "0";
|
| 265 |
+
}, 2000);
|
| 266 |
+
}
|
| 267 |
+
}
|
| 268 |
+
}
|
| 269 |
+
|
| 270 |
+
return {
|
| 271 |
+
destroy(): void {
|
| 272 |
+
node.removeEventListener("click", handle_copy);
|
| 273 |
+
}
|
| 274 |
+
};
|
| 275 |
+
}
|
| 276 |
+
|
| 277 |
+
async function copy_to_clipboard(value: string): Promise<boolean> {
|
| 278 |
+
let copied = false;
|
| 279 |
+
if ("clipboard" in navigator) {
|
| 280 |
+
await navigator.clipboard.writeText(value);
|
| 281 |
+
copied = true;
|
| 282 |
+
} else {
|
| 283 |
+
const textArea = document.createElement("textarea");
|
| 284 |
+
textArea.value = value;
|
| 285 |
+
|
| 286 |
+
textArea.style.position = "absolute";
|
| 287 |
+
textArea.style.left = "-999999px";
|
| 288 |
+
|
| 289 |
+
document.body.prepend(textArea);
|
| 290 |
+
textArea.select();
|
| 291 |
+
|
| 292 |
+
try {
|
| 293 |
+
document.execCommand("copy");
|
| 294 |
+
copied = true;
|
| 295 |
+
} catch (error) {
|
| 296 |
+
console.error(error);
|
| 297 |
+
copied = false;
|
| 298 |
+
} finally {
|
| 299 |
+
textArea.remove();
|
| 300 |
+
}
|
| 301 |
+
}
|
| 302 |
+
|
| 303 |
+
return copied;
|
| 304 |
+
}
|