From 4294be0717d498654f3d5b4af94c8daa09127166 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20St=C3=B6r?= Date: Sat, 15 Jan 2022 01:24:50 +0100 Subject: [PATCH] Fix the handling of relative links Do not change the URL to artifacts that reside inside the `/docs` folder as they are correctly managed by MkDocs. Fixes #3495 --- docs/js/extra.js | 41 ++++++++++++++++++----------------------- 1 file changed, 18 insertions(+), 23 deletions(-) diff --git a/docs/js/extra.js b/docs/js/extra.js index 86d3e6bb..0f0b9fe3 100644 --- a/docs/js/extra.js +++ b/docs/js/extra.js @@ -1,4 +1,3 @@ -var nodemcu = nodemcu || {}; (function () { 'use strict'; @@ -11,10 +10,10 @@ var nodemcu = nodemcu || {}; * Adds a TOC-style table to each page in the 'Modules' section. */ function addToc() { - var func, intro, tocHtmlTable; + let func, intro, tocHtmlTable; if (isModulePage()) { tocHtmlTable = ''; - $('h2').each(function (index) { + $('h2').each(function () { // 'slice' cuts off the single permalink character at the end of the text (e.g. 'ΒΆ') func = $(this).text().slice(0, -1); // get the first sentence of the paragraph directly below h2 @@ -32,8 +31,8 @@ var nodemcu = nodemcu || {}; function createTocTableRow(func, intro) { // fragile attempt to auto-create the in-page anchor // good tests: file.md, - var href = func.replace(/[\.:\(\)]/g, '').replace(/ --|, | /g, '-'); - var link = '' + func + ''; + const href = func.replace(/[\.:\(\)]/g, '').replace(/ --|, | /g, '-'); + const link = '' + func + ''; return ''; } } @@ -45,13 +44,18 @@ var nodemcu = nodemcu || {}; */ function replaceRelativeLinksWithStaticGitHubUrl() { if (isOnRtd()) { - var relativePath = "../.."; - var gitHubPath = "https://github.com/nodemcu/nodemcu-firmware/tree/" + determineSelectedBranch(); + const ignoredUrlSegments = ["/lua-modules/", "/modules/"]; + const relativePath = "../.."; + const gitHubPath = "https://github.com/nodemcu/nodemcu-firmware/tree/" + determineSelectedBranch(); // 'div.section' denotes the container into which doc pages are integrated i.e. "the content" w/o navigation, // header, breadcrumbs, footer, etc. It's important that only links in this very section are manipulated. - var gitHubLinks = $("div.section a[href^='" + relativePath + "']").each(function (index) { - var url = $(this).attr('href'); - $(this).attr('href', url.replace(relativePath, gitHubPath)); + $("div.section a[href^='" + relativePath + "']").each(function () { + const url = $(this).attr('href'); + // do not change the URL to artifacts that reside inside the /docs folder as they are correctly managed by + // MkDocs + if (!ignoredUrlSegments.some(segment => url.startsWith(relativePath + segment))) { + $(this).attr('href', url.replace(relativePath, gitHubPath)); + } }); } } @@ -64,29 +68,20 @@ var nodemcu = nodemcu || {}; * @returns GitHub branch name */ function determineSelectedBranch() { - var branch = 'dev', path = window.location.pathname; + let branch = 'dev'; + const path = window.location.pathname; if (isOnRtd()) { // path is like /en///build/ -> extract 'lang' // split[0] is an '' because the path starts with the separator - var thirdPathSegment = path.split('/')[2]; + const thirdPathSegment = path.split('/')[2]; // 'latest' is an alias on RTD for the 'dev' branch - which is the default for 'branch' here - if (thirdPathSegment != 'latest') { + if (thirdPathSegment !== 'latest') { branch = thirdPathSegment; } } return branch; } - function values(associativeArray) { - var values = []; - for (var key in associativeArray) { - if (associativeArray.hasOwnProperty(key)) { - values.push(associativeArray[key]); - } - } - return values; - } - function isOnRtd() { return window.location.origin.indexOf('readthedocs') > -1; }
' + link + '' + intro + '