2015-12-27 23:32:07 +01:00
|
|
|
(function () {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
$(document).ready(function () {
|
2016-01-15 22:34:33 +01:00
|
|
|
addToc();
|
2016-03-05 10:47:01 +01:00
|
|
|
replaceRelativeLinksWithStaticGitHubUrl();
|
2015-12-27 23:32:07 +01:00
|
|
|
});
|
|
|
|
|
2016-01-15 22:34:33 +01:00
|
|
|
/**
|
|
|
|
* Adds a TOC-style table to each page in the 'Modules' section.
|
|
|
|
*/
|
|
|
|
function addToc() {
|
2022-01-15 01:24:50 +01:00
|
|
|
let func, intro, tocHtmlTable;
|
2016-01-15 22:34:33 +01:00
|
|
|
if (isModulePage()) {
|
|
|
|
tocHtmlTable = '<table class="docutils">';
|
2022-01-15 01:24:50 +01:00
|
|
|
$('h2').each(function () {
|
2016-01-15 22:34:33 +01:00
|
|
|
// '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
|
|
|
|
intro = $(this).next().text();
|
|
|
|
intro = intro.substring(0, intro.indexOf('.') + 1);
|
|
|
|
tocHtmlTable += createTocTableRow(func, intro);
|
|
|
|
});
|
|
|
|
tocHtmlTable += '</table>';
|
|
|
|
$(tocHtmlTable).insertBefore($('h2').first())
|
|
|
|
}
|
|
|
|
function isModulePage() {
|
|
|
|
// if the breadcrumb contains 'Modules »' it must be an API page
|
2024-02-18 22:14:27 +01:00
|
|
|
return $("ul.wy-breadcrumbs li:contains('C Modules')").length > 0;
|
2016-01-15 22:34:33 +01:00
|
|
|
}
|
|
|
|
function createTocTableRow(func, intro) {
|
|
|
|
// fragile attempt to auto-create the in-page anchor
|
2017-04-13 21:42:18 +02:00
|
|
|
// good tests: file.md,
|
2022-01-15 01:24:50 +01:00
|
|
|
const href = func.replace(/[\.:\(\)]/g, '').replace(/ --|, | /g, '-');
|
|
|
|
const link = '<a href="#' + href.toLowerCase() + '">' + func + '</a>';
|
2016-01-15 22:34:33 +01:00
|
|
|
return '<tr><td>' + link + '</td><td>' + intro + '</td></tr>';
|
|
|
|
}
|
2016-06-08 22:32:21 +02:00
|
|
|
}
|
|
|
|
|
2016-03-05 10:47:01 +01:00
|
|
|
/**
|
|
|
|
* The module doc pages contain relative links to artifacts in the GitHub repository. For those links to work both
|
|
|
|
* on GitHub (i.e. when the page is viewed on GitHub) and on RTD they are defined with a relative URL. This function
|
|
|
|
* replaces the relative path with an absolute path based on the selected branch.
|
|
|
|
*/
|
|
|
|
function replaceRelativeLinksWithStaticGitHubUrl() {
|
2019-02-11 22:52:17 +01:00
|
|
|
if (isOnRtd()) {
|
2022-01-15 01:24:50 +01:00
|
|
|
const ignoredUrlSegments = ["/lua-modules/", "/modules/"];
|
|
|
|
const relativePath = "../..";
|
|
|
|
const gitHubPath = "https://github.com/nodemcu/nodemcu-firmware/tree/" + determineSelectedBranch();
|
2021-08-19 07:44:56 +02:00
|
|
|
// '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.
|
2022-01-15 01:24:50 +01:00
|
|
|
$("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));
|
|
|
|
}
|
2019-02-11 22:52:17 +01:00
|
|
|
});
|
|
|
|
}
|
2016-03-05 10:47:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Analyzes the URL of the current page to find out what the selected GitHub branch is. It's usually
|
|
|
|
* part of the location path. The code needs to distinguish between running MkDocs standalone
|
|
|
|
* and docs served from RTD. If no valid branch could be determined 'dev' returned.
|
|
|
|
*
|
|
|
|
* @returns GitHub branch name
|
|
|
|
*/
|
|
|
|
function determineSelectedBranch() {
|
2022-01-15 01:24:50 +01:00
|
|
|
let branch = 'dev';
|
|
|
|
const path = window.location.pathname;
|
2018-09-04 22:13:55 +02:00
|
|
|
if (isOnRtd()) {
|
2016-03-05 10:47:01 +01:00
|
|
|
// path is like /en/<branch>/<lang>/build/ -> extract 'lang'
|
|
|
|
// split[0] is an '' because the path starts with the separator
|
2022-01-15 01:24:50 +01:00
|
|
|
const thirdPathSegment = path.split('/')[2];
|
2016-07-10 00:57:50 +02:00
|
|
|
// 'latest' is an alias on RTD for the 'dev' branch - which is the default for 'branch' here
|
2022-01-15 01:24:50 +01:00
|
|
|
if (thirdPathSegment !== 'latest') {
|
2016-07-10 00:57:50 +02:00
|
|
|
branch = thirdPathSegment;
|
|
|
|
}
|
2016-03-05 10:47:01 +01:00
|
|
|
}
|
|
|
|
return branch;
|
|
|
|
}
|
|
|
|
|
2018-09-04 22:13:55 +02:00
|
|
|
function isOnRtd() {
|
|
|
|
return window.location.origin.indexOf('readthedocs') > -1;
|
|
|
|
}
|
2017-04-13 21:42:18 +02:00
|
|
|
}());
|