Skip to content

Commit c3ad14f

Browse files
committed
feat: Allow cutting the same initial part of the path from tested HTML files
1 parent fd56c61 commit c3ad14f

File tree

9 files changed

+1139
-24
lines changed

9 files changed

+1139
-24
lines changed

README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ A custom reporter for [grunt-html] - the HTML validation task - which formats th
1010

1111
You can use the reporter programmatically. In that case you do not need [Grunt] as stated below. You can also use the reporter directly with the [grunt-html] task. The reporter is usually installed and used together with other development tasks.
1212

13-
You need [node >= 4][node], [npm] and [grunt >= 0.4][Grunt] installed and your project build managed by a [Gruntfile] with the necessary modules listed in [package.json], including [grunt-html]. If you haven't used Grunt before, be sure to check out the [Getting Started] guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:
13+
You need [node >= 6][node], [npm] and [grunt >= 0.4][Grunt] installed and your project build managed by a [Gruntfile] with the necessary modules listed in [package.json], including [grunt-html]. If you haven't used Grunt before, be sure to check out the [Getting Started] guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:
1414

1515
```sh
1616
$ npm install grunt-html-html-reporter --save-dev
@@ -36,7 +36,13 @@ fs.writeFileSync('report.html', output, 'utf-8')
3636
Type: `Boolean`
3737
Default value: `false`
3838

39-
Cuts the directory from tested HTML files, when creating page titles from in the report. If you use unique names for files alone, you will not get too long page titles, if you flip this flag tp `true`.
39+
Cuts the directory from tested HTML files, when creating page titles of them in the report. If you use unique names for files alone, you will not get too long page titles, if you flip this flag tp `true`.
40+
41+
#### showCommonPathOnly
42+
Type: `Boolean`
43+
Default value: `true`
44+
45+
Cuts the same initial part of the path from tested HTML files, when the paths are used for page titles in the report. If you use sub-directories to organize your files, this option will make the file paths in the report short, but still unique. The default is flipped to `true` already.
4046

4147
## Usage with grunt-html
4248

@@ -61,6 +67,7 @@ In lieu of a formal styleguide, take care to maintain the existing coding style.
6167

6268
## Release History
6369

70+
* 2018-05-14 v3.1.0 Allow cutting the same initial part of the path from tested HTML files for displaying purposes
6471
* 2018-04-27 v3.0.0 Dropped support of Node.js 4
6572
* 2018-03-05 v2.2.0 Allow generating page titles from file names without directory
6673
* 2018-03-04 v2.1.0 Add filtering and accessibility to the reports

index.js

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
'use strict'
22

3-
const fs = require('fs')
4-
const path = require('path')
3+
const {getCommonPathLength} = require('common-path-start')
4+
const {readFileSync} = require('fs')
5+
const {basename, join, normalize} = require('path')
56

67
const objectValues = require('object.values')
78
if (!Object.values) {
89
objectValues.shim()
910
}
1011

1112
function formatIssues (issues, panelColor) {
12-
return issues.map(function (issue) {
13+
return issues.map(issue => {
1314
const extract = issue.extract.split('<').join('&lt;')
1415
const message = issue.message.split('<').join('&lt;').split('"').join('&quot;')
1516
const line = issue.line
@@ -54,22 +55,26 @@ function formatFile (file) {
5455
return content
5556
}
5657

57-
module.exports = function (results, options) {
58+
module.exports = (results, options) => {
5859
const showFileNameOnly = options && options.showFileNameOnly
60+
const showCommonPathOnly = !(options && options.showCommonPathOnly === false)
61+
const commonPathLength = showCommonPathOnly &&
62+
getCommonPathLength(results.map(result => normalize(result.file)))
5963
const files = {}
60-
var errorCount = 0
61-
var warningCount = 0
62-
var noticeCount = 0
64+
let errorCount = 0
65+
let warningCount = 0
66+
let noticeCount = 0
6367

64-
results.forEach(function (result) {
65-
const name = path.normalize(result.file)
68+
results.forEach(result => {
69+
const name = normalize(result.file)
6670
const severity = result.type
67-
var file = files[name]
68-
var fileName, issues
71+
let file = files[name]
72+
let fileName, issues
6973
if (!file) {
7074
if (showFileNameOnly) {
71-
fileName = path.parse(name)
72-
fileName = fileName.name + fileName.ext
75+
fileName = basename(name)
76+
} else if (commonPathLength) {
77+
fileName = name.substr(commonPathLength)
7378
} else {
7479
fileName = name
7580
}
@@ -102,8 +107,8 @@ module.exports = function (results, options) {
102107
})
103108
})
104109

105-
const template = fs.readFileSync(
106-
path.join(__dirname, 'template.html'), 'utf8')
110+
const template = readFileSync(
111+
join(__dirname, 'template.html'), 'utf8')
107112

108113
const buttonMarkup =
109114
'<button class="btn btn-sm btn-danger">Errors <span class="badge">' + errorCount + '</span></button>' +

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,14 @@
3434
"travis-deploy-once": "travis-deploy-once"
3535
},
3636
"dependencies": {
37+
"common-path-start": "^0.0.1",
3738
"object.values": "^1.0.4"
3839
},
3940
"devDependencies": {
40-
"semantic-release": "^15.1.7",
41+
"semantic-release": "^15.4.1",
4142
"standard": "^11.0.1",
42-
"tap": "^11.1.4",
43-
"travis-deploy-once": "^4.4.1"
43+
"tap": "^11.1.5",
44+
"travis-deploy-once": "^5.0.0"
4445
},
4546
"keywords": [
4647
"grunt-tasks",

0 commit comments

Comments
 (0)