Explorar el Código

Fix handling of relative urls

Steven Silvester hace 8 años
padre
commit
b7b92c384c
Se han modificado 2 ficheros con 15 adiciones y 9 borrados
  1. 1 7
      packages/rendermime-extension/src/index.ts
  2. 14 2
      packages/rendermime/src/widgets.ts

+ 1 - 7
packages/rendermime-extension/src/index.ts

@@ -9,10 +9,6 @@ import {
   ICommandLinker
 } from '@jupyterlab/apputils';
 
-import {
-  URLExt
-} from '@jupyterlab/coreutils';
-
 import {
   IRenderMime, RenderMime
 } from '@jupyterlab/rendermime';
@@ -41,9 +37,7 @@ export default plugin;
 function activate(app: JupyterLab, linker: ICommandLinker): IRenderMime {
   let linkHandler = {
     handleLink: (node: HTMLElement, path: string) => {
-      if (!URLExt.parse(path).protocol && path.indexOf('//') !== 0) {
-        linker.connectNode(node, 'file-operations:open', { path });
-      }
+      linker.connectNode(node, 'file-operations:open', { path });
     }
   };
   let items = RenderMime.getDefaultItems();

+ 14 - 2
packages/rendermime/src/widgets.ts

@@ -432,17 +432,29 @@ namespace Private {
    */
   function handleAnchor(anchor: HTMLAnchorElement, resolver: RenderMime.IResolver, linkHandler: RenderMime.ILinkHandler | null): Promise<void> {
     anchor.target = '_blank';
+    // Get the link path without the location prepended.
+    // (e.g. "./foo.md#Header 1" vs "http://localhost:8888/foo.md#Header 1")
     let href = anchor.getAttribute('href');
-    if (!href) {
+    // Bail if it is not a file-like url.
+    if (!href || href.indexOf('://') !== -1 && href.indexOf('//') === 0) {
       return Promise.resolve(void 0);
     }
+    // Remove the hash until we can handle it.
+    let hash = anchor.hash;
+    if (hash) {
+      href = href.replace(hash, '');
+    }
+    // Get the appropriate file path.
     return resolver.resolveUrl(href).then(path => {
+      // Handle the click override.
       if (linkHandler) {
         linkHandler.handleLink(anchor, path);
       }
+      // Get the appropriate file download path.
       return resolver.getDownloadUrl(path);
     }).then(url => {
-      anchor.href = url;
+      // Set the visible anchor.
+      anchor.href = url + hash;
     });
   }
 }