當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript marked.marked函數代碼示例

本文整理匯總了TypeScript中vs/base/common/marked/marked.marked函數的典型用法代碼示例。如果您正苦於以下問題:TypeScript marked函數的具體用法?TypeScript marked怎麽用?TypeScript marked使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了marked函數的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。

示例1: renderMarkdown

export function renderMarkdown(markdown: IMarkdownString, options: RenderOptions = {}): HTMLElement {
	const element = createElement(options);

	// signal to code-block render that the
	// element has been created
	let signalInnerHTML: Function;
	const withInnerHTML = new Promise(c => signalInnerHTML = c);

	const renderer = new marked.Renderer();
	renderer.image = (href: string, title: string, text: string) => {
		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) {
			attributes.push(`src="${href}"`);
		}
		if (text) {
			attributes.push(`alt="${text}"`);
		}
		if (title) {
			attributes.push(`title="${title}"`);
		}
		if (dimensions.length) {
			attributes = attributes.concat(dimensions);
		}
		return '<img ' + attributes.join(' ') + '>';
	};
	renderer.link = (href, title, text): string => {
		// Remove markdown escapes. Workaround for https://github.com/chjj/marked/issues/829
		if (href === text) { // raw link case
			text = removeMarkdownEscapes(text);
		}
		title = removeMarkdownEscapes(title);
		href = removeMarkdownEscapes(href);
		if (
			!href
			|| href.match(/^data:|javascript:/i)
			|| (href.match(/^command:/i) && !markdown.isTrusted)
		) {
			// drop the link
			return text;

		} else {
			return `<a href="#" data-href="${href}" title="${title || href}">${text}</a>`;
		}
	};
	renderer.paragraph = (text): string => {
		return `<p>${text}</p>`;
	};

	if (options.codeBlockRenderer) {
		renderer.code = (code, lang) => {
			const value = options.codeBlockRenderer(lang, code);
			// when code-block rendering is async we return sync
			// but update the node with the real result later.
			const id = defaultGenerator.nextId();

			// {{SQL CARBON EDIT}} - Promise.all not returning the strValue properly in original code?
			const promise = value.then(strValue => {
				withInnerHTML.then(e => {
					const span = element.querySelector(`div[data-code="${id}"]`);
					if (span) {
						span.innerHTML = strValue;
					}
				}).catch(err => {
					// ignore
				});
			});

			// original VS Code source
			// const promise = Promise.all([value, withInnerHTML]).then(values => {
			// 	const strValue = values[0];
			// 	const span = element.querySelector(`div[data-code="${id}"]`);
			// 	if (span) {
			// 		span.innerHTML = strValue;
			// 	}
			// }).catch(err => {
			// 	// ignore
			// });

			if (options.codeBlockRenderCallback) {
				promise.then(options.codeBlockRenderCallback);
//.........這裏部分代碼省略.........
開發者ID:burhandodhy,項目名稱:azuredatastudio,代碼行數:101,代碼來源:htmlContentRenderer.ts

示例2: _renderHtml

function _renderHtml(content: IHTMLContentElement, options: RenderOptions = {}): Node {

	let {codeBlockRenderer, actionCallback} = options;

	if (content.isText) {
		return document.createTextNode(content.text);
	}

	var tagName = getSafeTagName(content.tagName) || 'div';
	var element = document.createElement(tagName);

	if (content.className) {
		element.className = content.className;
	}
	if (content.text) {
		element.textContent = content.text;
	}
	if (content.style) {
		element.setAttribute('style', content.style);
	}
	if (content.customStyle) {
		Object.keys(content.customStyle).forEach((key) => {
			element.style[key] = content.customStyle[key];
		});
	}
	if (content.children) {
		content.children.forEach((child) => {
			element.appendChild(renderHtml(child, options));
		});
	}
	if (content.formattedText) {
		renderFormattedText(element, parseFormattedText(content.formattedText), actionCallback);
	}

	if (content.code && codeBlockRenderer) {
		// this is sort of legacy given that we have full
		// support for markdown. Turn this into markdown
		// and continue
		let {language, value} = content.code;
		content.markdown = '```' + language + '\n' + value + '\n```';
	}
	if (content.markdown) {

		// signal to code-block render that the
		// element has been created
		let signalInnerHTML: Function;
		const withInnerHTML = new TPromise(c => signalInnerHTML = c);

		const renderer = new marked.Renderer();
		renderer.link = (href, title, text): string => {
			return `<a href="#" data-href="${href}" title="${title || text}">${text}</a>`;
		};
		renderer.paragraph = (text): string => {
			return `<div>${text}</div>`;
		};

		if (options.codeBlockRenderer) {
			renderer.code = (code, lang) => {
				let value = options.codeBlockRenderer(lang, code);
				if (typeof value === 'string') {
					return value;
				}

				if (TPromise.is(value)) {
					// when code-block rendering is async we return sync
					// but update the node with the real result later.
					const id = defaultGenerator.nextId();
					TPromise.join([value, withInnerHTML]).done(values => {
						let [value] = values;
						let span = element.querySelector(`span[data-code="${id}"]`);
						if (span) {
							span.innerHTML = value;
						}
					}, err => {
						// ignore
					});
					return `<span data-code="${id}">${code}</span>`;
				}

				return code;
			};
		}

		if (options.actionCallback) {
			DOM.addStandardDisposableListener(element, 'click', event => {
				if (event.target.tagName === 'A') {
					const href = event.target.dataset['href'];
					if (href) {
						options.actionCallback(href, event);
					}
				}
			});
		}

		element.innerHTML = marked(content.markdown, {
			sanitize: true,
			renderer
		});
		signalInnerHTML();
	}
//.........這裏部分代碼省略.........
開發者ID:Magicwalker,項目名稱:vscode,代碼行數:101,代碼來源:htmlContentRenderer.ts

示例3: _renderHtml

function _renderHtml(content: IHTMLContentElement, options: RenderOptions = {}): Node {

	let {codeBlockRenderer, actionCallback} = options;

	if (content.isText) {
		return document.createTextNode(content.text);
	}

	var tagName = getSafeTagName(content.tagName) || 'div';
	var element = document.createElement(tagName);

	if (content.className) {
		element.className = content.className;
	}
	if (content.text) {
		element.textContent = content.text;
	}
	if (content.style) {
		element.setAttribute('style', content.style);
	}
	if (content.customStyle) {
		Object.keys(content.customStyle).forEach((key) => {
			element.style[key] = content.customStyle[key];
		});
	}
	if (content.code && codeBlockRenderer) {
		let html = codeBlockRenderer(content.code.language, content.code.value);
		element.innerHTML = html;
	}
	if (content.children) {
		content.children.forEach((child) => {
			element.appendChild(renderHtml(child, options));
		});
	}
	if (content.formattedText) {
		renderFormattedText(element, parseFormattedText(content.formattedText), actionCallback);
	}
	if (content.markdown) {
		const renderer = new marked.Renderer();
		renderer.link = (href, title, text): string => {
			return `<a href="#" data-href="${href}" title="${title || text}">${text}</a>`;
		};
		renderer.paragraph = (text): string => {
			return `<div>${text}</div>`;
		};

		if (options.codeBlockRenderer) {
			renderer.code = (code, lang) => {
				return options.codeBlockRenderer(lang, code);
			};
		}

		if (options.actionCallback) {
			DOM.addStandardDisposableListener(element, 'click', event => {
				if (event.target.tagName === 'A') {
					const href = event.target.dataset['href'];
					if (href) {
						options.actionCallback(href, event);
					}
				}
			});
		}

		element.innerHTML = marked(content.markdown, {
			sanitize: true,
			renderer
		});
	}

	return element;
}
開發者ID:13572293130,項目名稱:vscode,代碼行數:71,代碼來源:htmlContentRenderer.ts

示例4: link

function link(href, title, text): string {
	return `<a href="#" data-href="${href}" title="${title || text}">${text}</a>`
}


export const value = {

	markdownToHtml(main: WorkerServer, resolve: Function, reject: Function, progress: Function, data: { source: string; highlight: boolean; }): void {

		function highlight(code: string, lang: string, callback?: (error: Error, result: string) => void) {
			main.request('highlight', { code, lang }).then(value => callback(void 0, value), err => callback(err, void 0));
		};

		const renderer = new marked.Renderer();
		renderer.link = link;

		marked(data.source, {
			gfm: true,
			sanitize: true,
			renderer,
			// highlight
		}, function(err, html) {
			if (err) {
				reject(err);
			} else {
				resolve(html);
			}
		});
	}
}
開發者ID:amamut,項目名稱:vscode,代碼行數:30,代碼來源:simpleMarkedWorker.ts

示例5: _renderHtml

function _renderHtml(content: IHTMLContentElement, options: RenderOptions = {}): Node {

	let {codeBlockRenderer, actionCallback} = options;

	if (content.isText) {
		return document.createTextNode(content.text);
	}

	var tagName = getSafeTagName(content.tagName) || 'div';
	var element = document.createElement(tagName);

	if (content.className) {
		element.className = content.className;
	}
	if (content.text) {
		element.textContent = content.text;
	}
	if (content.style) {
		element.setAttribute('style', content.style);
	}
	if (content.customStyle) {
		Object.keys(content.customStyle).forEach((key) => {
			element.style[key] = content.customStyle[key];
		});
	}
	if (content.children) {
		content.children.forEach((child) => {
			element.appendChild(renderHtml(child, options));
		});
	}
	if (content.formattedText) {
		renderFormattedText(element, parseFormattedText(content.formattedText), actionCallback);
	}

	if (content.code && codeBlockRenderer) {
		// this is sort of legacy given that we have full
		// support for markdown. Turn this into markdown
		// and continue
		let {language, value} = content.code;
		content.markdown = '```' + language + '\n' + value + '\n```';
	}
	if (content.markdown) {

		// signal to code-block render that the
		// element has been created
		let signalInnerHTML: Function;
		const withInnerHTML = new TPromise(c => signalInnerHTML = c);

		const renderer = new marked.Renderer();
		renderer.image = (href: string, title: string, text: string) => {
			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) {
				attributes.push(`src="${href}"`);
			}
			if (text) {
				attributes.push(`alt="${text}"`);
			}
			if (title) {
				attributes.push(`title="${title}"`);
			}
			if (dimensions.length) {
				attributes = attributes.concat(dimensions);
			}
			return '<img ' + attributes.join(' ') + '>';
		};
		renderer.link = (href, title, text): string => {
			// Remove markdown escapes. Workaround for https://github.com/chjj/marked/issues/829
			if (href === text) { // raw link case
				text = removeMarkdownEscapes(text);
			}
			title = removeMarkdownEscapes(title);
			href = removeMarkdownEscapes(href);
			return `<a href="#" data-href="${href}" title="${title || text}">${text}</a>`;
		};
		renderer.paragraph = (text): string => {
			return `<p>${text}</p>`;
		};

		if (options.codeBlockRenderer) {
			renderer.code = (code, lang) => {
//.........這裏部分代碼省略.........
開發者ID:diarmaidm,項目名稱:vscode,代碼行數:101,代碼來源:htmlContentRenderer.ts

示例6: renderMarkdown

export function renderMarkdown(markdown: string, options: RenderOptions = {}): Node {
	const element = createElement(options);

	const { codeBlockRenderer, actionCallback } = options;

	// signal to code-block render that the
	// element has been created
	let signalInnerHTML: Function;
	const withInnerHTML = new TPromise(c => signalInnerHTML = c);

	const renderer = new marked.Renderer();
	renderer.image = (href: string, title: string, text: string) => {
		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) {
			attributes.push(`src="${href}"`);
		}
		if (text) {
			attributes.push(`alt="${text}"`);
		}
		if (title) {
			attributes.push(`title="${title}"`);
		}
		if (dimensions.length) {
			attributes = attributes.concat(dimensions);
		}
		return '<img ' + attributes.join(' ') + '>';
	};
	renderer.link = (href, title, text): string => {
		// Remove markdown escapes. Workaround for https://github.com/chjj/marked/issues/829
		if (href === text) { // raw link case
			text = removeMarkdownEscapes(text);
		}
		title = removeMarkdownEscapes(title);
		href = removeMarkdownEscapes(href);
		if (!href || href.match(/^data:|javascript:/i)) {
			return text;
		}
		return `<a href="#" data-href="${href}" title="${title || text}">${text}</a>`;
	};
	renderer.paragraph = (text): string => {
		return `<p>${text}</p>`;
	};

	if (options.codeBlockRenderer) {
		renderer.code = (code, lang) => {
			const value = options.codeBlockRenderer(lang, code);
			if (typeof value === 'string') {
				return value;
			}

			if (TPromise.is(value)) {
				// when code-block rendering is async we return sync
				// but update the node with the real result later.
				const id = defaultGenerator.nextId();
				TPromise.join([value, withInnerHTML]).done(values => {
					const strValue = values[0] as string;
					const span = element.querySelector(`div[data-code="${id}"]`);
					if (span) {
						span.innerHTML = strValue;
					}
				}, err => {
					// ignore
				});
				return `<div class="code" data-code="${id}">${escape(code)}</div>`;
			}

			return code;
		};
	}

	if (options.actionCallback) {
		DOM.addStandardDisposableListener(element, 'click', event => {
			if (event.target.tagName === 'A') {
				const href = event.target.dataset['href'];
				if (href) {
					options.actionCallback(href, event);
				}
			}
		});
	}
//.........這裏部分代碼省略.........
開發者ID:Chan-PH,項目名稱:vscode,代碼行數:101,代碼來源:htmlContentRenderer.ts


注:本文中的vs/base/common/marked/marked.marked函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。