Skip to content

Commit

Permalink
Merge pull request #10 from HiDeoo/hd-no-alt-image-dimensions
Browse files Browse the repository at this point in the history
  • Loading branch information
HiDeoo committed Mar 24, 2024
2 parents 7118857 + 6718971 commit 0385b40
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 3 deletions.
2 changes: 2 additions & 0 deletions fixtures/basics/External images with dimensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
![External image with a width|50](https://history-computer.com/ModernComputer/Basis/images/Engelbart.jpg)

![External image with a width and a height|50x200](https://history-computer.com/ModernComputer/Basis/images/Engelbart.jpg)

![100](https://history-computer.com/ModernComputer/Basis/images/Engelbart.jpg)
2 changes: 2 additions & 0 deletions fixtures/basics/Internal images with dimensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
![[An image.png|An image with a width|100]]

![[An image.png|An image with a width and a height|100x200]]

![[An image.png|125]]
7 changes: 4 additions & 3 deletions packages/starlight-obsidian/libs/remark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const commentReplacementRegex = /%%(?<comment>(?:(?!%%).)+)%%/gs
const wikilinkReplacementRegex = /!?\[\[(?<url>(?:(?![[\]|]).)+)(?:\|(?<maybeText>(?:(?![[\]]).)+))?]]/g
const tagReplacementRegex = /(?:^|\s)#(?<tag>[\w/-]+)/g
const calloutRegex = /^\[!(?<type>\w+)][+-]? ?(?<title>.*)$/
const imageSizeRegex = /^(?<altText>.*)\|(?:(?<widthOnly>\d+)|(?:(?<width>\d+)x(?<height>\d+)))$/
const imageSizeRegex = /^(?:(?<altText>.*)\|)?(?:(?<widthOnly>\d+)|(?:(?<width>\d+)x(?<height>\d+)))$/

const asideDelimiter = ':::'

Expand Down Expand Up @@ -534,6 +534,7 @@ function handleImagesWithSize(node: Image, context: VisitorContext, type: 'asset
return
}

const imgAltText = altText ?? ''
const imgWidth = widthOnly ?? width
const imgHeight = height ?? 'auto'
// Workaround Starlight `auto` height default style.
Expand All @@ -542,7 +543,7 @@ function handleImagesWithSize(node: Image, context: VisitorContext, type: 'asset
if (type === 'external') {
replaceNode(context, {
type: 'html',
value: `<img src="${node.url}" alt="${altText}" width="${imgWidth}" height="${imgHeight}"${imgStyle} />`,
value: `<img src="${node.url}" alt="${imgAltText}" width="${imgWidth}" height="${imgHeight}"${imgStyle} />`,
})
} else {
const importId = generateAssetImportId()
Expand All @@ -556,7 +557,7 @@ function handleImagesWithSize(node: Image, context: VisitorContext, type: 'asset
replaceNode(
context,
createMdxNode(
`<Image src={${importId}} alt="${altText}" width="${imgWidth}" height="${imgHeight}"${imgStyle} />`,
`<Image src={${importId}} alt="${imgAltText}" width="${imgWidth}" height="${imgHeight}"${imgStyle} />`,
),
)
}
Expand Down
3 changes: 3 additions & 0 deletions packages/starlight-obsidian/tests/embeds.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ test('transforms external images with dimensions', async () => {
<img src="https://history-computer.com/ModernComputer/Basis/images/Engelbart.jpg" alt="External image with a width" width="50" height="auto" />
<img src="https://history-computer.com/ModernComputer/Basis/images/Engelbart.jpg" alt="External image with a width and a height" width="50" height="200" style="height: 200px !important;" />
<img src="https://history-computer.com/ModernComputer/Basis/images/Engelbart.jpg" alt="" width="100" height="auto" />
"
`)
})
Expand All @@ -125,4 +127,5 @@ test('transforms internal images with dimensions', async () => {
expect(result.content).toMatch(
/^<Image src={\w+} alt="An image with a width and a height" width="100" height="200" style="height: 200px !important;" \/>$/m,
)
expect(result.content).toMatch(/^<Image src={\w+} alt="" width="125" height="auto" \/>$/m)
})

0 comments on commit 0385b40

Please sign in to comment.