Ver Fonte

Add handling of local vs external resolving

Steven Silvester há 8 anos atrás
pai
commit
dbef41e8c1

+ 5 - 2
src/docregistry/context.ts

@@ -373,14 +373,17 @@ class Context<T extends DocumentRegistry.IModel> implements DocumentRegistry.ICo
   /**
    * Resolve a relative url to a correct server path.
    */
-  resolveUrl(url: string): Promise<string> {
+  resolveUrl(url: string, local: boolean): Promise<string> {
     // Ignore urls that have a protocol.
     if (utils.urlParse(url).protocol || url.indexOf('//') === 0) {
       return Promise.resolve(url);
     }
     let cwd = ContentsManager.dirname(this._path);
     let path = ContentsManager.getAbsolutePath(url, cwd);
-    return Promise.resolve(path);
+    if (local) {
+      return Promise.resolve(path);
+    }
+    return this._manager.contents.getDownloadUrl(path);
   }
 
   /**

+ 6 - 1
src/docregistry/registry.ts

@@ -736,9 +736,14 @@ namespace DocumentRegistry {
     /**
      * Resolve a url to a correct server path.
      *
+     * @param url - The source url.
+     *
+     * @param local - Whether to to return the local path versus
+     *   a download path.
+     *
      * @returns a promise which resolves with the resolved url.
      */
-    resolveUrl(url: string): Promise<string>;
+    resolveUrl(url: string, local: boolean): Promise<string>;
 
     /**
      * Add a sibling widget to the document manager.

+ 4 - 3
src/renderers/widget.ts

@@ -352,7 +352,7 @@ function resolveUrls(node: HTMLElement, resolver: RenderMime.IResolver,
     let img = imgs[i];
     let source = img.getAttribute('src');
     if (source) {
-      promises.push(resolver.resolveUrl(source).then(url => {
+      promises.push(resolver.resolveUrl(source, false).then(url => {
         img.src = url;
         return void 0;
       }));
@@ -364,9 +364,10 @@ function resolveUrls(node: HTMLElement, resolver: RenderMime.IResolver,
     let href = anchor.getAttribute('href');
     anchor.target = '_blank';
     if (href) {
-      promises.push(resolver.resolveUrl(href).then(url => {
+      let local = linker && !utils.urlParse(href).protocol;
+      promises.push(resolver.resolveUrl(href, local).then(url => {
         anchor.href = url;
-        if (linker && !utils.urlParse(url).protocol) {
+        if (local) {
           linker.connectNode(anchor, CommandIDs.open, {
             path: url
           });

+ 8 - 1
src/rendermime/index.ts

@@ -389,7 +389,14 @@ namespace RenderMime {
   interface IResolver {
     /**
      * Resolve a url to a correct server path.
+     *
+     * @param url - The source url.
+     *
+     * @param local - Whether to to return the local path versus
+     *   a download path.
+     *
+     * @returns a promise which resolves with the resolved url.
      */
-    resolveUrl(url: string): Promise<string>;
+    resolveUrl(url: string, local: boolean): Promise<string>;
   }
 }