Skip to content

Commit

Permalink
fix(Makdown decoding): Use title and caption
Browse files Browse the repository at this point in the history
  • Loading branch information
nokome committed Aug 31, 2021
1 parent da64b64 commit ca46470
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
31 changes: 17 additions & 14 deletions rust/src/methods/decode/md.rs
Expand Up @@ -21,9 +21,9 @@ use pulldown_cmark::{CodeBlockKind, Event, Options, Parser, Tag};
use regex::Regex;
use stencila_schema::{
Article, AudioObjectSimple, BlockContent, Cite, CiteGroup, CodeBlock, CodeChunk,
CodeExpression, CodeFragment, CreativeWorkContent, Delete, Emphasis, Heading,
ImageObjectSimple, InlineContent, Link, List, ListItem, ListItemContent, MathFragment, Node,
Paragraph, QuoteBlock, Strong, Subscript, Superscript, TableCell, TableCellContent, TableRow,
CodeExpression, CodeFragment, CreativeWorkTitle, Delete, Emphasis, Heading, ImageObjectSimple,
InlineContent, Link, List, ListItem, ListItemContent, MathFragment, Node, Paragraph,
QuoteBlock, Strong, Subscript, Superscript, TableCell, TableCellContent, TableRow,
TableRowRowType, TableSimple, ThematicBreak, VideoObjectSimple,
};

Expand Down Expand Up @@ -328,44 +328,47 @@ pub fn decode_fragment(md: &str) -> Vec<BlockContent> {
}))
}
Tag::Image(_link_type, url, title) => {
let content = inlines.pop_tail();
let content = if content.is_empty() {
let caption = inlines.pop_tail();
let caption = if caption.is_empty() {
None
} else {
// Content is reduced to a string. Media object do not often have other, more
// complicated, Markdown content in any case.
let txt = content.to_txt();
Some(Box::new(CreativeWorkContent::String(txt)))
let txt = caption.to_txt();
Some(Box::new(txt))
};

let title = {
let title = title.to_string();
if !title.is_empty() {
Some(Box::new(title))
Some(Box::new(CreativeWorkTitle::String(title)))
} else {
None
}
};

let extension = PathBuf::from(&url.to_string()).extension().map_or_else(
|| "".to_string(),
|ext| ext.to_string_lossy().to_string().to_ascii_lowercase(),
);

let media_object = match format_type(extension.as_str()) {
FormatType::AudioObject => InlineContent::AudioObject(AudioObjectSimple {
content,
caption,
content_url: url.to_string(),
caption: title,
title,
..Default::default()
}),
FormatType::VideoObject => InlineContent::VideoObject(VideoObjectSimple {
content,
caption,
content_url: url.to_string(),
caption: title,
title,
..Default::default()
}),
_ => InlineContent::ImageObject(ImageObjectSimple {
content,
caption,
content_url: url.to_string(),
caption: title,
title,
..Default::default()
}),
};
Expand Down
4 changes: 2 additions & 2 deletions rust/src/methods/decode/snapshots/md_fragments@image.md.snap
Expand Up @@ -23,7 +23,7 @@ input_file: fixtures/fragments/md/image.md
{
"type": "ImageObject",
"contentUrl": "image.png",
"caption": "the title"
"title": "the title"
},
"."
]
Expand All @@ -35,7 +35,7 @@ input_file: fixtures/fragments/md/image.md
{
"type": "ImageObject",
"contentUrl": "image.png",
"content": "some content"
"caption": "some content"
},
"."
]
Expand Down

0 comments on commit ca46470

Please sign in to comment.