1
0

page.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. if ( ! window.frameElement && window.location.protocol !== 'file:' ) {
  2. // navigates to docs home if direct access, e.g.
  3. // https://mrdoob.github.io/three.js/docs/api/en/audio/Audio.html#filter
  4. // ->https://mrdoob.github.io/three.js/docs/#api/en/audio/Audio.filter
  5. const url = new URL( window.location.href );
  6. // hash route, e.g. #api/en/audio/Audio.filter
  7. url.hash = url.pathname.replace( /\/docs\/(.*?)(?:\.html)?$/, '$1' ) + url.hash.replace( '#', '.' );
  8. // docs home, e.g. https://mrdoob.github.io/three.js/docs/
  9. url.pathname = url.pathname.replace( /(\/docs\/).*$/, '$1' );
  10. window.location.replace( url );
  11. } else {
  12. document.addEventListener( 'DOMContentLoaded', onDocumentLoad, { once: true } );
  13. }
  14. function onDocumentLoad() {
  15. let path, localizedPath;
  16. const pathname = window.location.pathname;
  17. const section = /\/(manual|api|examples)\//.exec( pathname )[ 1 ].toString().split( '.html' )[ 0 ];
  18. let name = /[\-A-Za-z0-9]+\.html/.exec( pathname ).toString().split( '.html' )[ 0 ];
  19. switch ( section ) {
  20. case 'api':
  21. localizedPath = /\/api\/[A-Za-z0-9\/]+/.exec( pathname ).toString().slice( 5 );
  22. // Remove localized part of the path (e.g. 'en/' or 'es-MX/'):
  23. path = localizedPath.replace( /^[A-Za-z0-9-]+\//, '' );
  24. break;
  25. case 'examples':
  26. path = localizedPath = /\/examples\/[A-Za-z0-9\/]+/.exec( pathname ).toString().slice( 10 );
  27. break;
  28. case 'manual':
  29. name = name.replace( /\-/g, ' ' );
  30. path = pathname.replace( /\ /g, '-' );
  31. path = localizedPath = /\/manual\/[-A-Za-z0-9\/]+/.exec( path ).toString().slice( 8 );
  32. break;
  33. }
  34. let text = document.body.innerHTML;
  35. text = text.replace( /\[name\]/gi, name );
  36. text = text.replace( /\[path\]/gi, path );
  37. text = text.replace( /\[page:([\w\.]+)\]/gi, '[page:$1 $1]' ); // [page:name] to [page:name title]
  38. text = text.replace( /\[page:\.([\w\.]+) ([\w\.\s]+)\]/gi, `[page:${name}.$1 $2]` ); // [page:.member title] to [page:name.member title]
  39. text = text.replace( /\[page:([\w\.]+) ([\w\.\s]+)\]/gi, '<a class=\'links\' data-fragment=\'$1\' title=\'$1\'>$2</a>' ); // [page:name title]
  40. // text = text.replace( /\[member:.([\w]+) ([\w\.\s]+)\]/gi, "<a onclick=\"window.parent.setUrlFragment('" + name + ".$1')\" title=\"$1\">$2</a>" );
  41. text = text.replace( /\[(member|property|method|param):([\w]+)\]/gi, '[$1:$2 $2]' ); // [member:name] to [member:name title]
  42. text = text.replace( /\[(?:member|property|method):([\w]+) ([\w\.\s]+)\]\s*(\([\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>` );
  43. text = text.replace( /\[param:([\w\.]+) ([\w\.\s]+)\]/gi, '$2 : <a class=\'param links\' data-fragment=\'$1\'>$1</a>' ); // [param:name title]
  44. text = text.replace( /\[link:([\w\:\/\.\-\_\(\)\?\#\=\!\~]+)\]/gi, '<a href="$1" target="_blank">$1</a>' ); // [link:url]
  45. text = text.replace( /\[link:([\w:/.\-_()?#=!~]+) ([\w\p{L}:/.\-_'\s]+)\]/giu, '<a href="$1" target="_blank">$2</a>' ); // [link:url title]
  46. text = text.replace( /\*([\u4e00-\u9fa5\w\d\-\(\"\(\“][\u4e00-\u9fa5\w\d\ \/\+\-\(\)\=\,\.\(\)\,\。"]*[\u4e00-\u9fa5\w\d\"\)\”\)]|\w)\*/gi, '<strong>$1</strong>' ); // *text*
  47. text = text.replace( /\`(.*?)\`/gs, '<code class="inline">$1</code>' ); // `code`
  48. text = text.replace( /\[example:([\w\_]+)\]/gi, '[example:$1 $1]' ); // [example:name] to [example:name title]
  49. text = text.replace( /\[example:([\w\_]+) ([\w\:\/\.\-\_ \s]+)\]/gi, '<a href="../examples/#$1" target="_blank">$2</a>' ); // [example:name title]
  50. 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
  51. document.body.innerHTML = text;
  52. if ( window.parent.getPageURL ) {
  53. const links = document.querySelectorAll( '.links' );
  54. for ( let i = 0; i < links.length; i ++ ) {
  55. const pageURL = window.parent.getPageURL( links[ i ].dataset.fragment );
  56. if ( pageURL ) {
  57. links[ i ].href = './index.html#' + pageURL;
  58. }
  59. }
  60. }
  61. document.body.addEventListener( 'click', event => {
  62. const element = event.target;
  63. if ( element.classList.contains( 'links' ) && event.button === 0 && ! event.shiftKey && ! event.ctrlKey && ! event.metaKey && ! event.altKey ) {
  64. window.parent.setUrlFragment( element.dataset.fragment );
  65. event.preventDefault();
  66. }
  67. } );
  68. // handle code snippets formatting
  69. function dedent( text ) {
  70. // ignores singleline text
  71. const lines = text.split( '\n' );
  72. if ( lines.length <= 1 ) return text;
  73. // ignores blank text
  74. const nonBlankLine = lines.filter( l => l.trim() )[ 0 ];
  75. if ( nonBlankLine === undefined ) return text;
  76. // strips indents if any
  77. const m = nonBlankLine.match( /^([\t ]+)/ );
  78. if ( m ) text = lines.map( l => l.startsWith( m[ 1 ] ) ? l.substring( m[ 1 ].length ) : l ).join( '\n' );
  79. // strips leading and trailing whitespaces finally
  80. return text.trim();
  81. }
  82. const elements = document.getElementsByTagName( 'code' );
  83. for ( let i = 0; i < elements.length; i ++ ) {
  84. const element = elements[ i ];
  85. element.textContent = dedent( element.textContent );
  86. }
  87. // Edit button
  88. const button = document.createElement( 'div' );
  89. button.id = 'button';
  90. button.innerHTML = '<img src="../files/ic_mode_edit_black_24dp.svg">';
  91. button.addEventListener( 'click', function () {
  92. window.open( 'https://github.com/mrdoob/three.js/blob/dev/docs/' + section + '/' + localizedPath + '.html' );
  93. }, false );
  94. document.body.appendChild( button );
  95. // Syntax highlighting
  96. const styleBase = document.createElement( 'link' );
  97. styleBase.href = pathname.substring( 0, pathname.indexOf( 'docs' ) + 4 ) + '/prettify/prettify.css';
  98. styleBase.rel = 'stylesheet';
  99. const styleCustom = document.createElement( 'link' );
  100. styleCustom.href = pathname.substring( 0, pathname.indexOf( 'docs' ) + 4 ) + '/prettify/threejs.css';
  101. styleCustom.rel = 'stylesheet';
  102. document.head.appendChild( styleBase );
  103. document.head.appendChild( styleCustom );
  104. const prettify = document.createElement( 'script' );
  105. prettify.src = pathname.substring( 0, pathname.indexOf( 'docs' ) + 4 ) + '/prettify/prettify.js';
  106. prettify.onload = function () {
  107. const elements = document.getElementsByTagName( 'code' );
  108. for ( let i = 0; i < elements.length; i ++ ) {
  109. const e = elements[ i ];
  110. e.className += ' prettyprint';
  111. e.setAttribute( 'translate', 'no' );
  112. }
  113. prettyPrint(); // eslint-disable-line no-undef
  114. };
  115. document.head.appendChild( prettify );
  116. }