Skip to content

Commit aeff733

Browse files
committed
Remove support for indents
Previously, remark had information on where content started, so that syntax (such as block quote start markers) could be ignored. This is no longer available in remark, and thus it was removed from this project. This commit *does* fix support for cr and cr + lf line endings.
1 parent 5058d13 commit aeff733

File tree

3 files changed

+68
-59
lines changed

3 files changed

+68
-59
lines changed

index.js

+19-19
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,34 @@ var location = require('vfile-location')
44

55
module.exports = source
66

7+
var search = /\r?\n|\r/g
8+
79
function source(value, file) {
810
var doc = String(file)
911
var loc = location(file)
1012
var position = (value && value.position) || value || {}
11-
var start
13+
var startOffset = loc.toOffset(position.start)
14+
var endOffset = loc.toOffset(position.end)
15+
var results = []
16+
var match
1217
var end
13-
var indents
14-
var indent
15-
var lines
16-
var length
17-
var index
18-
19-
start = loc.toOffset(position.start)
20-
end = loc.toOffset(position.end)
21-
indents = position.indent || []
2218

23-
if (start === -1 || end === -1) {
19+
if (startOffset === -1 || endOffset === -1) {
2420
return null
2521
}
2622

27-
lines = doc.slice(start, end).split('\n')
28-
length = lines.length
29-
index = 0
30-
31-
while (++index < length) {
32-
indent = indents[index - 1]
33-
lines[index] = lines[index].slice(indent ? indent - 1 : 0)
23+
while (startOffset < endOffset) {
24+
search.lastIndex = startOffset
25+
match = search.exec(doc)
26+
end = match && match.index < endOffset ? match.index : endOffset
27+
results.push(doc.slice(startOffset, end))
28+
startOffset = end
29+
30+
if (match && match.index < endOffset) {
31+
startOffset += match[0].length
32+
results.push(match[0])
33+
}
3434
}
3535

36-
return lines.join('\n')
36+
return results.join('')
3737
}

readme.md

+4-5
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,14 @@ var tree = unified()
4040
.use(parse)
4141
.parse(file)
4242

43-
var list = tree.children[0].children[0]
44-
console.log(source(list, file))
43+
var strong = tree.children[0].children[0].children[0].children[0].children[0]
44+
console.log(source(strong, file))
4545
```
4646

4747
Now, running `node example` yields:
4848

4949
```markdown
50-
+ **[Hello](./example)**
51-
world.
50+
**[Hello](./example)**
5251
```
5352

5453
## API
@@ -70,7 +69,7 @@ See [`contributing.md` in `syntax-tree/.github`][contributing] for ways to get
7069
started.
7170
See [`support.md`][support] for ways to get help.
7271

73-
This project has a [Code of Conduct][coc].
72+
This project has a [code of conduct][coc].
7473
By interacting with this repository, organisation, or community you agree to
7574
abide by its terms.
7675

test.js

+45-35
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,64 @@
11
'use strict'
22

33
var test = require('tape')
4+
var vfile = require('vfile')
45
var remark = require('remark')
56
var source = require('.')
67

78
test('unist-util-source', function (t) {
8-
t.plan(10)
9+
var file = vfile('> + **[Hello](./example)**\n> world.')
10+
var node = remark().parse(file)
911

10-
remark()
11-
.use(function () {
12-
return transformer
13-
function transformer(tree, file) {
14-
var node = tree
12+
t.equal(source(node, file), '> + **[Hello](./example)**\n> world.', 'root')
1513

16-
t.equal(source(node, file), '> + **[Hello](./example)**\n> world.')
14+
node = node.children[0]
15+
t.equal(
16+
source(node, file),
17+
'> + **[Hello](./example)**\n> world.',
18+
'block quote'
19+
)
1720

18-
// Blockquote.
19-
node = tree.children[0]
20-
t.equal(source(node, file), '> + **[Hello](./example)**\n> world.')
21+
node = node.children[0]
22+
t.equal(source(node, file), '+ **[Hello](./example)**\n> world.', 'list')
2123

22-
// List.
23-
node = node.children[0]
24-
t.equal(source(node, file), '+ **[Hello](./example)**\nworld.')
24+
node = node.children[0]
25+
t.equal(source(node, file), '+ **[Hello](./example)**\n> world.', 'list item')
2526

26-
// List-item.
27-
node = node.children[0]
28-
t.equal(source(node, file), '+ **[Hello](./example)**\nworld.')
27+
node = node.children[0]
28+
t.equal(source(node, file), '**[Hello](./example)**\n> world.', 'paragraph')
2929

30-
// Paragraph.
31-
node = node.children[0]
32-
t.equal(source(node, file), '**[Hello](./example)**\nworld.')
30+
node = node.children[0]
31+
t.equal(source(node, file), '**[Hello](./example)**', 'strong')
3332

34-
// Strong.
35-
node = node.children[0]
36-
t.equal(source(node, file), '**[Hello](./example)**')
33+
node = node.children[0]
34+
t.equal(source(node, file), '[Hello](./example)', 'link')
3735

38-
// Link.
39-
node = node.children[0]
40-
t.equal(source(node, file), '[Hello](./example)')
36+
node = node.children[0]
37+
t.equal(source(node, file), 'Hello', 'text')
4138

42-
// Text.
43-
node = node.children[0]
44-
t.equal(source(node, file), 'Hello')
39+
t.equal(source({type: node.type, value: node.value}, file), null, 'generated')
4540

46-
// Generated.
47-
t.equal(source({type: node.type, value: node.value}, file), null)
41+
t.equal(source(null, file), null, 'missing')
4842

49-
// Missing.
50-
t.equal(source(null, file), null)
51-
}
52-
})
53-
.processSync(['> + **[Hello](./example)**', '> world.'].join('\n'))
43+
file = vfile('a\r\nb')
44+
node = remark().parse(file).children[0]
45+
46+
t.equal(source(node, file), 'a\r\nb', 'cr + lf')
47+
48+
file = vfile('a\rb')
49+
node = remark().parse(file).children[0]
50+
51+
t.equal(source(node, file), 'a\rb', 'cr')
52+
53+
file = vfile('a\n')
54+
node = remark().parse(file)
55+
56+
t.equal(source(node, file), 'a\n', 'eof eol')
57+
58+
file = vfile('a\n\rb')
59+
node = remark().parse(file)
60+
61+
t.equal(source(node, file), 'a\n\rb', 'blank lines')
62+
63+
t.end()
5464
})

0 commit comments

Comments
 (0)