Skip to content

Commit ac5736f

Browse files
authored
Support video tag with poster attribute (#189)
1 parent daa9e28 commit ac5736f

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

Diff for: markdownify/__init__.py

+18
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,24 @@ def convert_img(self, el, text, parent_tags):
538538

539539
return '![%s](%s%s)' % (alt, src, title_part)
540540

541+
def convert_video(self, el, text, parent_tags):
542+
if ('_inline' in parent_tags
543+
and el.parent.name not in self.options['keep_inline_images_in']):
544+
return text
545+
src = el.attrs.get('src', None) or ''
546+
if not src:
547+
sources = el.find_all('source', attrs={'src': True})
548+
if sources:
549+
src = sources[0].attrs.get('src', None) or ''
550+
poster = el.attrs.get('poster', None) or ''
551+
if src and poster:
552+
return '[![%s](%s)](%s)' % (text, poster, src)
553+
if src:
554+
return '[%s](%s)' % (text, src)
555+
if poster:
556+
return '![%s](%s)' % (text, poster)
557+
return text
558+
541559
def convert_list(self, el, text, parent_tags):
542560

543561
# Converting a list to inline is undefined.

Diff for: tests/test_conversions.py

+8
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,14 @@ def test_img():
243243
assert md('<img src="/path/to/img.jpg" alt="Alt text" />') == '![Alt text](/path/to/img.jpg)'
244244

245245

246+
def test_video():
247+
assert md('<video src="/path/to/video.mp4" poster="/path/to/img.jpg">text</video>') == '[![text](/path/to/img.jpg)](/path/to/video.mp4)'
248+
assert md('<video src="/path/to/video.mp4">text</video>') == '[text](/path/to/video.mp4)'
249+
assert md('<video><source src="/path/to/video.mp4"/>text</video>') == '[text](/path/to/video.mp4)'
250+
assert md('<video poster="/path/to/img.jpg">text</video>') == '![text](/path/to/img.jpg)'
251+
assert md('<video>text</video>') == 'text'
252+
253+
246254
def test_kbd():
247255
inline_tests('kbd', '`')
248256

0 commit comments

Comments
 (0)