Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite cheat-sheet URLs to access page sections #196

Merged
merged 5 commits into from
Oct 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 80 additions & 8 deletions src/js/50-cheat-sheet-toggle.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,33 @@ const prodMatrix = {
'neo4j-enterprise': 'neo4j-ee',
}

const defaultProd = 'auradb-enterprise'

const defaultClasses = ['exampleblock', 'sect2', 'sect1']

document.addEventListener('DOMContentLoaded', function () {
if (!document.querySelector('body.cheat-sheet')) return

const selectionFromPath = fixURL()

const curURL = document.location
const selectionFromPath = getSelectionFromPath(curURL)

if (selectionFromPath) {
updateSelectorFromProduct(selectionFromPath)
updateMetaFromProduct(selectionFromPath, curURL)
}

const queryString = document.location.search
const urlParams = new URLSearchParams(queryString)

if (urlParams.has('sid')) {
const scrollToSection = checkHashVariations(urlParams.get('sid'))
if (scrollToSection) {
document.location.hash = scrollToSection
}
document.location.replace(document.location.href.replace(document.location.search, ''))
}

// check for a checkbox to display or hide labels
const labelShow = document.querySelector('#products-highlight')
if (labelShow) {
Expand Down Expand Up @@ -363,19 +377,77 @@ const stripTrailingSlash = (str) => {
return str.endsWith('/') ? str.slice(0, -1) : str
}

function getSelectionFromPath (pageURL) {
const pathArr = stripTrailingSlash(pageURL.pathname).split('/')
function fixURL () {
const url = window.location
let href = url.href
// eg /docs/cypher-cheat-sheet/current/where
// or /docs/cypher-cheat-sheet/5/auradb-free/
// or /docs/cypher-cheat-sheet/5/auradb-free/where

const pathArr = stripTrailingSlash(url.pathname).split('/')
if (pathArr[0] === '') pathArr.shift()
const values = pathArr.slice(pathArr.indexOf('cypher-cheat-sheet'))
values.shift()
// there should be three elements to the path from here: version, product, [section]
values.length = 3

// the first item in values should be a version number
// let version = values[0]
// the second item in values should be the product
const pathProduct = values[1]
if (!pathProduct) return
// parse pathProduct to match something you could select in the product dropdown
if (Object.keys(prodMatrix).includes(pathProduct)) {
return prodMatrix[pathProduct]
let product = values[1]
// the third is a page that can be turned into a section id
let possibleID = values[2]
let id = ''

// just return if there's no product for some reason
// todo: force a product
if (!product) return

if (possibleID) {
id = checkHashVariations(possibleID)
console.log(id)
}

// update window.location.href
if (!Object.keys(prodMatrix).includes(product)) {
const reProd = new RegExp(`/${product}/?`)
href = href.replace(reProd, `/${defaultProd}/`)
// maybe this was actually the page, which could be converted to a section id
possibleID = product
product = defaultProd
id = checkHashVariations(possibleID)
if (id) {
window.location.hash = '#' + id
href = href + '#' + id
}
}

// update window.location.hash
if (id && Object.keys(prodMatrix).includes(product)) {
window.location.hash = '#' + id
const reHash = new RegExp(`/${possibleID}/?`)
href = href.replace(reHash, `#${id}`)
}

if (href !== url.href) {
window.location.replace(href)
}

return prodMatrix[product]
}

function checkHashVariations (id) {
const dashes = /-/g
const idVariants = [
id,
'_' + id.replace(dashes, '_'),
]

const actualID = idVariants.filter(function (i) {
return document.getElementById(i)
})

if (actualID) return actualID[0]
}

function removeDefaultClasses (c) {
Expand Down
Loading