-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Render captions for singleton images with titles (#17)
* Add new plugin to wrap images in figures with captions * Update and fix full integration test * Add styling for figures + figcaptions * Fix script execution for dev * Add plugin to readme * Add to changelog
- Loading branch information
Showing
12 changed files
with
239 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
Copyright 2022 DigitalOcean | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
/** | ||
* @module rules/image_caption | ||
*/ | ||
|
||
/** | ||
* Wrap singleton images that have title text in a figure with a rendered caption. | ||
* | ||
* @example | ||
*  | ||
* | ||
*  | ||
* | ||
* <figure><img src="test.png" alt="alt text"><figcaption>title text</figcaption></figure> | ||
* | ||
* <figure><img src="test.png" alt="alt text"><figcaption>title text <em>with Markdown</em></figcaption></figure> | ||
* | ||
* @type {import('markdown-it').PluginSimple} | ||
*/ | ||
module.exports = md => { | ||
/** | ||
* Parsing rule for wrapping singleton image that has a title in a figure with a caption. | ||
* | ||
* @type {import('markdown-it/lib/parser_core').RuleCore} | ||
* @private | ||
*/ | ||
const imageCaptionRule = state => { | ||
// Iterate over all tokens, except the first and last | ||
for (let i = 1; i < state.tokens.length - 1; i += 1) { | ||
const token = state.tokens[i]; | ||
|
||
// Check if we have an image | ||
if (token.type !== 'inline') continue; | ||
if (token.children.length !== 1) continue; | ||
if (token.children[0].type !== 'image') continue; | ||
|
||
// Check if we have a title | ||
const title = token.children[0].attrGet('title'); | ||
if (!title) continue; | ||
|
||
// Check this is the only item in the paragraph | ||
const open = state.tokens[i - 1]; | ||
const close = state.tokens[i + 1]; | ||
if (open.type !== 'paragraph_open') continue; | ||
if (close.type !== 'paragraph_close') continue; | ||
|
||
// Mutate open/close to become a figure, not a paragraph | ||
open.type = 'figure_open'; | ||
open.tag = 'figure'; | ||
close.type = 'figure_close'; | ||
close.tag = 'figure'; | ||
|
||
// Inject the caption, treating the title as inline Markdown | ||
token.children.push(new state.Token('figcaption_open', 'figcaption', 1)); | ||
token.children.push(...md.parseInline(title, state.env)[0].children); | ||
token.children.push(new state.Token('figcaption_close', 'figcaption', -1)); | ||
token.children[0].attrs = token.children[0].attrs.filter(([ attr ]) => attr !== 'title'); | ||
} | ||
}; | ||
|
||
md.core.ruler.after('inline', 'image_caption', imageCaptionRule); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
Copyright 2022 DigitalOcean | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const md = require('markdown-it')().use(require('./image_caption')); | ||
|
||
it('handles an image with no alt text and no title text (no caption)', () => { | ||
expect(md.render('')).toBe(`<p><img src="test.png" alt=""></p> | ||
`); | ||
}); | ||
|
||
it('handles an image with alt text and no title text (no caption)', () => { | ||
expect(md.render('')).toBe(`<p><img src="test.png" alt="alt text"></p> | ||
`); | ||
}); | ||
|
||
it('handles an image with no alt text and unclosed title text (no image)', () => { | ||
expect(md.render('')).toBe(`<p></p> | ||
`); | ||
}); | ||
|
||
it('handles an image with alt text and unclosed title text (no image)', () => { | ||
expect(md.render('')).toBe(`<p></p> | ||
`); | ||
}); | ||
|
||
it('handles an image with no alt text but title text, with surrounding text (no caption)', () => { | ||
expect(md.render(' hello')).toBe(`<p><img src="test.png" alt="" title="title text"> hello</p> | ||
`); | ||
}); | ||
|
||
it('handles an image with no alt text but title text, with text after (no caption)', () => { | ||
expect(md.render('\nhello')).toBe(`<p><img src="test.png" alt="" title="title text"> | ||
hello</p> | ||
`); | ||
}); | ||
|
||
it('handles an image with no alt text but title text', () => { | ||
expect(md.render('')).toBe(`<figure><img src="test.png" alt=""><figcaption>title text</figcaption></figure> | ||
`); | ||
}); | ||
|
||
it('handles an image with alt text and title text', () => { | ||
expect(md.render('')).toBe(`<figure><img src="test.png" alt="alt text"><figcaption>title text</figcaption></figure> | ||
`); | ||
}); | ||
|
||
it('handles an image with alt text and title text using Markdown', () => { | ||
expect(md.render('')).toBe(`<figure><img src="test.png" alt="alt text"><figcaption>title text <em>with Markdown</em></figcaption></figure> | ||
`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters