Skip to content

Commit

Permalink
#90, #98 option to not encode uri
Browse files Browse the repository at this point in the history
  • Loading branch information
Yu committed Feb 25, 2018
1 parent 06fc4f0 commit c37d2bd
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@
"default": true,
"description": "Auto update on save"
},
"markdown.extension.toc.encodeUri": {
"type": "boolean",
"default": true,
"description": "In VS Code preview, the anchors of headings are encoded. But if you want the generated TOC to work on GitHub etc., you might need to set this to `false`"
},
"markdown.extension.preview.autoShowPreviewToSide": {
"type": "boolean",
"default": false,
Expand Down
3 changes: 2 additions & 1 deletion src/print.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import * as vscode from 'vscode';
import * as path from 'path';
import * as fs from "fs";
import { slugify } from './util';

const officialExt = vscode.extensions.getExtension("Microsoft.vscode-markdown");

Expand All @@ -25,7 +26,7 @@ const md = require(path.join(officialExt.extensionPath, 'node_modules', 'markdow
return str;
}
}).use(mdnh, {
slugify: (header: string) => TocProvider.slugify(header)
slugify: (header: string) => slugify(header)
}).use(mdtl);

let thisContext: vscode.ExtensionContext;
Expand Down
4 changes: 2 additions & 2 deletions src/toc.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

import * as vscode from 'vscode';
import { log } from './util';
import { log, slugify } from './util';
import * as path from 'path';

const officialExt = vscode.extensions.getExtension("Microsoft.vscode-markdown");
Expand Down Expand Up @@ -115,7 +115,7 @@ async function generateTocText(document: vscode.TextDocument): Promise<string> {
let row = [
docConfig.tab.repeat(indentation),
(tocConfig.orderedList ? ++order[indentation] + '.' : tocConfig.listMarker) + ' ',
tocConfig.plaintext ? entryText : `[${entryText}](#${TocProvider.slugify(entryText)})`
tocConfig.plaintext ? entryText : `[${entryText}](#${slugify(entryText)})`
];
toc.push(row.join(''));
if (tocConfig.orderedList) order.fill(0, indentation + 1);
Expand Down
12 changes: 11 additions & 1 deletion src/util.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

import { Position, Range, TextDocument } from 'vscode';
import { Position, Range, TextDocument, workspace } from 'vscode';

export function log(msg: string, obj?) {
if (obj) {
Expand All @@ -25,3 +25,13 @@ export function log(msg: string, obj?) {
console.log(msg);
}
}

export function slugify(heading: string) {
let slug = heading.trim()
.toLowerCase()
.replace(/[\]\[\!\"\#\$\%\&\'\(\)\*\+\,\.\/\:\;\<\=\>\?\@\\\^\_\{\|\}\~\`]/g, '')
.replace(/\s+/g, '-')
.replace(/^\-+/, '')
.replace(/\-+$/, '');
return workspace.getConfiguration('markdown.extension.toc').get<boolean>('encodeUri') ? encodeURI(slug) : slug;
}

0 comments on commit c37d2bd

Please sign in to comment.