Skip to content

Commit

Permalink
Add anchor on hyperlinks as fragment if present
Browse files Browse the repository at this point in the history
  • Loading branch information
mwilliamson committed May 18, 2017
1 parent 327b998 commit 9aae75f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

* Extract the default data URI image converter to the images module.

* Add anchor on hyperlinks as fragment if present.

# 1.3.6

* Handle complex fields that do not have a "separate" fldChar.
Expand Down
5 changes: 5 additions & 0 deletions lib/docx/body-reader.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
exports.createBodyReader = createBodyReader;
exports._readNumberingProperties = readNumberingProperties;

var url = require("url");

var _ = require("underscore");

var documents = require("../documents");
Expand Down Expand Up @@ -210,6 +212,9 @@ function BodyReader(options) {
return readXmlElements(element.children).map(function(children) {
if (relationshipId) {
var href = relationships[relationshipId].target;
if (anchor) {
href = url.resolve(href, "#" + anchor);
}
return new documents.Hyperlink(children, {href: href});
} else if (anchor) {
return new documents.Hyperlink(children, {anchor: anchor});
Expand Down
15 changes: 13 additions & 2 deletions test/docx/body-reader.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -878,7 +878,7 @@ function assertChildrenAreConvertedNormally(tagName) {
assert.deepEqual(result.value[0].type, "run");
}

test("w:hyperlink is read as document hyperlink if it has a relationship ID", function() {
test("w:hyperlink is read as external hyperlink if it has a relationship ID", function() {
var runXml = new XmlElement("w:r", {}, []);
var hyperlinkXml = new XmlElement("w:hyperlink", {"r:id": "r42"}, [runXml]);
var relationships = {
Expand All @@ -889,7 +889,18 @@ test("w:hyperlink is read as document hyperlink if it has a relationship ID", fu
assert.deepEqual(result.value.children[0].type, "run");
});

test("w:hyperlink is read as document hyperlink if it has an anchor", function() {
test("w:hyperlink is read as external hyperlink if it has a relationship ID and an anchor", function() {
var runXml = new XmlElement("w:r", {}, []);
var hyperlinkXml = new XmlElement("w:hyperlink", {"r:id": "r42", "w:anchor": "fragment"}, [runXml]);
var relationships = {
"r42": {target: "http://example.com/"}
};
var result = readXmlElement(hyperlinkXml, {relationships: relationships});
assert.deepEqual(result.value.href, "http://example.com/#fragment");
assert.deepEqual(result.value.children[0].type, "run");
});

test("w:hyperlink is read as internal hyperlink if it has an anchor", function() {
var runXml = new XmlElement("w:r", {}, []);
var hyperlinkXml = new XmlElement("w:hyperlink", {"w:anchor": "_Peter"}, [runXml]);
var result = readXmlElement(hyperlinkXml);
Expand Down

0 comments on commit 9aae75f

Please sign in to comment.