Files
coopgo/node_modules/hast-util-to-mdast/lib/handlers/media.js
sgauthier 6e64e138e2
All checks were successful
Publish To Prod / deploy_and_publish (push) Successful in 35s
planning
2024-10-14 09:15:30 +02:00

61 lines
1.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
'use strict'
module.exports = media
var convert = require('hast-util-is-element/convert')
var toString = require('mdast-util-to-string')
var visit = require('unist-util-visit')
var all = require('../all')
var resolve = require('../util/resolve')
var wrap = require('../util/wrap')
var source = convert('source')
var video = convert('video')
function media(h, node) {
var nodes = all(h, node)
var poster = video(node) && node.properties.poster
var src = node.properties.src
var index = -1
var linkInFallbackContent
visit({type: 'root', children: nodes}, 'link', findLink)
// If the content links to something, or if its not phrasing…
if (linkInFallbackContent || wrap.needed(nodes)) {
return nodes
}
// Find the source.
while (!src && ++index < node.children.length) {
if (source(node.children[index])) {
src = node.children[index].properties.src
}
}
// If theres a poster defined on the video, create an image.
if (poster) {
nodes = [
{
type: 'image',
title: null,
url: resolve(h, poster),
alt: toString({children: nodes})
}
]
}
// Link to the media resource.
return {
type: 'link',
title: node.properties.title || null,
url: resolve(h, src),
children: nodes
}
function findLink() {
linkInFallbackContent = true
return visit.EXIT
}
}