Skip to content

Commit

Permalink
improve image source extraction for remote cases, kisstkondoros/gutte…
Browse files Browse the repository at this point in the history
  • Loading branch information
jrieken committed Jul 11, 2019
1 parent ff703d8 commit e9d8aaa
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 23 deletions.
24 changes: 3 additions & 21 deletions src/vs/base/browser/htmlContentRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import * as DOM from 'vs/base/browser/dom';
import { defaultGenerator } from 'vs/base/common/idGenerator';
import { escape } from 'vs/base/common/strings';
import { removeMarkdownEscapes, IMarkdownString } from 'vs/base/common/htmlContent';
import { removeMarkdownEscapes, IMarkdownString, parseHrefAndDimensions } from 'vs/base/common/htmlContent';
import * as marked from 'vs/base/common/marked/marked';
import { IMouseEvent } from 'vs/base/browser/mouseEvent';
import { DisposableStore } from 'vs/base/common/lifecycle';
Expand Down Expand Up @@ -100,29 +100,11 @@ export function renderMarkdown(markdown: IMarkdownString, options: RenderOptions

const renderer = new marked.Renderer();
renderer.image = (href: string, title: string, text: string) => {
href = _href(href, true);
let dimensions: string[] = [];
if (href) {
const splitted = href.split('|').map(s => s.trim());
href = splitted[0];
const parameters = splitted[1];
if (parameters) {
const heightFromParams = /height=(\d+)/.exec(parameters);
const widthFromParams = /width=(\d+)/.exec(parameters);
const height = heightFromParams ? heightFromParams[1] : '';
const width = widthFromParams ? widthFromParams[1] : '';
const widthIsFinite = isFinite(parseInt(width));
const heightIsFinite = isFinite(parseInt(height));
if (widthIsFinite) {
dimensions.push(`width="${width}"`);
}
if (heightIsFinite) {
dimensions.push(`height="${height}"`);
}
}
}
let attributes: string[] = [];
if (href) {
({ href, dimensions } = parseHrefAndDimensions(href));
href = _href(href, true);
attributes.push(`src="${href}"`);
}
if (text) {
Expand Down
22 changes: 22 additions & 0 deletions src/vs/base/common/htmlContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,25 @@ export function removeMarkdownEscapes(text: string): string {
}
return text.replace(/\\([\\`*_{}[\]()#+\-.!])/g, '$1');
}

export function parseHrefAndDimensions(href: string): { href: string, dimensions: string[] } {
const dimensions: string[] = [];
const splitted = href.split('|').map(s => s.trim());
href = splitted[0];
const parameters = splitted[1];
if (parameters) {
const heightFromParams = /height=(\d+)/.exec(parameters);
const widthFromParams = /width=(\d+)/.exec(parameters);
const height = heightFromParams ? heightFromParams[1] : '';
const width = widthFromParams ? widthFromParams[1] : '';
const widthIsFinite = isFinite(parseInt(width));
const heightIsFinite = isFinite(parseInt(height));
if (widthIsFinite) {
dimensions.push(`width="${width}"`);
}
if (heightIsFinite) {
dimensions.push(`height="${height}"`);
}
}
return { href, dimensions };
}
7 changes: 5 additions & 2 deletions src/vs/workbench/api/common/extHostTypeConverters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,7 @@ export namespace MarkdownString {
const resUris: { [href: string]: UriComponents } = Object.create(null);
res.uris = resUris;

const renderer = new marked.Renderer();
renderer.image = renderer.link = (href: string): string => {
const collectUri = (href: string): string => {
try {
let uri = URI.parse(href, true);
uri = uri.with({ query: _uriMassage(uri.query, resUris) });
Expand All @@ -248,6 +247,10 @@ export namespace MarkdownString {
}
return '';
};
const renderer = new marked.Renderer();
renderer.link = collectUri;
renderer.image = href => collectUri(htmlContent.parseHrefAndDimensions(href).href);

marked(res.value, { renderer });

return res;
Expand Down

0 comments on commit e9d8aaa

Please sign in to comment.