Skip to content

Commit

Permalink
Add textlint's transformers
Browse files Browse the repository at this point in the history
- Add markdown transformer
- Add txt transformer
  • Loading branch information
azu committed May 2, 2016
1 parent a63af85 commit 5f54377
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
export default function (context) {
console.log(context);
return {}
}
const {Syntax, RuleError, report, getSource} = context;
return {
[Syntax.Str](node){
report(node, new Error("error"));
}
};
}
73 changes: 46 additions & 27 deletions src/parsers/md/transformers/textlint-markdown-to-ast/index.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,52 @@
import compileModule from '../../../utils/compileModule';
import pkg from 'textlint/package.json';

const ID = 'textlint';
const ID = 'textlint:markdown';

function formatResults(results, code) {
const format = (message) => {
return formatResult(message, code);
};
return results.messages.length === 0
? 'Lint rule not fired.'
: results.messages.map(format).join('').trim();
}

function formatResult(result, code) {
const pointer = '-'.repeat(result.column - 1) + '^';
console.log(code.split('\n')[result.line - 1]);
return `
| ${result.message} (at ${result.line}:${result.column})
${code.split('\n')[result.line - 1] || ''}
| ${pointer}
`;
}

export default {
id: ID,
displayName: ID,
version: pkg.version,
homepage: pkg.homepage,

defaultParserID: 'textlint:markdown-to-ast',

loadTransformer(callback) {
require(['textlint/lib/textlint-core', 'babel-core'], (TextLintCore, babel) => {
callback({ TextLintCore, babel });
})
},

transform({TextLintCore, babel}, transformCode, code) {
console.log(TextLintCore);
const textlintCore = new TextLintCore();
let rule = compileModule( // eslint-disable-line no-shadow
babel.transform(transformCode).code
);
textlintCore.setupRules({
'astExplorerRule': rule
});
return textlintCore.lintText(code, ".md").then(result => {
return JSON.stringify(result);
});
}
id: ID,
displayName: ID,
version: pkg.version,
homepage: pkg.homepage,

defaultParserID: 'textlint:markdown-to-ast',

loadTransformer(callback) {
require(['textlint/lib/textlint-core', 'babel-core'], (TextLintCore, babel) => {
callback({TextLintCore, babel});
})
},

transform({TextLintCore, babel}, transformCode, code) {
const textlintCore = new TextLintCore();
let rule = compileModule( // eslint-disable-line no-shadow
babel.transform(transformCode).code
);
textlintCore.setupRules({
'astExplorerRule': rule,
});
return textlintCore.lintText(code, '.md').then(result => {
return formatResults(result, code);
});
},
};

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default function (context) {
const {Syntax, RuleError, report, getSource} = context;
return {
[Syntax.Str](node){
report(node, new Error("error"));
}
};
}
52 changes: 52 additions & 0 deletions src/parsers/txt/transformers/textlint-txt-to-ast/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import compileModule from '../../../utils/compileModule';
import pkg from '../../../../../node_modules/textlint/package.json';

const ID = 'textlint:txt';

function formatResults(results, code) {
const format = (message) => {
return formatResult(message, code);
};
return results.messages.length === 0
? 'Lint rule not fired.'
: results.messages.map(format).join('').trim();
}

function formatResult(result, code) {
const pointer = '-'.repeat(result.column - 1) + '^';
console.log(code.split('\n')[result.line - 1]);
return `
| ${result.message} (at ${result.line}:${result.column})
${code.split('\n')[result.line - 1] || ''}
| ${pointer}
`;
}

export default {
id: ID,
displayName: ID,
version: pkg.version,
homepage: pkg.homepage,

defaultParserID: 'textlint:txt-to-ast',

loadTransformer(callback) {
require(['../../../../../node_modules/textlint/lib/textlint-core', 'babel-core'], (TextLintCore, babel) => {
callback({TextLintCore, babel});
})
},

transform({TextLintCore, babel}, transformCode, code) {
const textlintCore = new TextLintCore();
let rule = compileModule( // eslint-disable-line no-shadow
babel.transform(transformCode).code
);
textlintCore.setupRules({
'astExplorerRule': rule,
});
return textlintCore.lintText(code, '.txt').then(result => {
return formatResults(result, code);
});
},
};

0 comments on commit 5f54377

Please sign in to comment.