Skip to content

Commit

Permalink
Add alt handling
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Nov 15, 2024
1 parent fbd9dee commit c07be31
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 8 deletions.
4 changes: 1 addition & 3 deletions packages/break/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@ test('fixtures', async function (t) {
// We could do this, if we operated in the parser.
// To do: consider `micromark-extension-github-break`?
.replace(/(<p>Character)<br>(\nreference break\.<\/p>)/, '$1$2')
// GitHub removes line endings in `alt` on image it seems?
// To do: investigate.
// CommonMark doesn’t seem to do that.
// See `rehype-github-image` — GH removes line endings in `alt` on image.
.replace(/(alt="im)\n(age")/, '$1 $2')
// GitHub drops HTML comments.
.replace(/<!--[\s\S]*?-->/g, '')
Expand Down
18 changes: 13 additions & 5 deletions packages/image/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ import {visitParents} from 'unist-util-visit-parents'
*/
const magicDefaultHostname = 'example.com'

const lineEnding = /\r?\n/g

/** @type {Options} */
const emptyOptions = {}
/** @type {[]} */
Expand Down Expand Up @@ -129,17 +131,23 @@ export default function rehypeGithubImage(options) {

/** @type {BuildVisitor<Root, 'element'>} */
function onelement(node, parents) {
if (
node.type === 'element' &&
node.tagName === 'img' &&
node.properties
) {
if (node.type === 'element' && node.tagName === 'img') {
let fragment = node
const sources = [fragment]
const raw = node.properties.src
const [source, proxy] = sanitizeSource(raw, toProxyUrl, internal)

if (proxy) node.properties.dataCanonicalSrc = raw

// GitHub removes line endings in `alt` on images.
// CommonMark doesn’t seem to do that.
if (node.properties.alt !== null && node.properties.alt !== undefined) {
node.properties.alt = String(node.properties.alt).replace(
lineEnding,
' '
)
}

node.properties.src = ''
node.properties.style = 'max-width: 100%;'

Expand Down
7 changes: 7 additions & 0 deletions packages/image/test/fixtures/alt.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<p>alpha <a target="_blank" rel="noopener noreferrer" href="z"><img src="z" alt="x y" style="max-width: 100%;"></a></p>
<p>bravo <a target="_blank" rel="noopener noreferrer" href="z"><img src="z" alt="x " style="max-width: 100%;"></a></p>
<p>charlie <a target="_blank" rel="noopener noreferrer" href="z"><img src="z" alt=" y" style="max-width: 100%;"></a></p>
<p>delta <a target="_blank" rel="noopener noreferrer" href="z"><img src="z" alt=" x y" style="max-width: 100%;"></a></p>
<p>echo <a target="_blank" rel="noopener noreferrer" href="z"><img src="z" alt="x y " style="max-width: 100%;"></a></p>
<p>foxtrot <a target="_blank" rel="noopener noreferrer" href="z"><img src="z" alt=" x y" style="max-width: 100%;"></a></p>
<p>golf <a target="_blank" rel="noopener noreferrer" href="z"><img src="z" alt="x y " style="max-width: 100%;"></a></p>
20 changes: 20 additions & 0 deletions packages/image/test/fixtures/alt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
alpha ![x
y](z)

bravo ![x
](z)

charlie ![
y](z)

delta ![ x
y](z)

echo ![x
y ](z)

foxtrot ![ x
y](z)

golf ![x
y ](z)
14 changes: 14 additions & 0 deletions packages/image/test/fixtures/title.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<p>alpha <a target="_blank" rel="noopener noreferrer" href="z"><img src="z" alt="" title="x
y" style="max-width: 100%;"></a></p>
<p>bravo <a target="_blank" rel="noopener noreferrer" href="z"><img src="z" alt="" title="x
" style="max-width: 100%;"></a></p>
<p>charlie <a target="_blank" rel="noopener noreferrer" href="z"><img src="z" alt="" title="
y" style="max-width: 100%;"></a></p>
<p>delta <a target="_blank" rel="noopener noreferrer" href="z"><img src="z" alt="" title=" x
y" style="max-width: 100%;"></a></p>
<p>echo <a target="_blank" rel="noopener noreferrer" href="z"><img src="z" alt="" title="x
y " style="max-width: 100%;"></a></p>
<p>foxtrot <a target="_blank" rel="noopener noreferrer" href="z"><img src="z" alt="" title=" x
y" style="max-width: 100%;"></a></p>
<p>golf <a target="_blank" rel="noopener noreferrer" href="z"><img src="z" alt="" title="x
y " style="max-width: 100%;"></a></p>
20 changes: 20 additions & 0 deletions packages/image/test/fixtures/title.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
alpha ![](z "x
y")

bravo ![](z "x
")

charlie ![](z "
y")

delta ![](z " x
y")

echo ![](z "x
y ")

foxtrot ![](z " x
y")

golf ![](z "x
y ")

0 comments on commit c07be31

Please sign in to comment.