page.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. if ( ! window.frameElement && window.location.protocol !== 'file:' ) {
  2. // If the page is not yet displayed as an iframe of the index page (navigation panel/working links),
  3. // redirect to the index page (using the current URL without extension as the new fragment).
  4. // If this URL itself has a fragment, append it with a dot (since '#' in a URL fragment is not allowed).
  5. let href = window.location.href;
  6. const splitIndex = href.lastIndexOf( '/docs/' ) + 6;
  7. const docsBaseURL = href.slice( 0, splitIndex );
  8. let hash = window.location.hash;
  9. if ( hash !== '' ) {
  10. href = href.replace( hash, '' );
  11. hash = hash.replace( '#', '.' );
  12. }
  13. const extension = href.split( '.' ).pop();
  14. const end = ( extension === 'html' ) ? - 5 : href.length;
  15. const pathSnippet = href.slice( splitIndex, end );
  16. window.location.replace( docsBaseURL + '#' + pathSnippet + hash );
  17. }
  18. function onDocumentLoad() {
  19. let path, localizedPath;
  20. const pathname = window.location.pathname;
  21. const section = /\/(manual|api|examples)\//.exec( pathname )[ 1 ].toString().split( '.html' )[ 0 ];
  22. let name = /[\-A-Za-z0-9]+\.html/.exec( pathname ).toString().split( '.html' )[ 0 ];
  23. switch ( section ) {
  24. case 'api':
  25. localizedPath = /\/api\/[A-Za-z0-9\/]+/.exec( pathname ).toString().slice( 5 );
  26. // Remove localized part of the path (e.g. 'en/' or 'es-MX/'):
  27. path = localizedPath.replace( /^[A-Za-z0-9-]+\//, '' );
  28. break;
  29. case 'examples':
  30. path = localizedPath = /\/examples\/[A-Za-z0-9\/]+/.exec( pathname ).toString().slice( 10 );
  31. break;
  32. case 'manual':
  33. name = name.replace( /\-/g, ' ' );
  34. path = pathname.replace( /\ /g, '-' );
  35. path = localizedPath = /\/manual\/[-A-Za-z0-9\/]+/.exec( path ).toString().slice( 8 );
  36. break;
  37. }
  38. let text = document.body.innerHTML;
  39. text = text.replace( /\[name\]/gi, name );
  40. text = text.replace( /\[path\]/gi, path );
  41. text = text.replace( /\[page:([\w\.]+)\]/gi, '[page:$1 $1]' ); // [page:name] to [page:name title]
  42. text = text.replace( /\[page:\.([\w\.]+) ([\w\.\s]+)\]/gi, `[page:${name}.$1 $2]` ); // [page:.member title] to [page:name.member title]
  43. text = text.replace( /\[page:([\w\.]+) ([\w\.\s]+)\]/gi, '<a class=\'links\' data-fragment=\'$1\' title=\'$1\'>$2</a>' ); // [page:name title]
  44. // text = text.replace( /\[member:.([\w]+) ([\w\.\s]+)\]/gi, "<a onclick=\"window.parent.setUrlFragment('" + name + ".$1')\" title=\"$1\">$2</a>" );
  45. text = text.replace( /\[(member|property|method|param):([\w]+)\]/gi, '[$1:$2 $2]' ); // [member:name] to [member:name title]
  46. text = text.replace( /\[(?:member|property|method):([\w]+) ([\w\.\s]+)\]\s*(\(.*\))?/gi, `<a class='permalink links' data-fragment='${name}.$2' target='_parent' title='${name}.$2'>#</a> .<a class='links' data-fragment='${name}.$2' id='$2'>$2</a> $3 : <a class='param links' data-fragment='$1'>$1</a>` );
  47. text = text.replace( /\[param:([\w\.]+) ([\w\.\s]+)\]/gi, '$2 : <a class=\'param links\' data-fragment=\'$1\'>$1</a>' ); // [param:name title]
  48. text = text.replace( /\[link:([\w\:\/\.\-\_\(\)\?\#\=\!\~]+)\]/gi, '<a href="$1" target="_blank">$1</a>' ); // [link:url]
  49. text = text.replace( /\[link:([\w:/.\-_()?#=!~]+) ([\w\p{L}:/.\-_'\s]+)\]/giu, '<a href="$1" target="_blank">$2</a>' ); // [link:url title]
  50. text = text.replace( /\*([\w\d\"\-\(][\w\d\ \/\+\-\(\)\=\,\."]*[\w\d\"\)]|\w)\*/gi, '<strong>$1</strong>' ); // *text*
  51. text = text.replace( /\`(.*?)\`/gi, '<code class="inline">$1</code>' ); // `code`
  52. text = text.replace( /\[example:([\w\_]+)\]/gi, '[example:$1 $1]' ); // [example:name] to [example:name title]
  53. text = text.replace( /\[example:([\w\_]+) ([\w\:\/\.\-\_ \s]+)\]/gi, '<a href="../examples/#$1" target="_blank">$2</a>' ); // [example:name title]
  54. text = text.replace( /<a class=\'param links\' data-fragment=\'\w+\'>(undefined|null|this|Boolean|Object|Array|Number|String|Integer|Float|TypedArray|ArrayBuffer)<\/a>/gi, '<span class="param">$1</span>' ); // remove links to primitive types
  55. document.body.innerHTML = text;
  56. if ( window.parent.getPageURL ) {
  57. const links = document.querySelectorAll( '.links' );
  58. for ( let i = 0; i < links.length; i ++ ) {
  59. const pageURL = window.parent.getPageURL( links[ i ].dataset.fragment );
  60. if ( pageURL ) {
  61. links[ i ].href = './index.html#' + pageURL;
  62. }
  63. }
  64. }
  65. document.body.addEventListener( 'click', event => {
  66. const element = event.target;
  67. if ( element.classList.contains( 'links' ) && event.button === 0 && ! event.shiftKey && ! event.ctrlKey && ! event.metaKey && ! event.altKey ) {
  68. window.parent.setUrlFragment( element.dataset.fragment );
  69. event.preventDefault();
  70. }
  71. } );
  72. // handle code snippets formatting
  73. const elements = document.getElementsByTagName( 'code' );
  74. for ( let i = 0; i < elements.length; i ++ ) {
  75. const element = elements[ i ];
  76. text = element.textContent.trim();
  77. text = text.replace( /^\t\t/gm, '' );
  78. element.textContent = text;
  79. }
  80. // Edit button
  81. const button = document.createElement( 'div' );
  82. button.id = 'button';
  83. button.innerHTML = '<img src="../files/ic_mode_edit_black_24dp.svg">';
  84. button.addEventListener( 'click', function () {
  85. window.open( 'https://github.com/mrdoob/three.js/blob/dev/docs/' + section + '/' + localizedPath + '.html' );
  86. }, false );
  87. document.body.appendChild( button );
  88. // Syntax highlighting
  89. const styleBase = document.createElement( 'link' );
  90. styleBase.href = pathname.substring( 0, pathname.indexOf( 'docs' ) + 4 ) + '/prettify/prettify.css';
  91. styleBase.rel = 'stylesheet';
  92. const styleCustom = document.createElement( 'link' );
  93. styleCustom.href = pathname.substring( 0, pathname.indexOf( 'docs' ) + 4 ) + '/prettify/threejs.css';
  94. styleCustom.rel = 'stylesheet';
  95. document.head.appendChild( styleBase );
  96. document.head.appendChild( styleCustom );
  97. const prettify = document.createElement( 'script' );
  98. prettify.src = pathname.substring( 0, pathname.indexOf( 'docs' ) + 4 ) + '/prettify/prettify.js';
  99. prettify.onload = function () {
  100. const elements = document.getElementsByTagName( 'code' );
  101. for ( let i = 0; i < elements.length; i ++ ) {
  102. const e = elements[ i ];
  103. e.className += ' prettyprint';
  104. e.setAttribute( 'translate', 'no' );
  105. }
  106. prettyPrint(); // eslint-disable-line no-undef
  107. };
  108. document.head.appendChild( prettify );
  109. }
  110. document.addEventListener( 'DOMContentLoaded', onDocumentLoad, false );