-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexamples.js
More file actions
58 lines (49 loc) · 1.88 KB
/
examples.js
File metadata and controls
58 lines (49 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import hljs from './vendor/highlight.js/core.min.js';
import sql from './vendor/highlight.js/sql.min.js';
import xml from './vendor/highlight.js/xml.min.js';
import('./vendor/highlight.js/github-dark.min.css', { with: { type: 'css' } })
.then((exports) => {
document.adoptedStyleSheets = [exports.default];
}).catch((_) => {
const link = document.createElement('link');
link.rel = 'stylesheet';
link.href = './vendor/highlight.js/github-dark.min.css';
document.head.appendChild(link);
});
hljs.registerLanguage('sql', sql);
hljs.registerLanguage('xml', xml);
function addExample(element, example) {
const placeholder = "__PLACEHOLDER__";
const query = element.getAttribute("query");
const highlightedQuery = hljs.highlight(query, {
language: "sql",
}).value;
const textElement = element.outerHTML.replace(
`query="${query}"`,
placeholder
);
const highlightedElement = hljs.highlight(textElement, {
language: "html",
}).value;
const quote = '<span class="hljs-string">"</span>';
const codeElement = document.createElement("code");
codeElement.innerHTML = highlightedElement.replace(
`<span class="hljs-attr">${placeholder}</span>`,
`<span class="hljs-attr">query</span>=${quote}${highlightedQuery}${quote}`
);
example.appendChild(codeElement);
example.removeAttribute("class");
}
document.querySelectorAll("sql-table, sql-value").forEach((el) => {
if (
el.previousElementSibling &&
el.previousElementSibling.className === "example-placeholder"
) {
addExample(el, el.previousElementSibling);
} else if (
el.parentElement.previousElementSibling &&
el.parentElement.previousElementSibling.className === "example-placeholder"
) {
addExample(el, el.parentElement.previousElementSibling)
}
});