markdown-it-fitvids v0.2.0
A markdown-it plugin to make iframes and videos responsive. The original idea goes back to FitVids.js and the evolutionary improvements that are possible because web browsers are capable of more now.
Fitting media
Videos and iframes are not automatically responsive or fluid. They come with a fixed setting for width and height. To make them responsive, while keeping aspect-ratio, markdown-it-fitvids will expand the style
attribute of these elements with the aspect-ratio
property, based on the given width
and height
attributes. Also added to the style
attribute will be the settings max-width:100%; height:auto;
. These settings will allow the video to shrink, but not to expand above its initial size. To let the video grow above its initial size, the options of markdown-it-fitvids can be adjusted to add width: 100%;
to the style
attribute.
!IMPORTANT The above described fitting of media can only be performed for elements that have the html attributes
width
andheight
set, or that do already haveaspect-ratio
set in thestyle
attribute!
For example, the following iframe
<iframe
src="https://player.vimeo.com/video/304626830"
width="600"
height="338"
></iframe>
after processed by markdown-it-fitvids will become
<iframe
src="https://player.vimeo.com/video/304626830"
style="aspect-ratio:600/338; max-width:100%; height:auto;"
width="600"
height="338"
>
</iframe>
Options
markdown-it-fitvids has the following default options:
{
fitSelector: "iframe,video",
applyStyle: "",
applyClass: "",
minimalStyle: false
}
fitSelector
: Identify the html elements that should be processed by markdown-it-fitvidsapplyStyle
: For every processed element the here provided string is added to thestyle
attribute. E.g, to let processed elements grow above their initial size, add the stringwidth:100%;
. Defautl is""
applyClass
: For every processed element the here provided CSS classe are added to theclass
attribute. This is a string with space separated CSS class names. You are responsible to maintain the CSS classes within your stylesheet. Default is""
minimalStyle
: By default every processed element will get the settingmax-width:100%; height:auto;
assigned to thestyle
attribute. If you do not want to set thestyle
attribute, and instead do the styling through CSS classes, set this option totrue
. Default isfalse
. When setting this totrue
, you are then responsible to provide the suitable CSS classes within your stylesheet and to mention those classes inapplyClass
option.
Usage
import markdownIt from "markdown-it";
import markdownItFitVids from "markdown-it-fitvids";
markdownIt({
html: true,
}).use(markdownItFitVids, { //default options, you can omit these
});