extra.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. var nodemcu = nodemcu || {};
  2. (function () {
  3. 'use strict';
  4. $(document).ready(function () {
  5. addToc();
  6. replaceRelativeLinksWithStaticGitHubUrl();
  7. });
  8. /**
  9. * Adds a TOC-style table to each page in the 'Modules' section.
  10. */
  11. function addToc() {
  12. var func, intro, tocHtmlTable;
  13. if (isModulePage()) {
  14. tocHtmlTable = '<table class="docutils">';
  15. $('h2').each(function (index) {
  16. // 'slice' cuts off the single permalink character at the end of the text (e.g. '¶')
  17. func = $(this).text().slice(0, -1);
  18. // get the first sentence of the paragraph directly below h2
  19. intro = $(this).next().text();
  20. intro = intro.substring(0, intro.indexOf('.') + 1);
  21. tocHtmlTable += createTocTableRow(func, intro);
  22. });
  23. tocHtmlTable += '</table>';
  24. $(tocHtmlTable).insertBefore($('h2').first())
  25. }
  26. function isModulePage() {
  27. // if the breadcrumb contains 'Modules »' it must be an API page
  28. return $("ul.wy-breadcrumbs li:contains('Modules »')").size() > 0;
  29. }
  30. function createTocTableRow(func, intro) {
  31. // fragile attempt to auto-create the in-page anchor
  32. // good tests: file.md,
  33. var href = func.replace(/[\.:\(\)]/g, '').replace(/ --|, | /g, '-');
  34. var link = '<a href="#' + href.toLowerCase() + '">' + func + '</a>';
  35. return '<tr><td>' + link + '</td><td>' + intro + '</td></tr>';
  36. }
  37. }
  38. /**
  39. * The module doc pages contain relative links to artifacts in the GitHub repository. For those links to work both
  40. * on GitHub (i.e. when the page is viewed on GitHub) and on RTD they are defined with a relative URL. This function
  41. * replaces the relative path with an absolute path based on the selected branch.
  42. */
  43. function replaceRelativeLinksWithStaticGitHubUrl() {
  44. if (isOnRtd()) {
  45. var relativePath = "../..";
  46. var gitHubPath = "https://github.com/nodemcu/nodemcu-firmware/tree/" + determineSelectedBranch();
  47. // 'div.section' denotes the container into which doc pages are integrated i.e. "the content" w/o navigation,
  48. // header, breadcrumbs, footer, etc. It's important that only links in this very section are manipulated.
  49. var gitHubLinks = $("div.section a[href^='" + relativePath + "']").each(function (index) {
  50. var url = $(this).attr('href');
  51. $(this).attr('href', url.replace(relativePath, gitHubPath));
  52. });
  53. }
  54. }
  55. /**
  56. * Analyzes the URL of the current page to find out what the selected GitHub branch is. It's usually
  57. * part of the location path. The code needs to distinguish between running MkDocs standalone
  58. * and docs served from RTD. If no valid branch could be determined 'dev' returned.
  59. *
  60. * @returns GitHub branch name
  61. */
  62. function determineSelectedBranch() {
  63. var branch = 'dev', path = window.location.pathname;
  64. if (isOnRtd()) {
  65. // path is like /en/<branch>/<lang>/build/ -> extract 'lang'
  66. // split[0] is an '' because the path starts with the separator
  67. var thirdPathSegment = path.split('/')[2];
  68. // 'latest' is an alias on RTD for the 'dev' branch - which is the default for 'branch' here
  69. if (thirdPathSegment != 'latest') {
  70. branch = thirdPathSegment;
  71. }
  72. }
  73. return branch;
  74. }
  75. function values(associativeArray) {
  76. var values = [];
  77. for (var key in associativeArray) {
  78. if (associativeArray.hasOwnProperty(key)) {
  79. values.push(associativeArray[key]);
  80. }
  81. }
  82. return values;
  83. }
  84. function isOnRtd() {
  85. return window.location.origin.indexOf('readthedocs') > -1;
  86. }
  87. }());