From 091197d9f53604bfa0c8cf859b5167673aa62ca9 Mon Sep 17 00:00:00 2001 From: isidor Date: Mon, 26 Oct 2015 11:28:56 +0100 Subject: [PATCH] util.massageMarkdownLinks with tests. --- src/test/assets/absoluteLinks1.md | 31 ++++++++ src/test/assets/absoluteLinks2.md | 119 ++++++++++++++++++++++++++++++ src/test/assets/relativeLinks1.md | 31 ++++++++ src/test/assets/relativeLinks2.md | 119 ++++++++++++++++++++++++++++++ src/test/package.test.ts | 22 ++++++ src/util.ts | 16 ++++ 6 files changed, 338 insertions(+) create mode 100644 src/test/assets/absoluteLinks1.md create mode 100644 src/test/assets/absoluteLinks2.md create mode 100644 src/test/assets/relativeLinks1.md create mode 100644 src/test/assets/relativeLinks2.md diff --git a/src/test/assets/absoluteLinks1.md b/src/test/assets/absoluteLinks1.md new file mode 100644 index 0000000..10543fd --- /dev/null +++ b/src/test/assets/absoluteLinks1.md @@ -0,0 +1,31 @@ +# VSCode Extension Manager + +This tool assists in publishing Visual Studio Code extensions. + +[**Documentation**](https://github.com/Microsoft/vscode-extensionbuilders/blob/master/docs/tools/vscecli.md) + +## Usage + +First, install using npm: + +``` +npm install -g vsce +``` + +Then, `cd` to your extension's directory. +It is good practice to list the files that will be included in your extension's +package, before you actually publish: + +``` +$ vsce ls +hello.js +package.json +``` + +Publish away: + +``` +$ vsce publish +Publishing uuid@0.0.1... +Successfully published uuid@0.0.1! +``` diff --git a/src/test/assets/absoluteLinks2.md b/src/test/assets/absoluteLinks2.md new file mode 100644 index 0000000..a64b173 --- /dev/null +++ b/src/test/assets/absoluteLinks2.md @@ -0,0 +1,119 @@ +# README + +>**Important:** Once installed the checker will only update if you add the setting `"spellMD.enable": true` to your `.vscode\settings.json` file. + +This README covers off: +* [Functionality](#functionality) +* [Install](#install) +* [Run and Configure](#run-and-configure) +* [Known Issues/Bugs](#known-issuesbugs) +* [Backlog](#backlog) +* [How to Debug](#how-to-debug) + +# Functionality + +Load up a Markdown file and get highlights and hovers for existing issues. Checking will occur as you type in the document. + +![Underscores and hovers](https://github.com/Microsoft/vscode-SpellMD/raw/master/images/SpellMDDemo1.gif) + +The status bar lets you quickly navigate to any issue and you can see all positions in the gutter. + +![Jump to issues](https://github.com/Microsoft/vscode-SpellMD/raw/master/images/SpellMDDemo2.gif) + +The `spellMD.json` config file is watched so you can add more ignores or change mappings at will. + +![Add to dictionary](https://github.com/Microsoft/vscode-SpellMD/raw/master/images/SpellMDDemo3.gif) + +# Install +This extension is published in the VS Code Gallery. So simply hit 'F1' and type 'ext inst' from there select `SpellMD` and follow instructions. + + +To clone the extension and load locally... + +``` +git clone https://github.com/Microsoft/vscode-SpellMD.git +npm install +tsc +``` + +>**Note:** TypeScript 1.6 or higher is required you can check with `tsc -v` and if you need to upgrade then run `npm install -g typescript`. + +Copy the extension folder into user settings. + +Depending on your platform, this folder is located here: +* **Windows** `%USERPROFILE%\.vscode\extensions` +* **Mac** `$HOME/.vscode/extensions` +* **Linux** `$HOME/.vscode/extensions` + +# Run and Configure + +## Enable via Config setting +Add the following setting to your WorkSpace [or User] settings: + +```json + "spellMD.enable": true, +``` + +## Open a Markdown file +Then open any Markdown file and BOOM. + +## Configure +The plug-in supports and watches a config file. This should go in the `.vscode` directory and needs to be called `spellMD.json`. This file has the following sections: +* **version** incase I change the format +* **ignoreWordsList** an array of strings that represents words not to check +* **mistakeTypeToStatus** we detect many error types and this is how they map to VS Code severities +* **replaceRegExp** this is an arry of RegExps represented as strings for pre-parsing the doc e.g. removing code blocks + +> **Tip:** you need to convert any `\` from the RegExp to a `\\\\` sequence for the JSON to parse. + +Here is an example file... + +```json +{ + "version": "0.1.0", + "ignoreWordsList": [ + "IntelliSense", "project.json", "nodejs", "transpiled", "ASPNET" + ], + "mistakeTypeToStatus": { + "Passive voice": "Info", + "Spelling": "Error", + "Complex Expression": "Info", + "Hidden Verbs": "Info", + "Hyphen Required": "Error", + "Did you mean...": "Info", + "Repeated Word": "Error", + "Missing apostrophe": "Error", + "Redundant Expression": "Info", + "Cliches": "Warn", + "Missing Word": "Warn", + "Make I uppercase": "Error" + }, + "replaceRegExp": [ + "/^((`{3}\\\\s*)(\\\\w+)?(\\\\s*([\\\\w\\\\W]+?)\\\\n*)\\\\2)\\\\n*(?:[^\\\\S\\\\w\\\\s]|$)/gm", + "/\\\\]\\\\(([^\\\\)]+)\\\\)/g" + ] +} +``` + +# Backlog + +Here are some ideas - fell free to add more. + +1. Let the user act on the suggestions e.g. a right-click or `Alt+S` opens command palette with suggestions, selection replaces. +2. Include the Text Tools extension w/ this i.e. WordCount, HTMLEncode etc +3. On project open check every file in the background + 1. Have an `excludeFilesList` in the options + 2. Suppress some types of issue completely i.e. don't report `Cliches` less noise in the list +4. Provide an action to add a word to the dictionary e.g. `Alt+A` + 1. Automatically create a spellMD.json file when a user adds a word + 2. When adding a word also add plurals/sentence case etc + + +# Debug This Code +Run this command in the directory w/ markdown files to check. + +``` +code --debugLanguageWorker=* --extensionDevelopmentPath="c:\src\vscode-SpellMD" . +``` + +Then open `VS Code` in the project directory and `Attach` to the running process. \ No newline at end of file diff --git a/src/test/assets/relativeLinks1.md b/src/test/assets/relativeLinks1.md new file mode 100644 index 0000000..a72ce90 --- /dev/null +++ b/src/test/assets/relativeLinks1.md @@ -0,0 +1,31 @@ +# VSCode Extension Manager + +This tool assists in publishing Visual Studio Code extensions. + +[**Documentation**](vscode-extensionbuilders/blob/master/docs/tools/vscecli.md) + +## Usage + +First, install using npm: + +``` +npm install -g vsce +``` + +Then, `cd` to your extension's directory. +It is good practice to list the files that will be included in your extension's +package, before you actually publish: + +``` +$ vsce ls +hello.js +package.json +``` + +Publish away: + +``` +$ vsce publish +Publishing uuid@0.0.1... +Successfully published uuid@0.0.1! +``` diff --git a/src/test/assets/relativeLinks2.md b/src/test/assets/relativeLinks2.md new file mode 100644 index 0000000..20cde63 --- /dev/null +++ b/src/test/assets/relativeLinks2.md @@ -0,0 +1,119 @@ +# README + +>**Important:** Once installed the checker will only update if you add the setting `"spellMD.enable": true` to your `.vscode\settings.json` file. + +This README covers off: +* [Functionality](#functionality) +* [Install](#install) +* [Run and Configure](#run-and-configure) +* [Known Issues/Bugs](#known-issuesbugs) +* [Backlog](#backlog) +* [How to Debug](#how-to-debug) + +# Functionality + +Load up a Markdown file and get highlights and hovers for existing issues. Checking will occur as you type in the document. + +![Underscores and hovers](/images/SpellMDDemo1.gif) + +The status bar lets you quickly navigate to any issue and you can see all positions in the gutter. + +![Jump to issues](/images/SpellMDDemo2.gif) + +The `spellMD.json` config file is watched so you can add more ignores or change mappings at will. + +![Add to dictionary](/images/SpellMDDemo3.gif) + +# Install +This extension is published in the VS Code Gallery. So simply hit 'F1' and type 'ext inst' from there select `SpellMD` and follow instructions. + + +To clone the extension and load locally... + +``` +git clone https://github.com/Microsoft/vscode-SpellMD.git +npm install +tsc +``` + +>**Note:** TypeScript 1.6 or higher is required you can check with `tsc -v` and if you need to upgrade then run `npm install -g typescript`. + +Copy the extension folder into user settings. + +Depending on your platform, this folder is located here: +* **Windows** `%USERPROFILE%\.vscode\extensions` +* **Mac** `$HOME/.vscode/extensions` +* **Linux** `$HOME/.vscode/extensions` + +# Run and Configure + +## Enable via Config setting +Add the following setting to your WorkSpace [or User] settings: + +```json + "spellMD.enable": true, +``` + +## Open a Markdown file +Then open any Markdown file and BOOM. + +## Configure +The plug-in supports and watches a config file. This should go in the `.vscode` directory and needs to be called `spellMD.json`. This file has the following sections: +* **version** incase I change the format +* **ignoreWordsList** an array of strings that represents words not to check +* **mistakeTypeToStatus** we detect many error types and this is how they map to VS Code severities +* **replaceRegExp** this is an arry of RegExps represented as strings for pre-parsing the doc e.g. removing code blocks + +> **Tip:** you need to convert any `\` from the RegExp to a `\\\\` sequence for the JSON to parse. + +Here is an example file... + +```json +{ + "version": "0.1.0", + "ignoreWordsList": [ + "IntelliSense", "project.json", "nodejs", "transpiled", "ASPNET" + ], + "mistakeTypeToStatus": { + "Passive voice": "Info", + "Spelling": "Error", + "Complex Expression": "Info", + "Hidden Verbs": "Info", + "Hyphen Required": "Error", + "Did you mean...": "Info", + "Repeated Word": "Error", + "Missing apostrophe": "Error", + "Redundant Expression": "Info", + "Cliches": "Warn", + "Missing Word": "Warn", + "Make I uppercase": "Error" + }, + "replaceRegExp": [ + "/^((`{3}\\\\s*)(\\\\w+)?(\\\\s*([\\\\w\\\\W]+?)\\\\n*)\\\\2)\\\\n*(?:[^\\\\S\\\\w\\\\s]|$)/gm", + "/\\\\]\\\\(([^\\\\)]+)\\\\)/g" + ] +} +``` + +# Backlog + +Here are some ideas - fell free to add more. + +1. Let the user act on the suggestions e.g. a right-click or `Alt+S` opens command palette with suggestions, selection replaces. +2. Include the Text Tools extension w/ this i.e. WordCount, HTMLEncode etc +3. On project open check every file in the background + 1. Have an `excludeFilesList` in the options + 2. Suppress some types of issue completely i.e. don't report `Cliches` less noise in the list +4. Provide an action to add a word to the dictionary e.g. `Alt+A` + 1. Automatically create a spellMD.json file when a user adds a word + 2. When adding a word also add plurals/sentence case etc + + +# Debug This Code +Run this command in the directory w/ markdown files to check. + +``` +code --debugLanguageWorker=* --extensionDevelopmentPath="c:\src\vscode-SpellMD" . +``` + +Then open `VS Code` in the project directory and `Attach` to the running process. \ No newline at end of file diff --git a/src/test/package.test.ts b/src/test/package.test.ts index f26ebda..50029fe 100644 --- a/src/test/package.test.ts +++ b/src/test/package.test.ts @@ -4,7 +4,9 @@ import * as fs from 'fs'; import * as assert from 'assert'; import { parseString } from 'xml2js'; import * as denodeify from 'denodeify'; +import * as util from '../util'; +const readFile = denodeify(fs.readFile); const parseXml = denodeify(parseString); const fixture = name => path.join(__dirname, 'fixtures', name); @@ -308,4 +310,24 @@ describe('toContentTypes', () => { assert.ok(!result.Types.Default.some(d => d.$.Extension === '')); }); }); +}); + +describe('readmeMassaging', () => { + it('should prepand links', () => { + return util.massageMarkdownLinks(path.join(process.cwd(), '/src/test/assets/relativeLinks1.md'), 'https://github.com/Microsoft/') + .then(result => readFile(path.join(process.cwd(), '/src/test/assets/absoluteLinks1.md'), 'utf-8') + .then(expected => { + assert.equal(result, expected); + }) + ); + }); + + it('should prepand links 2', () => { + return util.massageMarkdownLinks(path.join(process.cwd(), '/src/test/assets/relativeLinks2.md'), 'https://github.com/Microsoft/vscode-SpellMD/raw/master/') + .then(result => readFile(path.join(process.cwd(), '/src/test/assets/absoluteLinks2.md'), 'utf-8') + .then(expected => { + assert.equal(result, expected); + }) + ); + }); }); \ No newline at end of file diff --git a/src/util.ts b/src/util.ts index 155aba6..af9ef55 100644 --- a/src/util.ts +++ b/src/util.ts @@ -1,8 +1,13 @@ import { assign } from 'lodash'; import * as _read from 'read'; +import * as fs from 'fs'; +import * as path from 'path'; import { WebApi, getBasicHandler } from 'vso-node-api/WebApi'; import { IGalleryApi, IQGalleryApi } from 'vso-node-api/GalleryApi'; import * as denodeify from 'denodeify'; +import urljoin = require('url-join'); + +const readFile = denodeify(fs.readFile); export function fatal(message: any, ...args: any[]): Promise { if (message instanceof Error) { @@ -42,3 +47,14 @@ export function getRawGalleryAPI(pat: string): IGalleryApi { export function normalize(path: string): string { return path.replace(/\\/g, '/'); } + +export function massageMarkdownLinks(pathToMarkdown: string, prefix: string): Promise { + return readFile(pathToMarkdown, 'utf8').then(markdown => markdown.replace(/\[[^\[]+\]\(([^\)]+)\)/g, (titleAndLink, link) => + titleAndLink.replace(link, prepandRelativeLink(link, prefix)) + )); +} + +function prepandRelativeLink(link: string, prefix: string): string { + // Prepand only relative links, also ignore links to the sections in markdown (they contain #). + return /^(?:\w+:)\/\//.test(link) || link.indexOf('#') !== -1 ? link : urljoin(prefix, link); +}