github.com/SamarSidharth/kpt@v0.0.0-20231122062228-c7d747ae3ace/site/static/js/plugins/plugins.js (about) 1 function convertFromHugo(content) { 2 const hugoHideDirectives = /{{% hide %}}.+?{{% \/hide %}}/gms; 3 const hugoDirectiveTags = /{{.*}}/g; 4 5 content = processHugoTitleHeading(content); 6 return content.replace(hugoHideDirectives, "").replace(hugoDirectiveTags, ""); 7 } 8 9 async function addVersionDropdown() { 10 const sidebar = document.getElementsByClassName("sidebar").item(0); 11 const latestVersion = "v1.0.0-beta"; 12 const versionDropdown = ` 13 <div class="dropdown"> 14 <button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">${latestVersion} 15 <span class="caret"></span></button> 16 <ol class="dropdown-menu"> 17 <li><a href="/installation/">${latestVersion}</a></li> 18 <li><a href="https://googlecontainertools.github.io/kpt/installation/" target="_self">v0.39</a></li> 19 </ol> 20 </div> 21 `; 22 const node = document.createElement("div"); 23 node.innerHTML = versionDropdown; 24 sidebar.getElementsByClassName("app-name").item(0).appendChild(node); 25 } 26 27 function showBookPageFooters() { 28 const isBookPage = document.location.pathname 29 .toLowerCase() 30 .startsWith("/book"); 31 32 const hideButtonsToNonBookPages = (buttons) => { 33 buttons.forEach((el) => { 34 url = new URL(el.lastElementChild.href); 35 el.style.display = isBookPage && url.pathname.toLowerCase().startsWith("/book") 36 ? "flex" 37 : "none"; 38 }); 39 }; 40 41 const previousPaginationButtons = Array.from( 42 document.getElementsByClassName("pagination-item--previous") 43 ); 44 45 const nextPaginationButtons = Array.from( 46 document.getElementsByClassName("pagination-item--next") 47 ); 48 49 hideButtonsToNonBookPages( 50 previousPaginationButtons.concat(nextPaginationButtons) 51 ); 52 } 53 54 function addSidebarCollapsibility(sidebar) { 55 const tocLists = Array.from(sidebar?.getElementsByTagName("ul")); 56 57 // Hide a child list if neither its parent nor any of its descendants are active. 58 tocLists.forEach((ul) => 59 ul.parentElement.classList.contains("active") || 60 ul.getElementsByClassName("active").length 61 ? ul.classList.remove("inactive") 62 : ul.classList.add("inactive") 63 ); 64 } 65 66 // Make Markdown standard titles (# Title) out of the following: 67 // +++ 68 // title: Page Title 69 // +++ 70 function processHugoTitleHeading(content) { 71 const titleBlock = /^[\+\-]{3}[\s\S]*?^[\+\-]{3}$/m; 72 const titleMatch = content.match(/title:\s*["'](.*)["']/); 73 74 const titleHeading = titleMatch ? `# ${titleMatch[1]}` : ""; 75 76 return content.replace(titleBlock, titleHeading); 77 } 78 79 // Convert Hugo Asciinema directives to HTML. 80 function processAsciinemaTags(content) { 81 const asciinemaDirective = /{{<\s*asciinema.+key="(.+?)".+}}/g; 82 83 return content.replace( 84 asciinemaDirective, 85 (_, fileName) => 86 `<asciinema-player src="${window.location.origin}/static/casts/${fileName}.cast" cols="160"></asciinema-player>` 87 ); 88 } 89 // Workaround for https://github.com/docsifyjs/docsify/pull/1468 90 function defaultLinkTargets() { 91 const externalPageLinks = Array.from( 92 document.getElementsByTagName("a") 93 ).filter( 94 (a) => 95 window.Docsify.util.isExternal(a.href) && 96 !window.$docsify.crossOriginLinks.includes(a.href) 97 ); 98 externalPageLinks.forEach( 99 (a) => (a.target = window.$docsify.externalLinkTarget) 100 ); 101 } 102 103 function localPlugins(hook, _vm) { 104 // Process Markdown directives appropriately. 105 hook.beforeEach(function (content) { 106 content = processAsciinemaTags(content); 107 108 // Until all source markdown files stop using Hugo directives, 109 // convert here for compatibility. 110 content = convertFromHugo(content); 111 return content; 112 }); 113 114 hook.mounted(addVersionDropdown); 115 116 // Show navigation footer for book pages. 117 hook.doneEach(showBookPageFooters); 118 119 // Reset all external links to their appropriate targets. 120 hook.doneEach(defaultLinkTargets); 121 122 // Process elements in the navigation sidebar. 123 hook.doneEach(function () { 124 const sidebar = document.getElementsByClassName("sidebar-nav").item(0); 125 126 // Only show child pages for currently active page to avoid sidebar cluttering. 127 addSidebarCollapsibility(sidebar); 128 }); 129 } 130 131 // Load plugins into Docsify. 132 window.$docsify = window.$docsify || {}; 133 window.$docsify.plugins = [localPlugins].concat(window.$docsify.plugins || []); 134