Skip to content

Commit

Permalink
Advanced bg: Render the image alt text to sr-only <figcaption>
Browse files Browse the repository at this point in the history
  • Loading branch information
yhatt committed Sep 9, 2023
1 parent d03895d commit 33240f9
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/markdown/background_image/advanced.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ function _advancedBackground(md) {
{
tag: 'figure',
style: style.toString(),
open: {
meta: {
// For getting better alt text, we should store
// the reference of the original image token.
marpitBackgroundSource: img.source,
},
},
},
),
)
Expand Down Expand Up @@ -155,6 +162,34 @@ function _advancedBackground(md) {
state.tokens = newTokens
},
)

// Renderer for advanced background image
md.renderer.rules.marpit_advanced_background_image_open = (
tokens,
idx,
options,
env,
self,
) => {
const token = tokens[idx]
const open = self.renderToken(tokens, idx, options)

if (token.meta && token.meta.marpitBackgroundSource) {
// Try to render figcaption for background image
// (Image token after parsed has text children without Marpit keywords)
const figcaption = self
.renderInlineAsText(
token.meta.marpitBackgroundSource.children,
options,
env,
)
.trim()

if (figcaption) return `${open}<figcaption>${figcaption}</figcaption>`
}

return open
}
}

export const advancedBackground = marpitPlugin(_advancedBackground)
Expand Down
1 change: 1 addition & 0 deletions src/markdown/background_image/apply.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ function _backgroundImageApply(md) {
})(),
url,
width,
source: t,
},
]
}
Expand Down
12 changes: 12 additions & 0 deletions src/postcss/advanced_background.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@ section[data-marpit-advanced-background="background"] > div[data-marpit-advanced
margin: 0;
}
section[data-marpit-advanced-background="background"] > div[data-marpit-advanced-background-container] > figure > figcaption {
position: absolute;
border: 0;
clip: rect(0, 0, 0, 0);
height: 1px;
margin: -1px;
overflow: hidden;
padding: 0;
white-space: nowrap;
width: 1px;
}
section[data-marpit-advanced-background="content"],
section[data-marpit-advanced-background="pseudo"] {
background: transparent !important;
Expand Down
20 changes: 20 additions & 0 deletions test/markdown/background_image.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { load } from 'cheerio'
import dedent from 'dedent'
import MarkdownIt from 'markdown-it'
import { backgroundImage } from '../../src/markdown/background_image'
import { comment } from '../../src/markdown/comment'
Expand Down Expand Up @@ -205,6 +206,25 @@ describe('Marpit background image plugin', () => {
expect(figures.eq(1).attr('style')).toBe('background-image:url("B");')
})

it('renders alternative text as <figcaption>, without Marpit specific keywords', () => {
const $ = $load(
mdSVG().render(dedent`
![bg fit The background image](A)
![This is bg 20% w:40% xxxxx](B)
![ bg ](C)
`),
)
const figures = $('figure')

expect(figures.eq(0).is(':has(figcaption)')).toBe(true)
expect(figures.eq(0).find('figcaption').text()).toBe(
'The background image',
)
expect(figures.eq(1).is(':has(figcaption)')).toBe(true)
expect(figures.eq(1).find('figcaption').text()).toBe('This is xxxxx')
expect(figures.eq(2).is(':has(figcaption)')).toBe(false)
})

it('assigns background-size style with resizing keyword / scale', () => {
const markdown =
'![bg fit](A) ![bg 50%](B) ![bg w:40px](C) ![bg 10% h:20%](D)'
Expand Down

0 comments on commit 33240f9

Please sign in to comment.