Skip to content

Commit

Permalink
Update doc-versions json and js
Browse files Browse the repository at this point in the history
  • Loading branch information
gadenbuie committed Nov 3, 2023
1 parent 9ecfdf6 commit 44f7413
Show file tree
Hide file tree
Showing 6 changed files with 389 additions and 61 deletions.
83 changes: 72 additions & 11 deletions dev/doc-versions.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,39 @@
return origin + path
}

function pathDir (url) {
return url.replace(/\/[^\/]+$/, '/')
}

function ensureTrailingSlash (url) {
return /\/$/.test(url) ? url : url + '/'
}

function ensureFullPath (url) {
return /^\//.test(url) ? window.location.origin + url : url
}

function findPkgdownLocalRoot () {
// get `src` attribute of the current <script> tag
const scripts = document.getElementsByTagName('script')
const currentScript = scripts[scripts.length - 1]
const src = currentScript.getAttribute('src').replace(/\/[^\/]+$/, '/')

const currentDir = window.location.href.replace(/\/[^\/]+$/, '/')
let src = pathDir(currentScript.getAttribute('src'))
if (!src.includes('/') || src === "/") {
src = ''
}

const currentDir = pathDir(window.location.href)
const root = new URL(currentDir + src).toString()
return ensureTrailingSlash(root)
}

function makeNewPkgdownLink (newBase) {
newBase = ensureFullPath(newBase)
newBase = ensureTrailingSlash(newBase)
return window.location.href.replace(findPkgdownLocalRoot(), newBase)
}

async function getVersions () {
let versionsUrl
if (window.PKGDOWN_VERSIONS_URL) {
Expand Down Expand Up @@ -103,26 +121,22 @@
return
}

for (const [text, url] of Object.entries(item)) {
const isCurrent = text === `v${current.innerText}`
ul.appendChild(createVersionDropdownItem(text, url, isCurrent))
}
const version = item.version ? item.version : label
const isCurrent = current.innerText === version
ul.appendChild(createVersionDropdownItem(item, isCurrent))
})

return dropdown
}

function createVersionDropdownItem (text, url, isCurrent = false) {
function createVersionDropdownItem ({text, url}, isCurrent = false) {
const li = document.createElement('li')
const a = document.createElement('a')
a.classList.add('dropdown-item')
if (isCurrent) a.classList.add('fw-bold')

// link to current page in the other version (may not exist!)
a.href = window.location.href.replace(
findPkgdownLocalRoot(),
ensureTrailingSlash(url)
)
a.href = makeNewPkgdownLink(url)
a.innerText = text

li.appendChild(a)
Expand Down Expand Up @@ -150,6 +164,52 @@
return li
}

function insertBanner (banner) {
if (!banner) return
const main = document.querySelector('main')
if (!navbar) return

let { html, class: classes } = banner
if (typeof classes === 'string') {
// Classes can be a string or an array of strings
classes = [classes]
}

// <div class="d-block px-3 py-2 text-center text-bold skippy">
// <a href="https://getbootstrap.com/" class="text-white text-decoration-none">There's a newer version of Bootstrap!</a>
// </div>
const div = document.createElement('div')
div.classList.add('alert', 'px-3', 'py-2', 'text-center', ...classes)
div.innerHTML = html
div
.querySelectorAll('a')
.forEach(a => {
if (a.getAttribute('href') === '#') {
a.href = makeNewPkgdownLink(findPkgdownGlobalRoot())
}
})

main.insertAdjacentElement('afterbegin', div)
return div
}

function maybeInsertBanner (versions) {
if (!versions) return
if (!Array.isArray(versions)) return

// keep versions entries that are objects with a `banner` property
versions = versions.filter(item => item && item.banner)

const localRoot = findPkgdownLocalRoot()

for (const version of versions) {
const versionUrl = ensureFullPath(version.url)
if (localRoot === versionUrl) {
return insertBanner(version.banner)
}
}
}

async function replaceVersionWithMenu () {
const current = document.querySelector('.navbar .navbar-brand + small')
if (!current) return
Expand All @@ -159,6 +219,7 @@

const dropdown = createVersionDropdown(current, versions)
current.replaceWith(dropdown)
maybeInsertBanner(versions)
}

replaceVersionWithMenu()
Expand Down
83 changes: 72 additions & 11 deletions doc-versions.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,39 @@
return origin + path
}

function pathDir (url) {
return url.replace(/\/[^\/]+$/, '/')
}

function ensureTrailingSlash (url) {
return /\/$/.test(url) ? url : url + '/'
}

function ensureFullPath (url) {
return /^\//.test(url) ? window.location.origin + url : url
}

function findPkgdownLocalRoot () {
// get `src` attribute of the current <script> tag
const scripts = document.getElementsByTagName('script')
const currentScript = scripts[scripts.length - 1]
const src = currentScript.getAttribute('src').replace(/\/[^\/]+$/, '/')

const currentDir = window.location.href.replace(/\/[^\/]+$/, '/')
let src = pathDir(currentScript.getAttribute('src'))
if (!src.includes('/') || src === "/") {
src = ''
}

const currentDir = pathDir(window.location.href)
const root = new URL(currentDir + src).toString()
return ensureTrailingSlash(root)
}

function makeNewPkgdownLink (newBase) {
newBase = ensureFullPath(newBase)
newBase = ensureTrailingSlash(newBase)
return window.location.href.replace(findPkgdownLocalRoot(), newBase)
}

async function getVersions () {
let versionsUrl
if (window.PKGDOWN_VERSIONS_URL) {
Expand Down Expand Up @@ -103,26 +121,22 @@
return
}

for (const [text, url] of Object.entries(item)) {
const isCurrent = text === `v${current.innerText}`
ul.appendChild(createVersionDropdownItem(text, url, isCurrent))
}
const version = item.version ? item.version : label
const isCurrent = current.innerText === version
ul.appendChild(createVersionDropdownItem(item, isCurrent))
})

return dropdown
}

function createVersionDropdownItem (text, url, isCurrent = false) {
function createVersionDropdownItem ({text, url}, isCurrent = false) {
const li = document.createElement('li')
const a = document.createElement('a')
a.classList.add('dropdown-item')
if (isCurrent) a.classList.add('fw-bold')

// link to current page in the other version (may not exist!)
a.href = window.location.href.replace(
findPkgdownLocalRoot(),
ensureTrailingSlash(url)
)
a.href = makeNewPkgdownLink(url)
a.innerText = text

li.appendChild(a)
Expand Down Expand Up @@ -150,6 +164,52 @@
return li
}

function insertBanner (banner) {
if (!banner) return
const main = document.querySelector('main')
if (!navbar) return

let { html, class: classes } = banner
if (typeof classes === 'string') {
// Classes can be a string or an array of strings
classes = [classes]
}

// <div class="d-block px-3 py-2 text-center text-bold skippy">
// <a href="https://getbootstrap.com/" class="text-white text-decoration-none">There's a newer version of Bootstrap!</a>
// </div>
const div = document.createElement('div')
div.classList.add('alert', 'px-3', 'py-2', 'text-center', ...classes)
div.innerHTML = html
div
.querySelectorAll('a')
.forEach(a => {
if (a.getAttribute('href') === '#') {
a.href = makeNewPkgdownLink(findPkgdownGlobalRoot())
}
})

main.insertAdjacentElement('afterbegin', div)
return div
}

function maybeInsertBanner (versions) {
if (!versions) return
if (!Array.isArray(versions)) return

// keep versions entries that are objects with a `banner` property
versions = versions.filter(item => item && item.banner)

const localRoot = findPkgdownLocalRoot()

for (const version of versions) {
const versionUrl = ensureFullPath(version.url)
if (localRoot === versionUrl) {
return insertBanner(version.banner)
}
}
}

async function replaceVersionWithMenu () {
const current = document.querySelector('.navbar .navbar-brand + small')
if (!current) return
Expand All @@ -159,6 +219,7 @@

const dropdown = createVersionDropdown(current, versions)
current.replaceWith(dropdown)
maybeInsertBanner(versions)
}

replaceVersionWithMenu()
Expand Down
35 changes: 29 additions & 6 deletions doc-versions.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,36 @@
[
{
"Latest Release": "https://pkg.garrickadenbuie.com/epoxy/",
"Development": "https://pkg.garrickadenbuie.com/epoxy/dev/"
"text": "Latest",
"version": "1.0.0",
"url": "/"
},
{
"text": "Development",
"version": "1.0.0.9000",
"url": "/dev/",
"banner": {
"html": "This is the development version of epoxy. View the <a href=\"#\">latest release</a>.",
"class": ["alert-warning"]
}
},
"---",
"Previous Releases",
"Releases",
{
"text": "v0.1.1",
"version": "0.1.1",
"url": "/v0.1.1/",
"banner": {
"html": "A <a href=\"#\">newer version of epoxy</a> is available!",
"class": ["alert-info"]
}
},
{
"v1.0.0": "https://pkg.garrickadenbuie.com/epoxy/v1.0.0/",
"v0.1.1": "https://pkg.garrickadenbuie.com/epoxy/v0.1.1/",
"v0.1.0": "https://pkg.garrickadenbuie.com/epoxy/v0.1.0/"
"text": "v0.1.0",
"version": "0.1.0",
"url": "/v0.1.0/",
"banner": {
"html": "A <a href=\"#\">newer version of epoxy</a> is available!",
"class": ["alert-info"]
}
}
]
Loading

0 comments on commit 44f7413

Please sign in to comment.