Skip to content

Commit

Permalink
fix: srcset of <img> in mdx format won't show image (#1536)
Browse files Browse the repository at this point in the history
Co-authored-by: xiaoyifang <[email protected]>
  • Loading branch information
shenlebantongying and xiaoyifang authored May 29, 2024
1 parent 9edef84 commit e6a2be8
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/common/globalregex.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ QRegularExpression Mdx::srcRe(
QRegularExpression Mdx::srcRe2(
R"(([\s"'](?:src|srcset)\s*=)\s*(?![\s"']|\b(?:(?:bres|https?|ftp)://|(?:data|javascript):))(?:file://)?[\x00-\x1f\x7f]*\.*/?([^\s">]+))",
QRegularExpression::CaseInsensitiveOption );
// matches srcset in <img srcset="text">
QRegularExpression Mdx::srcset( R"((?<before>[\s]srcset\s*=\s*["'])\s*(?<text>[\s\S]*)(?<after>["']))",
QRegularExpression::CaseInsensitiveOption );

QRegularExpression Mdx::links( R"(url\(\s*(['"]?)([^'"]*)(['"]?)\s*\))", QRegularExpression::CaseInsensitiveOption );

Expand Down
3 changes: 3 additions & 0 deletions src/common/globalregex.hh
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public:
static QRegularExpression closeScriptTagRe;
static QRegularExpression srcRe;
static QRegularExpression srcRe2;
static QRegularExpression srcset;

static QRegularExpression links;
static QRegularExpression fontFace;
Expand Down Expand Up @@ -72,6 +73,8 @@ const static QRegularExpression markPuncSpace( R"([\p{M}\p{Z}\p{P}])", QRegularE
//contain unicode space and mark.
const static QRegularExpression markSpace( R"([\p{M}\p{Z}])", QRegularExpression::UseUnicodePropertiesOption );

const static QRegularExpression whiteSpace( "\\s+" );

} // namespace RX

#endif // GLOBALREGEX_HH
30 changes: 30 additions & 0 deletions src/dict/mdx.cc
Original file line number Diff line number Diff line change
Expand Up @@ -978,6 +978,36 @@ void MdxDictionary::replaceLinks( QString & id, QString & article )
else
newLink = linkTxt.replace( RX::Mdx::srcRe2, R"(\1"bres://)" + id + R"(/\2")" );
}

// convert <img src="bres://{id}/a.png" srcset="a-1x.png 1x, b-2x.png 2x, c.png">
// into <img src="bres://{id}/a.png" srcset="bres://{id}/a-1x.png 1x,bres://{id}/b-2x.png 2x,bres://{id}/c.png">

if ( linkType.compare( "img" ) == 0 ) {
match = RX::Mdx::srcset.match( newLink ); // have to use newLink since linkTxt may already be modified
if ( match.hasMatch() ) {
auto srcsetOriginalText = match.captured( "text" );
QStringList srcsetNewText = {};

auto ImageList = srcsetOriginalText.split( u',', Qt::SkipEmptyParts );

for ( auto & img : ImageList ) {
auto imgPair = img.split( RX::whiteSpace );

if ( !imgPair.empty() && !imgPair.at( 0 ).contains( "//" ) ) {
if ( imgPair.length() == 1 ) {
srcsetNewText.append( QString( R"(bres://%1/%2)" ).arg( id, imgPair.at( 0 ) ) );
}
else if ( imgPair.length() == 2 ) {
srcsetNewText.append( QString( R"(bres://%1/%2 %3)" ).arg( id, imgPair.at( 0 ), imgPair.at( 1 ) ) );
}
}
}

newLink.replace( match.capturedStart(),
match.capturedLength(),
match.captured( "before" ) % srcsetNewText.join( ',' ) % match.captured( "after" ) );
}
}
}

if ( !newLink.isEmpty() ) {
Expand Down

0 comments on commit e6a2be8

Please sign in to comment.