extra.js 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. var gitHubLinks = $("a[href^='" + relativePath + "']").each(function (index) {
  48. var url = $(this).attr('href');
  49. $(this).attr('href', url.replace(relativePath, gitHubPath));
  50. });
  51. }
  52. }
  53. /**
  54. * Analyzes the URL of the current page to find out what the selected GitHub branch is. It's usually
  55. * part of the location path. The code needs to distinguish between running MkDocs standalone
  56. * and docs served from RTD. If no valid branch could be determined 'dev' returned.
  57. *
  58. * @returns GitHub branch name
  59. */
  60. function determineSelectedBranch() {
  61. var branch = 'dev', path = window.location.pathname;
  62. if (isOnRtd()) {
  63. // path is like /en/<branch>/<lang>/build/ -> extract 'lang'
  64. // split[0] is an '' because the path starts with the separator
  65. var thirdPathSegment = path.split('/')[2];
  66. // 'latest' is an alias on RTD for the 'dev' branch - which is the default for 'branch' here
  67. if (thirdPathSegment != 'latest') {
  68. branch = thirdPathSegment;
  69. }
  70. }
  71. return branch;
  72. }
  73. function values(associativeArray) {
  74. var values = [];
  75. for (var key in associativeArray) {
  76. if (associativeArray.hasOwnProperty(key)) {
  77. values.push(associativeArray[key]);
  78. }
  79. }
  80. return values;
  81. }
  82. function isOnRtd() {
  83. return window.location.origin.indexOf('readthedocs') > -1;
  84. }
  85. }());