-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
96 lines (88 loc) · 2.96 KB
/
script.js
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
Prism.languages.cedar = {
comment: {
pattern: /(^|[^\\:])\/\/.*/,
lookbehind: true,
greedy: true,
},
string: {
pattern: /(^|[^\\])"(?:\\.|[^\\"\r\n])*"(?!\s*:)/,
lookbehind: true,
greedy: true,
},
keyword: /\b(?<!\.)(?:permit|forbid|when|unless)\b/,
// don't worry about excluding . before boolean reserved identifiers
boolean: /\b(?:false|true)\b/,
symbol: /\?(?:principal|resource)\b/,
variable: /\b(?<![\.\?])(?:principal|action|resource|context)\b/,
number: /\b0|\-?[1-9](_?[0-9])*/,
operator: [
{
pattern: /(?:&&|\|\||==|!=|>=|<=|>|<|\+|-|\*)/,
},
{
// don't worry about excluding . before operator reserved identifiers
pattern: /\b(?:in|like|has|if|then|else|is)\b/,
},
],
"class-name": [
{
pattern: /\b(?:([_a-zA-Z][_a-zA-Z0-9]*::)*[_a-zA-Z][_a-zA-Z0-9]*)(?=::)/, // (?=::")
},
{
pattern: /(\s+is\s+)([_a-zA-Z][_a-zA-Z0-9]*::)*[_a-zA-Z][_a-zA-Z0-9]*/,
greedy: true, // since "is" is defined above as operator
lookbehind: true,
},
],
builtin: /\b(?:ip|decimal)(?=\()/,
function: [
{
// methods
pattern: /(?=.)(contains|containsAll|containsAny)(?=\()/,
},
{
// decimal methods
pattern:
/(?=.)(lessThan|lessThanOrEqual|greaterThan|greaterThanOrEqual)(?=\()/,
},
{
// ip methods
pattern: /(?=.)(isIpv4|isIpv6|isLoopback|isMulticast|isInRange)(?=\()/,
greedy: true,
},
],
punctuation: /(?:[\(|)|\[|\]|{|}|,|;])/,
};
// This function runs in the background to highlight cedar code blocks using this custom language spec which was sourced from
// https://github.com/cedar-policy/prism-cedar
(function () {
function highlightCedarCode() {
// Find all code blocks with the class 'language-js'
// this is used to mostly avoid a flicker from white to highlighted
// the js is close, and is run before this custom code runs
// ideally we could add the language to mintlify statically
const jsCodeBlocks = document.querySelectorAll("code.language-js");
jsCodeBlocks.forEach((block) => {
// Check if the block has already been highlighted
if (!block.hasAttribute("data-highlighted")) {
// Change the language class to 'language-cedar'
block.className = block.className.replace(
"language-js",
"language-cedar"
);
// Highlight the block
Prism.highlightElement(block);
// Mark the block as highlighted
block.setAttribute("data-highlighted", "true");
}
});
}
// Listen for changes in the URL caused by the browser's back/forward buttons
window.addEventListener("popstate", highlightCedarCode);
// Listen for changes in the URL hash (if applicable)
window.addEventListener("hashchange", highlightCedarCode);
// Check the URL periodically if the app updates the path dynamically
setInterval(highlightCedarCode, 250);
// Run on initial load as soon as possible
highlightCedarCode();
})();