Skip to content

Commit

Permalink
Take document as parameter instead document components
Browse files Browse the repository at this point in the history
  • Loading branch information
mjbvz committed Jan 2, 2019
1 parent df5a295 commit 7086fb7
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default class MarkdownFoldingProvider implements vscode.FoldingRangeProvi
(isStartRegion(token.content) || isEndRegion(token.content));


const tokens = await this.engine.parse(document.uri, document.getText());
const tokens = await this.engine.parse(document);
const regionMarkers = tokens.filter(isRegionMarker)
.map(token => ({ line: token.map[0], isStart: isStartRegion(token.content) }));

Expand Down Expand Up @@ -84,7 +84,7 @@ export default class MarkdownFoldingProvider implements vscode.FoldingRangeProvi
}
};

const tokens = await this.engine.parse(document.uri, document.getText());
const tokens = await this.engine.parse(document);
const multiLineListItems = tokens.filter(isFoldableToken);
return multiLineListItems.map(listItem => {
const start = listItem.map[0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export class MarkdownContentProvider {
const nonce = new Date().getTime() + '' + new Date().getMilliseconds();
const csp = this.getCspForResource(sourceUri, nonce);

const body = await this.engine.render(sourceUri, config.previewFrontMatter === 'hide', markdownDocument.getText());
const body = await this.engine.render(markdownDocument, config.previewFrontMatter === 'hide');
return `<!DOCTYPE html>
<html>
<head>
Expand Down
18 changes: 10 additions & 8 deletions extensions/markdown-language-features/src/markdownEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import * as crypto from 'crypto';
import { MarkdownIt, Token } from 'markdown-it';
import * as path from 'path';
import * as vscode from 'vscode';
import * as crypto from 'crypto';
import { MarkdownContributions } from './markdownExtensions';
import { Slugifier } from './slugify';
import { SkinnyTextDocument } from './tableOfContentsProvider';
import { getUriForLinkWithKnownExternalScheme } from './util/links';

const FrontMatterRegex = /^---\s*[^]*?(-{3}|\.{3})\s*/;
Expand Down Expand Up @@ -93,28 +94,29 @@ export class MarkdownEngine {
return { text, offset };
}

public async render(document: vscode.Uri, stripFrontmatter: boolean, text: string): Promise<string> {
public async render(document: SkinnyTextDocument, stripFrontmatter: boolean): Promise<string> {
let offset = 0;
let text = document.getText();
if (stripFrontmatter) {
const markdownContent = this.stripFrontmatter(text);
offset = markdownContent.offset;
text = markdownContent.text;
}
this.currentDocument = document;
this.currentDocument = document.uri;
this.firstLine = offset;
this._slugCount = new Map<string, number>();

const engine = await this.getEngine(document);
const engine = await this.getEngine(document.uri);
return engine.render(text);
}

public async parse(document: vscode.Uri, source: string): Promise<Token[]> {
public async parse(document: SkinnyTextDocument): Promise<Token[]> {
const UNICODE_NEWLINE_REGEX = /\u2028|\u2029/g;
const { text, offset } = this.stripFrontmatter(source);
this.currentDocument = document;
const { text, offset } = this.stripFrontmatter(document.getText());
this.currentDocument = document.uri;
this._slugCount = new Map<string, number>();

const engine = await this.getEngine(document);
const engine = await this.getEngine(document.uri);

return engine.parse(text.replace(UNICODE_NEWLINE_REGEX, ''), {}).map(token => {
if (token.map) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class TableOfContentsProvider {

private async buildToc(document: SkinnyTextDocument): Promise<TocEntry[]> {
const toc: TocEntry[] = [];
const tokens = await this.engine.parse(document.uri, document.getText());
const tokens = await this.engine.parse(document);

const slugCount = new Map<string, number>();

Expand Down

0 comments on commit 7086fb7

Please sign in to comment.