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
This commit is contained in:
Marcel Stör 2022-01-15 01:24:50 +01:00
parent fb232337e5
commit 46959c1bae
1 changed files with 18 additions and 23 deletions

View File

@ -1,4 +1,3 @@
var nodemcu = nodemcu || {};
(function () { (function () {
'use strict'; 'use strict';
@ -11,10 +10,10 @@ var nodemcu = nodemcu || {};
* Adds a TOC-style table to each page in the 'Modules' section. * Adds a TOC-style table to each page in the 'Modules' section.
*/ */
function addToc() { function addToc() {
var func, intro, tocHtmlTable; let func, intro, tocHtmlTable;
if (isModulePage()) { if (isModulePage()) {
tocHtmlTable = '<table class="docutils">'; tocHtmlTable = '<table class="docutils">';
$('h2').each(function (index) { $('h2').each(function () {
// 'slice' cuts off the single permalink character at the end of the text (e.g. '¶') // 'slice' cuts off the single permalink character at the end of the text (e.g. '¶')
func = $(this).text().slice(0, -1); func = $(this).text().slice(0, -1);
// get the first sentence of the paragraph directly below h2 // get the first sentence of the paragraph directly below h2
@ -32,8 +31,8 @@ var nodemcu = nodemcu || {};
function createTocTableRow(func, intro) { function createTocTableRow(func, intro) {
// fragile attempt to auto-create the in-page anchor // fragile attempt to auto-create the in-page anchor
// good tests: file.md, // good tests: file.md,
var href = func.replace(/[\.:\(\)]/g, '').replace(/ --|, | /g, '-'); const href = func.replace(/[\.:\(\)]/g, '').replace(/ --|, | /g, '-');
var link = '<a href="#' + href.toLowerCase() + '">' + func + '</a>'; const link = '<a href="#' + href.toLowerCase() + '">' + func + '</a>';
return '<tr><td>' + link + '</td><td>' + intro + '</td></tr>'; return '<tr><td>' + link + '</td><td>' + intro + '</td></tr>';
} }
} }
@ -45,13 +44,18 @@ var nodemcu = nodemcu || {};
*/ */
function replaceRelativeLinksWithStaticGitHubUrl() { function replaceRelativeLinksWithStaticGitHubUrl() {
if (isOnRtd()) { if (isOnRtd()) {
var relativePath = "../.."; const ignoredUrlSegments = ["/lua-modules/", "/modules/"];
var gitHubPath = "https://github.com/nodemcu/nodemcu-firmware/tree/" + determineSelectedBranch(); 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, // '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. // 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) { $("div.section a[href^='" + relativePath + "']").each(function () {
var url = $(this).attr('href'); const url = $(this).attr('href');
$(this).attr('href', url.replace(relativePath, gitHubPath)); // 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 * @returns GitHub branch name
*/ */
function determineSelectedBranch() { function determineSelectedBranch() {
var branch = 'dev', path = window.location.pathname; let branch = 'dev';
const path = window.location.pathname;
if (isOnRtd()) { if (isOnRtd()) {
// path is like /en/<branch>/<lang>/build/ -> extract 'lang' // path is like /en/<branch>/<lang>/build/ -> extract 'lang'
// split[0] is an '' because the path starts with the separator // 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 // '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; branch = thirdPathSegment;
} }
} }
return branch; return branch;
} }
function values(associativeArray) {
var values = [];
for (var key in associativeArray) {
if (associativeArray.hasOwnProperty(key)) {
values.push(associativeArray[key]);
}
}
return values;
}
function isOnRtd() { function isOnRtd() {
return window.location.origin.indexOf('readthedocs') > -1; return window.location.origin.indexOf('readthedocs') > -1;
} }