github.com/GoogleContainerTools/skaffold/v2@v2.13.2/docs-v1/content/en/docs/references/yaml/main.js (about) 1 import { html, render } from 'https://unpkg.com/lit-html@1.1.2/lit-html.js'; 2 import { unsafeHTML } from 'https://unpkg.com/lit-html@1.1.2/directives/unsafe-html.js'; 3 4 var version; 5 let latest; 6 (async function() { 7 const versionParam = "?version="; 8 const index = window.location.href.indexOf(versionParam); 9 const table = document.getElementById('table'); 10 11 latest = table.attributes['latest'].value.trim(); 12 if (index === -1) { 13 version = table.attributes['data-version'].value.trim(); 14 version = version.replace('skaffold/', ''); 15 } else { 16 version = window.location.href.substr(index + versionParam.length); 17 table.attributes['data-version'].value = 'skaffold/' + version; 18 } 19 20 const response = await fetch(`/schemas/${version}.json`); 21 const json = await response.json(); 22 23 render(html` 24 ${template(json.definitions, undefined, json.anyOf[0].$ref, 0, "")} 25 `, table); 26 27 if (location.hash) { 28 table.querySelector(location.hash).scrollIntoView(); 29 } 30 })(); 31 32 function* template(definitions, parentDefinition, ref, ident, parent) { 33 const name = ref.replace('#/definitions/', ''); 34 const allProperties = []; 35 const seen = {}; 36 37 const properties = definitions[name].properties; 38 for (const key of (definitions[name].preferredOrder || [])) { 39 allProperties.push([key, properties[key]]); 40 seen[key] = true; 41 } 42 43 const anyOfs = definitions[name].anyOf; 44 for (const anyOf of (anyOfs || [])) { 45 for (const key of (anyOf.preferredOrder || [])) { 46 if (seen[key]) continue; 47 48 allProperties.push([key, anyOf.properties[key]]); 49 seen[key] = true; 50 } 51 } 52 53 let index = -1; 54 for (let [key, definition] of allProperties) { 55 const path = parent.length == 0 ? key : `${parent}-${key}`; 56 index++; 57 58 // Key 59 let required = definitions[name].required && definitions[name].required.includes(key); 60 let keyClass = required ? 'key required' : 'key'; 61 62 // Value 63 let value = definition.default; 64 if (key === 'apiVersion') { 65 value = `skaffold/${version}`; 66 } else if (definition.examples && definition.examples.length > 0) { 67 value = definition.examples[0]; 68 } 69 let valueClass = definition.examples ? 'example' : 'value'; 70 71 // Description 72 let desc = definition['x-intellij-html-description']; 73 if (!desc) { 74 desc = "" 75 } 76 77 // Don't duplicate definitions of top level sections such as build, test, deploy and portForward. 78 if ((name === 'Profile') && definitions['SkaffoldConfig'].properties[key]) { 79 value = '{}'; 80 yield html` 81 <tr> 82 <td> 83 <span class="${keyClass}" style="margin-left: ${ident * 20}px">${anchor(path, key)}:</span> 84 <span class="${valueClass}">${value}</span> 85 </td> 86 <td><span class="comment"># </span></td> 87 <td><span class="comment">${unsafeHTML(desc)}</span></td> 88 </tr> 89 `; 90 continue; 91 } 92 93 if (definition.$ref) { 94 // Check if the referenced description is a final one 95 const refName = definition.$ref.replace('#/definitions/', ''); 96 if (desc === "" && definitions[refName]['x-intellij-html-description']) { 97 desc = definitions[refName]['x-intellij-html-description']; 98 } 99 if (!definitions[refName].properties && !definitions[refName].anyOf) { 100 value = '{}'; 101 } 102 103 yield html` 104 <tr class="top"> 105 <td> 106 <span class="${keyClass}" style="margin-left: ${ident * 20}px">${anchor(path, key)}:</span> 107 <span class="${valueClass}">${value}</span> 108 </td> 109 <td class="comment"># </td> 110 <td class="comment">${unsafeHTML(desc)}</td> 111 </tr> 112 `; 113 } else if (definition.items && definition.items.$ref) { 114 const refName = definition.items.$ref.replace('#/definitions/', ''); 115 if (desc === "" && definitions[refName]['x-intellij-html-description']) { 116 desc = definitions[refName]['x-intellij-html-description']; 117 } 118 yield html` 119 <tr class="top"> 120 <td> 121 <span class="${keyClass}" style="margin-left: ${ident * 20}px">${anchor(path, key)}:</span> 122 <span class="${valueClass}">${value}</span> 123 </td> 124 <td class="comment"># </td> 125 <td class="comment">${unsafeHTML(desc)}</td> 126 </tr> 127 `; 128 } else if (parentDefinition && (parentDefinition.type === 'array') && (index === 0)) { 129 yield html` 130 <tr> 131 <td> 132 <span class="${keyClass}" style="margin-left: ${(ident - 1) * 20}px">- ${anchor(path, key)}:</span> 133 <span class="${valueClass}">${value}</span> 134 </td> 135 <td class="comment"># </td> 136 <td class="comment">${unsafeHTML(desc)}</td> 137 </tr> 138 `; 139 } else if ((definition.type === 'array') && value && (value !== '[]')) { 140 // Parse value to json array 141 const values = JSON.parse(value); 142 143 yield html` 144 <tr> 145 <td> 146 <span class="${keyClass}" style="margin-left: ${ident * 20}px">${anchor(path, key)}:</span> 147 </td> 148 <td class="comment"># </td> 149 <td class="comment" rowspan="${1 + values.length}"> 150 ${unsafeHTML(desc)} 151 </td> 152 </tr> 153 `; 154 155 for (const v of values) { 156 yield html` 157 <tr> 158 <td> 159 <span class="key" style="margin-left: ${ident * 20}px">- <span class="${valueClass}">${JSON.stringify(v)}</span></span> 160 </td> 161 <td class="comment"># </td> 162 </tr> 163 `; 164 } 165 } else if (definition.type === 'object' && value && value !== '{}') { 166 // Parse value to json object 167 const values = JSON.parse(value); 168 169 yield html` 170 <tr> 171 <td> 172 <span class="${keyClass}" style="margin-left: ${ident * 20}px">${anchor(path, key)}:</span> 173 </td> 174 <td class="comment"># </td> 175 <td class="comment" rowspan="${1 + Object.keys(values).length}"> 176 ${unsafeHTML(desc)} 177 </td> 178 </tr> 179 `; 180 181 for (const k in values) { 182 if (!values.hasOwnProperty(k)) continue; 183 const v = values[k]; 184 185 yield html` 186 <tr> 187 <td> 188 <span class="key" style="margin-left: ${(ident + 1) * 20}px">${k}: <span class="${valueClass}">${v}</span></span> 189 </td> 190 <td class="comment"># </td> 191 </tr> 192 `; 193 } 194 } else { 195 yield html` 196 <tr> 197 <td> 198 <span class="${keyClass}" style="margin-left: ${ident * 20}px">${anchor(path, key)}:</span> 199 <span class="${valueClass}">${value}</span> 200 <span class="${keyClass}">${getLatest(key === 'apiVersion' && latest === value)}</span> 201 </td> 202 <td class="comment"># </td> 203 <td class="comment">${unsafeHTML(desc)}</td> 204 </tr> 205 `; 206 } 207 208 // This definition references another definition 209 if (definition.$ref) { 210 yield html` 211 ${template(definitions, definition, definition.$ref, ident + 1, path)} 212 `; 213 } 214 215 // This definition is an array 216 if (definition.items && definition.items.$ref) { 217 // don't infinitely recurse into nested tagger components 218 if (definition.items.$ref === "#/definitions/TaggerComponent") { 219 yield html ``; 220 } else { 221 yield html` 222 ${template(definitions, definition, definition.items.$ref, ident + 1, path)} 223 `; 224 } 225 } 226 } 227 } 228 229 function getLatest(isLatest) { 230 return isLatest ? "latest" : ""; 231 } 232 233 function anchor(path, label) { 234 return html`<a class="anchor" id="${path}"></a><a class="key" href="#${path}">${label}</a>` 235 }