Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support YAML configuration #21

Merged
merged 2 commits into from
Sep 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,8 @@
},
"dependencies": {
"npm-module-path": "^2.0.2",
"remark": "^12.0.0"
"remark": "^12.0.0",
"yaml": "^1.10.0"
},
"scripts": {
"vscode:prepublish": "npm run compile",
Expand Down
1 change: 1 addition & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ async function runRemark(document: vscode.TextDocument, range: vscode.Range): Pr
api = api.use(plugin.package);
}
} catch (err) {
console.error(err);
errors.push({
name: plugin.name,
err
Expand Down
20 changes: 15 additions & 5 deletions src/test/suite/workspace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@ suite('Extension Test Suite', () => {
vscode.window.showInformationMessage('All tests done!');
});

test('parseWorkspaceConfig', async () => {
// The configPath looks overly convoluted since `__dirname` points to the directory of the resulting .js
// file inside the `out` directory and not this source .ts file.
const configPath = path.join(__dirname, '..', '..', '..', 'src', 'test', 'suite', 'json', '.remarkrc');
console.log('configPath', configPath);
const parseWorkspaceConfigTest = async (configPath : string) => {
const files = [ vscode.Uri.parse(configPath) ];
const config = await workspace.parseWorkspaceConfig(files);

Expand All @@ -47,5 +43,19 @@ suite('Extension Test Suite', () => {

expect(check.object(config['first-heading']), 'config["first-heading"]').to.be.true;
expect(config['first-heading'].heading, 'config["first-heading"].heading').to.be.eq('Hello, .remarkrc file!');
};

test('parseWorkspaceConfig(json)', async () => {
// The configPath looks overly convoluted since `__dirname` points to the directory of the resulting .js
// file inside the `out` directory and not this source .ts file.
const configPath = path.join(__dirname, '..', '..', '..', 'src', 'test', 'suite', 'json', '.remarkrc');
await parseWorkspaceConfigTest(configPath);
});

test('parseWorkspaceConfig(yaml)', async () => {
// The configPath looks overly convoluted since `__dirname` points to the directory of the resulting .js
// file inside the `out` directory and not this source .ts file.
const configPath = path.join(__dirname, '..', '..', '..', 'src', 'test', 'suite', 'yaml', '.remarkrc.yml');
await parseWorkspaceConfigTest(configPath);
});
});
9 changes: 9 additions & 0 deletions src/test/suite/yaml/.remarkrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
plugins:
- first-heading
- github
settings:
closeAtx: true
github:
repository: https://github.com/mrmlnc/vscode-remark
first-heading:
heading: Hello, .remarkrc file!
14 changes: 13 additions & 1 deletion src/utils/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import * as vscode from 'vscode';
import { fileRead } from './fs';
import * as yaml from 'yaml';

export async function getWorkspaceConfig() {
const files = await vscode.workspace.findFiles('**/*remarkrc*', '**/node_modules/**');
Expand All @@ -12,19 +13,30 @@ export async function parseWorkspaceConfig(files : vscode.Uri[]) {
if (files.length === 0) {
return null;
}
if (files[0].fsPath.endsWith('.js')) {

const fileName = files[0].fsPath;

if (fileName.endsWith('.js')) {
try {
return require(files[0].fsPath);
}
catch (err) {
console.error(err);
return 'SyntaxError';
}
}

const content = await fileRead(files[0].fsPath);

try {
if (fileName.endsWith('.yml') || fileName.endsWith('.yaml')) {
return yaml.parse(content);
}

return JSON.parse(content);
}
catch (err) {
console.error(err);
return 'SyntaxError';
}
}
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"target": "es6",
"outDir": "out",
"lib": [
"es6"
"es6",
"dom"
],
"sourceMap": true,
"rootDir": "src",
Expand Down