Skip to content

Commit 5f9ac6b

Browse files
authored
fix: link's title should not to be string (#7)
[link](http://example.com "title") should be link
1 parent 19d31bc commit 5f9ac6b

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

src/StringSource.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,22 @@ export default class StringSource {
151151
return node.type === "Str";
152152
}
153153

154+
/**
155+
*
156+
* @param node
157+
* @returns {string|undefined}
158+
* @private
159+
*/
154160
_getValue(node) {
155161
if (node.value) {
156162
return node.value;
157163
} else if (node.alt) {
158164
return node.alt;
159165
} else if (node.title) {
166+
// See https://github.com/azu/textlint-rule-sentence-length/issues/6
167+
if (node.type === "Link") {
168+
return;
169+
}
160170
return node.title;
161171
}
162172
}
@@ -178,11 +188,11 @@ export default class StringSource {
178188
// [padding][value][padding]
179189
// =>
180190
// [value][value][value]
181-
let value = this._getValue(node);
191+
const value = this._getValue(node);
182192
if (!value) {
183193
return;
184194
}
185-
if (parent == null) {
195+
if (parent === null || parent === undefined) {
186196
return;
187197
}
188198
// <p><Str /></p>

test/StringSource-test.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,27 @@ describe("StringSource", function() {
9595
value: "link"
9696
});
9797
});
98+
99+
it("Link's title should be ignored", function() {
100+
let AST = parse('_[link](http://example "title")');
101+
let source = new StringSource(AST);
102+
let result = source.toString();
103+
assert.equal(result, "_link");
104+
let tokenStr = source.tokenMaps[0];
105+
assert.deepEqual(tokenStr, {
106+
generated: [0, 1],
107+
intermediate: [0, 1],
108+
original: [0, 1],
109+
value: "_"
110+
});
111+
let tokenLink = source.tokenMaps[1];
112+
assert.deepEqual(tokenLink, {
113+
generated: [1, 5],
114+
intermediate: [2, 6],
115+
original: [1, 31],
116+
value: "link"
117+
});
118+
});
98119
it("Str + `Code` + Str", function() {
99120
let AST = parse("text`code`text");
100121
let source = new StringSource(AST);
@@ -152,6 +173,25 @@ describe("StringSource", function() {
152173
value: " text"
153174
});
154175
});
176+
it("Image's title should be ignored", function() {
177+
let AST = parse('![alt](http://example.png "title") text');
178+
let source = new StringSource(AST);
179+
let result = source.toString();
180+
assert.equal(result, "alt text");
181+
assert.equal(source.tokenMaps.length, 2);
182+
assert.deepEqual(source.tokenMaps[0], {
183+
generated: [0, 3],
184+
intermediate: [2, 5],
185+
original: [0, 34],
186+
value: "alt"
187+
});
188+
assert.deepEqual(source.tokenMaps[1], {
189+
generated: [3, 8],
190+
intermediate: [34, 39],
191+
original: [34, 39],
192+
value: " text"
193+
});
194+
});
155195
it("confusing pattern", function() {
156196
let AST = parse("![!](http://example.com)");
157197
let source = new StringSource(AST);

0 commit comments

Comments
 (0)