1 | 'use strict';
|
2 |
|
3 | const fs = require('fs');
|
4 | const escape = require('escape-html');
|
5 | const pth = require('path');
|
6 | const {
|
7 | hasEngRusLetters,
|
8 | replaceEngLettersWithAsterisk,
|
9 | replaceRusLettersWithAsterisk,
|
10 | } = require('../helpers/string');
|
11 | const { isUrl } = require('../helpers/url');
|
12 | const { hasManyErrors, getTyposByCode } = require('../helpers/typos');
|
13 | const { uptime } = require('../helpers/uptime');
|
14 | const { consoleError, consoleInfo } = require('../helpers/console');
|
15 | const yaspeller = require('../yaspeller');
|
16 |
|
17 | const buffer = [];
|
18 |
|
19 | function prepareResource(resource) {
|
20 | let relativeFile = resource;
|
21 | if (!isUrl(resource)) {
|
22 | relativeFile = pth.relative('.', resource);
|
23 |
|
24 | if (relativeFile.indexOf('..') !== -1) {
|
25 | relativeFile = resource;
|
26 | }
|
27 | }
|
28 |
|
29 | return escape(relativeFile);
|
30 | }
|
31 |
|
32 | const filename = 'yaspeller_report.md';
|
33 |
|
34 | module.exports = {
|
35 | name: 'markdown',
|
36 | onResourceComplete(err, data) {
|
37 | const text = [];
|
38 | if (err) {
|
39 | text.push('**' + data + '**<br/><br/>\n');
|
40 | } else {
|
41 | if (hasManyErrors(data.data)) {
|
42 | text.push('**Too many errors.**<br/><br/>\n');
|
43 | }
|
44 |
|
45 | yaspeller.errors.forEach(function(el) {
|
46 | const typos = getTyposByCode(el.code, data.data);
|
47 | if (typos.length) {
|
48 | text.push('**' + el.title + '**\n\n' +
|
49 | '|Num.|Typo|Line:Col|Suggest|Comment|\n' +
|
50 | '|---:|----|--------|-------|-------|');
|
51 | typos.forEach(function(el, i) {
|
52 | const comment = [];
|
53 | const pos = el.position;
|
54 | const suggest = el.suggest ? el.suggest.map(word => '`' + escape(word) + '`').join(', ') : ' ';
|
55 |
|
56 | if (hasEngRusLetters(el.word)) {
|
57 | comment.push('`en: ' + escape(replaceRusLettersWithAsterisk(el.word)) + '`');
|
58 | comment.push('`ru: ' + escape(replaceEngLettersWithAsterisk(el.word)) + '`');
|
59 | }
|
60 |
|
61 | text.push([
|
62 | '',
|
63 | i + 1,
|
64 | '`' + escape(el.word) + '`' + (el.count > 1 ? ' <sup>' + el.count + '</sup>' : ''),
|
65 | pos.length ? pos[0].line + ':' + pos[0].column : ' ',
|
66 | suggest,
|
67 | comment.length ? comment.join('<br/>') : ' ',
|
68 | ''
|
69 | ].join('|'));
|
70 | });
|
71 | }
|
72 | });
|
73 |
|
74 | const time = data.time ? data.time + ' ms' : '';
|
75 | text.unshift((data.data && data.data.length ? '<br/>χ' : '✓') +
|
76 | ' `' + prepareResource(data.resource) + '` – ' + time + '<br/>\n');
|
77 | }
|
78 |
|
79 | buffer.push(text.join('\n'));
|
80 | },
|
81 | onComplete(data, stats) {
|
82 | const text = '<br/><br/>Processed resources: ' + stats.total +
|
83 | ' (χ – ' + stats.errors + ', ✓ – ' + stats.ok + ')<br/>' +
|
84 | 'Checking finished: ' + uptime() + '<br/><br/>\n' +
|
85 | buffer.join('');
|
86 |
|
87 | try {
|
88 | fs.writeFileSync(filename, text);
|
89 | consoleInfo(`Markdown report: ./${filename}`);
|
90 | } catch (e) {
|
91 | consoleError(e);
|
92 | }
|
93 | }
|
94 | };
|