Skip to content

fix: link's title should not to be string #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/StringSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,22 @@ export default class StringSource {
return node.type === "Str";
}

/**
*
* @param node
* @returns {string|undefined}
* @private
*/
_getValue(node) {
if (node.value) {
return node.value;
} else if (node.alt) {
return node.alt;
} else if (node.title) {
// See https://github.com/azu/textlint-rule-sentence-length/issues/6
if (node.type === "Link") {
return;
}
return node.title;
}
}
Expand All @@ -178,11 +188,11 @@ export default class StringSource {
// [padding][value][padding]
// =>
// [value][value][value]
let value = this._getValue(node);
const value = this._getValue(node);
if (!value) {
return;
}
if (parent == null) {
if (parent === null || parent === undefined) {
return;
}
// <p><Str /></p>
Expand Down
40 changes: 40 additions & 0 deletions test/StringSource-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,27 @@ describe("StringSource", function() {
value: "link"
});
});

it("Link's title should be ignored", function() {
let AST = parse('_[link](http://example "title")');
let source = new StringSource(AST);
let result = source.toString();
assert.equal(result, "_link");
let tokenStr = source.tokenMaps[0];
assert.deepEqual(tokenStr, {
generated: [0, 1],
intermediate: [0, 1],
original: [0, 1],
value: "_"
});
let tokenLink = source.tokenMaps[1];
assert.deepEqual(tokenLink, {
generated: [1, 5],
intermediate: [2, 6],
original: [1, 31],
value: "link"
});
});
it("Str + `Code` + Str", function() {
let AST = parse("text`code`text");
let source = new StringSource(AST);
Expand Down Expand Up @@ -152,6 +173,25 @@ describe("StringSource", function() {
value: " text"
});
});
it("Image's title should be ignored", function() {
let AST = parse('![alt](http://example.png "title") text');
let source = new StringSource(AST);
let result = source.toString();
assert.equal(result, "alt text");
assert.equal(source.tokenMaps.length, 2);
assert.deepEqual(source.tokenMaps[0], {
generated: [0, 3],
intermediate: [2, 5],
original: [0, 34],
value: "alt"
});
assert.deepEqual(source.tokenMaps[1], {
generated: [3, 8],
intermediate: [34, 39],
original: [34, 39],
value: " text"
});
});
it("confusing pattern", function() {
let AST = parse("![!](http://example.com)");
let source = new StringSource(AST);
Expand Down