Skip to content

Commit

Permalink
feat: Pass all data attributes through to image block (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
davisagli authored Sep 6, 2023
1 parent 30f5e11 commit 1bc9aff
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/converters/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ const imageBlock = (elem, href) => {
];
}

if (elem.dataset.href != null) {
block.href = elem.dataset.href;
}

switch (alignFromClassName(elem.className)) {
case 'left':
block.align = 'left';
Expand Down Expand Up @@ -56,6 +52,12 @@ const imageBlock = (elem, href) => {
break;
}
}

// pass through data attributes to block data
for (const [k, v] of Object.entries(elem.dataset)) {
block[k] = v;
}

return block;
};

Expand Down
8 changes: 8 additions & 0 deletions src/converters/blocks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,12 @@ describe('imageBlock processing', () => {
'../resolveuid/7c6a1b0a0d2f40ffb6a4c73fd67b185d',
);
});
test('Image with data attributes', () => {
const elem = elementFromString(
'<img src="../resolveuid/7c6a1b0a0d2f40ffb6a4c73fd67b185d" title="A Picture" alt="Picture of a person" data-align="wide">',
);
const result = imageBlock(elem);
expect(result['@type']).toBe('image');
expect(result['align']).toBe('wide');
});
});

0 comments on commit 1bc9aff

Please sign in to comment.