Merge from vscode merge-base (#22780)

* Revert "Revert "Merge from vscode merge-base (#22769)" (#22779)"

This reverts commit 47a1745180.

* Fix notebook download task

* Remove done call from extensions-ci
This commit is contained in:
Karl Burtram
2023-04-19 21:48:46 -07:00
committed by GitHub
parent decbe8dded
commit e7d3d047ec
2389 changed files with 92155 additions and 42602 deletions

View File

@@ -8,8 +8,109 @@ import MarkdownIt from 'markdown-it';
import type * as MarkdownItToken from 'markdown-it/lib/token';
import type { ActivationFunction } from 'vscode-notebook-renderer';
const allowedHtmlTags = Object.freeze([
'a',
'b',
'blockquote',
'br',
'button',
'caption',
'center',
'code',
'col',
'colgroup',
'details',
'div',
'em',
'font',
'h1',
'h2',
'h3',
'h4',
'h5',
'h6',
'hr',
'i',
'img',
'input',
'kbd',
'label',
'li',
'ol',
'p',
'pre',
'select',
'small',
'span',
'strong',
'sub',
'summary',
'sup',
'table',
'tbody',
'td',
'textarea',
'tfoot',
'th',
'thead',
'tr',
'tt',
'u',
'ul',
'video',
]);
const allowedSvgTags = Object.freeze([
'svg',
'a',
'altglyph',
'altglyphdef',
'altglyphitem',
'animatecolor',
'animatemotion',
'animatetransform',
'circle',
'clippath',
'defs',
'desc',
'ellipse',
'filter',
'font',
'g',
'glyph',
'glyphref',
'hkern',
'image',
'line',
'lineargradient',
'marker',
'mask',
'metadata',
'mpath',
'path',
'pattern',
'polygon',
'polyline',
'radialgradient',
'rect',
'stop',
'style',
'switch',
'symbol',
'text',
'textpath',
'title',
'tref',
'tspan',
'view',
'vkern',
]);
const sanitizerOptions: DOMPurify.Config = {
ALLOWED_TAGS: ['a', 'button', 'blockquote', 'code', 'div', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'hr', 'img', 'input', 'label', 'li', 'p', 'pre', 'select', 'small', 'span', 'strong', 'textarea', 'ul', 'ol'],
ALLOWED_TAGS: [
...allowedHtmlTags,
...allowedSvgTags,
],
};
export const activate: ActivationFunction<void> = (ctx) => {
@@ -26,6 +127,7 @@ export const activate: ActivationFunction<void> = (ctx) => {
markdownIt.linkify.set({ fuzzyLink: false });
addNamedHeaderRendering(markdownIt);
addLinkRenderer(markdownIt);
const style = document.createElement('style');
style.textContent = `
@@ -206,7 +308,9 @@ export const activate: ActivationFunction<void> = (ctx) => {
previewNode.classList.remove('emptyMarkdownCell');
const markdownText = outputInfo.mime.startsWith('text/x-') ? `\`\`\`${outputInfo.mime.substr(7)}\n${text}\n\`\`\``
: (outputInfo.mime.startsWith('application/') ? `\`\`\`${outputInfo.mime.substr(12)}\n${text}\n\`\`\`` : text);
const unsanitizedRenderedMarkdown = markdownIt.render(markdownText);
const unsanitizedRenderedMarkdown = markdownIt.render(markdownText, {
outputItem: outputInfo,
});
previewNode.innerHTML = (ctx.workspace.isTrusted
? unsanitizedRenderedMarkdown
: DOMPurify.sanitize(unsanitizedRenderedMarkdown, sanitizerOptions)) as string;
@@ -225,12 +329,12 @@ function addNamedHeaderRendering(md: InstanceType<typeof MarkdownIt>): void {
const originalHeaderOpen = md.renderer.rules.heading_open;
md.renderer.rules.heading_open = (tokens: MarkdownItToken[], idx: number, options, env, self) => {
const title = tokens[idx + 1].children!.reduce<string>((acc, t) => acc + t.content, '');
let slug = slugFromHeading(title);
let slug = slugify(title);
if (slugCounter.has(slug)) {
const count = slugCounter.get(slug)!;
slugCounter.set(slug, count + 1);
slug = slugFromHeading(slug + '-' + (count + 1));
slug = slugify(slug + '-' + (count + 1));
} else {
slugCounter.set(slug, 0);
}
@@ -251,9 +355,26 @@ function addNamedHeaderRendering(md: InstanceType<typeof MarkdownIt>): void {
};
}
function slugFromHeading(heading: string): string {
function addLinkRenderer(md: MarkdownIt): void {
const original = md.renderer.rules.link_open;
md.renderer.rules.link_open = (tokens: MarkdownItToken[], idx: number, options, env, self) => {
const token = tokens[idx];
const href = token.attrGet('href');
if (typeof href === 'string' && href.startsWith('#')) {
token.attrSet('href', '#' + slugify(href.slice(1)));
}
if (original) {
return original(tokens, idx, options, env, self);
} else {
return self.renderToken(tokens, idx, options);
}
};
}
function slugify(text: string): string {
const slugifiedHeading = encodeURI(
heading.trim()
text.trim()
.toLowerCase()
.replace(/\s+/g, '-') // Replace whitespace with -
// allow-any-unicode-next-line