| 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 | import { marked } from 'marked'; |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | class LinkBuilder { |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | #sourcesLinksCache; |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | #sourcesLinks; |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | #classesLinksCache; |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | #classesLinks; |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | #variablesLinksCache; |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | #variablesLinks; |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | #mdnLinks = Object.freeze ( |
| 88 | { |
| 89 | |
| 90 | |
| 91 | Array : 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array', |
| 92 | ArrayBuffer : 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer', |
| 93 | Boolean : 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean', |
| 94 | Error : 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error', |
| 95 | File : 'https://developer.mozilla.org/en-US/docs/Web/API/File', |
| 96 | Function : 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function', |
| 97 | Map : 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map', |
| 98 | Number : 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number', |
| 99 | Object : 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object', |
| 100 | Promise : 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise', |
| 101 | RegExp : 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp', |
| 102 | String : 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String', |
| 103 | Uint8Array : 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array', |
| 104 | |
| 105 | |
| 106 | Class : 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/class', |
| 107 | |
| 108 | |
| 109 | CryptoKey : 'https://developer.mozilla.org/en-US/docs/Web/API/CryptoKey', |
| 110 | Event : 'https://developer.mozilla.org/en-US/docs/Web/API/Event', |
| 111 | GeolocationPosition : 'https://developer.mozilla.org/en-US/docs/Web/API/GeolocationPosition', |
| 112 | GeolocationPositionError : 'https://developer.mozilla.org/en-US/docs/Web/API/GeolocationPositionError', |
| 113 | IDBFactory : 'https://developer.mozilla.org/en-US/docs/Web/API/IDBFactory', |
| 114 | HTMLCollection : 'https://developer.mozilla.org/en-US/docs/Web/API/HTMLCollection', |
| 115 | HTMLElement : 'https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement', |
| 116 | Node : 'https://developer.mozilla.org/en-US/docs/Web/API/Node', |
| 117 | NodeList : 'https://developer.mozilla.org/en-US/docs/Web/API/NodeList', |
| 118 | SVGElement : 'https://developer.mozilla.org/en-US/docs/Web/API/SVGElement', |
| 119 | Touch : 'https://developer.mozilla.org/en-US/docs/Web/API/Touch', |
| 120 | TouchList : 'https://developer.mozilla.org/en-US/docs/Web/API/TouchList', |
| 121 | XMLDocument : 'https://developer.mozilla.org/en-US/docs/Web/API/XMLDocument', |
| 122 | |
| 123 | |
| 124 | LeafletObject : 'https://leafletjs.com/reference.html', |
| 125 | OsmElement : 'https://wiki.openstreetmap.org/wiki/Overpass_API/Overpass_QL', |
| 126 | JsonObject : 'https://www.json.org/json-en.html' |
| 127 | } |
| 128 | ); |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | constructor ( ) { |
| 135 | Object.freeze ( this ); |
| 136 | this.#sourcesLinks = new Map ( ); |
| 137 | this.#classesLinks = new Map ( ); |
| 138 | this.#variablesLinks = new Map ( ); |
| 139 | } |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | getClassLink ( className, rootPath ) { |
| 150 | const classLink = this.#classesLinks.get ( className ); |
| 151 | return classLink ? `<a href="${rootPath + classLink}">${className}</a>` : className; |
| 152 | } |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | setClassLink ( classDoc ) { |
| 160 | this.#classesLinks.set ( |
| 161 | classDoc.name, |
| 162 | classDoc.file.substring ( 0, classDoc.file.lastIndexOf ( '/' ) + 1 ) + classDoc.name + '.html' |
| 163 | ); |
| 164 | } |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | get classesLinks ( ) { |
| 172 | if ( ! this.#classesLinksCache ) { |
| 173 | |
| 174 | |
| 175 | this.#classesLinksCache = this.#classesLinksCache ?? Array.from ( this.#classesLinks ).sort ( |
| 176 | ( first, second ) => first [ 0 ] .localeCompare ( second [ 0 ] ) |
| 177 | ); |
| 178 | } |
| 179 | |
| 180 | return this.#classesLinksCache; |
| 181 | } |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | getSourceLink ( doc ) { |
| 190 | let sourceLink = this.#sourcesLinks.get ( doc.file ); |
| 191 | if ( sourceLink ) { |
| 192 | |
| 193 | return doc.rootPath + sourceLink + '#L' + String ( doc.line ).padStart ( 5, '_' ); |
| 194 | } |
| 195 | return null; |
| 196 | } |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | setSourceLink ( fileName, path ) { |
| 205 | this.#sourcesLinks.set ( fileName, path ); |
| 206 | } |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | get sourcesLinks ( ) { |
| 215 | if ( ! this.#sourcesLinksCache ) { |
| 216 | this.#sourcesLinksCache = Array.from ( this.#sourcesLinks ).sort ( |
| 217 | ( first, second ) => { |
| 218 | const firstPath = first [ 0 ].substring ( 0, first [ 0 ].lastIndexOf ( '/' ) ); |
| 219 | const firstFile = first [ 0 ].substring ( first [ 0 ].lastIndexOf ( '/' ) + 1 ); |
| 220 | const secondPath = second [ 0 ].substring ( 0, second [ 0 ].lastIndexOf ( '/' ) ); |
| 221 | const secondFile = second [ 0 ].substring ( second [ 0 ].lastIndexOf ( '/' ) + 1 ); |
| 222 | const pathCompare = firstPath.localeCompare ( secondPath ); |
| 223 | if ( 0 === pathCompare ) { |
| 224 | return firstFile.localeCompare ( secondFile ); |
| 225 | } |
| 226 | return pathCompare; |
| 227 | } |
| 228 | ); |
| 229 | } |
| 230 | return this.#sourcesLinksCache; |
| 231 | } |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | setVariableLink ( variableDoc ) { |
| 239 | this.#variablesLinks.set ( |
| 240 | variableDoc.name, |
| 241 | `variables.html#${variableDoc.name}` |
| 242 | ); |
| 243 | } |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | get variablesLinks ( ) { |
| 251 | if ( ! this.#variablesLinksCache ) { |
| 252 | this.#variablesLinksCache = Array.from ( this.#variablesLinks ).sort ( |
| 253 | ( first, second ) => first [ 0 ] .localeCompare ( second [ 0 ] ) |
| 254 | ); |
| 255 | } |
| 256 | return this.#variablesLinksCache; |
| 257 | } |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | #getTypeLink ( type, rootPath ) { |
| 269 | if ( 'constructor' === type ) { |
| 270 | return type; |
| 271 | } |
| 272 | const classLink = this.#classesLinks.get ( type ); |
| 273 | if ( classLink ) { |
| 274 | return `<a href="${rootPath + classLink}">${type}</a>`; |
| 275 | } |
| 276 | const mdnLink = this.#mdnLinks [ type ]; |
| 277 | if ( mdnLink ) { |
| 278 | return `<a href="${mdnLink}">${type}</a>`; |
| 279 | } |
| 280 | const variableLink = this.#variablesLinks.get ( type ); |
| 281 | if ( variableLink ) { |
| 282 | return `<a href="${variableLink}">${type}</a>`; |
| 283 | } |
| 284 | return type; |
| 285 | } |
| 286 | |
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | isKnownType ( type ) { |
| 295 | return Boolean ( this.#classesLinks.get ( type ) || this.#mdnLinks [ type ] ); |
| 296 | } |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | getTypeLinks ( type, rootPath ) { |
| 308 | if ( ! type ) { |
| 309 | return 'null'; |
| 310 | } |
| 311 | let returnType = ''; |
| 312 | type.split ( ' ' ).forEach ( |
| 313 | tmpType => returnType += this.#getTypeLink ( tmpType, rootPath ) + ' ' |
| 314 | ); |
| 315 | |
| 316 | return returnType.trimEnd ( ); |
| 317 | } |
| 318 | |
| 319 | |
| 320 | |
| 321 | |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 327 | getDescLink ( desc, rootPath ) { |
| 328 | let returnDesc = ''; |
| 329 | desc.split ( ' ' ).forEach ( |
| 330 | word => returnDesc += this.#getTypeLink ( word, rootPath ) + ' ' |
| 331 | ); |
| 332 | return marked.parse ( returnDesc.trimEnd ( ) ); |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | |
| 337 | |
| 338 | |
| 339 | |
| 340 | |
| 341 | const theLinkBuilder = new LinkBuilder ( ); |
| 342 | |
| 343 | export default theLinkBuilder; |
| 344 | |
| 345 | |
| 346 | |