Skip to content

Commit 9f702fe

Browse files
committed
preserve scripts line number
1 parent b71a70a commit 9f702fe

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

lib/parser.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,38 @@ module.exports = function (content) {
7070

7171
var start = node.childNodes[0].__location.start
7272
var end = node.childNodes[node.childNodes.length - 1].__location.end
73+
74+
var result
75+
if (type === 'script') {
76+
result = commentScript(content.slice(0, start)) +
77+
content.slice(start, end) +
78+
commentScript(content.slice(end))
79+
} else {
80+
result = content.slice(start, end).trim()
81+
}
82+
7383
output[type].push({
7484
lang: lang,
7585
scoped: scoped,
76-
content: content.substring(start, end).trim()
86+
content: result
7787
})
7888
})
7989

8090
cb(null, 'module.exports = ' + JSON.stringify(output))
8191
}
8292

93+
function commentScript (content) {
94+
return content.split(/\n\r|\n|\r/g)
95+
.map(function (line) {
96+
if (line.trim() === '') {
97+
return line
98+
} else {
99+
return '// ' + line
100+
}
101+
})
102+
.join('\n')
103+
}
104+
83105
function getAttribute (node, name) {
84106
if (node.attrs) {
85107
var i = node.attrs.length

0 commit comments

Comments
 (0)