Skip to content
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

Feat: add support for image title #244

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions lib/documents.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ function Image(options) {
return Buffer.from(arrayBuffer);
});
},
title: options.title,
altText: options.altText,
contentType: options.contentType
};
Expand Down
8 changes: 4 additions & 4 deletions lib/docx/body-reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -483,12 +483,11 @@ function BodyReader(options) {

function readBlip(element, blip) {
var properties = element.first("wp:docPr").attributes;
var altText = isBlank(properties.descr) ? properties.title : properties.descr;
var blipImageFile = findBlipImageFile(blip);
if (blipImageFile === null) {
return emptyResultWithMessages([warning("Could not find image file for a:blip element")]);
} else {
return readImage(blipImageFile, altText);
return readImage(blipImageFile, properties.descr, properties.title);
}
}

Expand Down Expand Up @@ -532,12 +531,13 @@ function BodyReader(options) {
};
}

function readImage(imageFile, altText) {
function readImage(imageFile, altText, title) {
var contentType = contentTypes.findContentType(imageFile.path);

var image = documents.Image({
readImage: imageFile.read,
altText: altText,
altText: isBlank(altText) ? title : altText,
title: title,
contentType: contentType
});
var warnings = supportedImageTypes[contentType] ?
Expand Down
11 changes: 8 additions & 3 deletions test/docx/body-reader.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1052,13 +1052,15 @@ test("when v:imagedata element has no relationship ID then it is ignored with wa
test("can read inline pictures", function() {
var drawing = createInlineImage({
blip: createEmbeddedBlip(IMAGE_RELATIONSHIP_ID),
description: "It's a hat"
description: "It's a hat",
title: "Sombrero"
});

var result = readEmbeddedImage(drawing);

return promiseThat(result, isSuccess(contains(isImage({
altText: "It's a hat",
title: "Sombrero",
contentType: "image/png",
buffer: IMAGE_BUFFER
}))));
Expand All @@ -1073,6 +1075,7 @@ test("alt text title is used if alt text description is missing", function() {
var result = readEmbeddedImage(drawing);

return promiseThat(result, isSuccess(contains(isImage({
title: "It's a hat",
altText: "It's a hat"
}))));
});
Expand All @@ -1087,7 +1090,8 @@ test("alt text title is used if alt text description is blank", function() {
var result = readEmbeddedImage(drawing);

return promiseThat(result, isSuccess(contains(isImage({
altText: "It's a hat"
altText: "It's a hat",
title: "It's a hat"
}))));
});

Expand All @@ -1101,7 +1105,8 @@ test("alt text description is preferred to alt text title", function() {
var result = readEmbeddedImage(drawing);

return promiseThat(result, isSuccess(contains(isImage({
altText: "It's a hat"
altText: "It's a hat",
title: "hat"
}))));
});

Expand Down