Skip to content

Commit d5de7e4

Browse files
committed
给markdown增加点击图片支持
1 parent 748278f commit d5de7e4

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

lib/common/utils/CommonUtils.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,11 @@ class CommonUtils {
101101
static launchUrl(context, String url) {
102102
if (url == null && url.length == 0) return;
103103
Uri parseUrl = Uri.parse(url);
104-
if (isImageEnd(parseUrl.toString())) {
104+
bool isImage = isImageEnd(parseUrl.toString());
105+
if(parseUrl.toString().endsWith("?raw=true")) {
106+
isImage = isImageEnd(parseUrl.toString().replaceAll("?raw=true", ""));
107+
}
108+
if (isImage) {
105109
NavigatorUtils.gotoPhotoViewPage(context, url);
106110
return;
107111
}

lib/widget/GSYMarkdownWidget.dart

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,21 +111,28 @@ class GSYMarkdownWidget extends StatelessWidget {
111111
_getMarkDownData(String markdownData) {
112112
///优化图片显示
113113
RegExp exp = new RegExp(r'!\[.*\]\((.+)\)');
114+
RegExp expImg = new RegExp("<img.*?(?:>|\/>)");
115+
RegExp expSrc = new RegExp("src=[\'\"]?([^\'\"]*)[\'\"]?");
116+
114117
Iterable<Match> tags = exp.allMatches(markdownData);
115118
String mdDataCode = markdownData;
116119
if (tags != null && tags.length > 0) {
117120
for (Match m in tags) {
118121
String imageMatch = m.group(0);
119122
if (imageMatch != null) {
120123
String match = m.group(0).replaceAll("\)", "?raw=true)");
124+
if (!match.contains(".svg")) {
125+
///增加点击
126+
String src = match.replaceAll("![](", "").replaceAll(")", "");
127+
String actionMatch = "[$match]($src)";
128+
match = actionMatch;
129+
}
121130
mdDataCode = mdDataCode.replaceAll(m.group(0), match);
122131
}
123132
}
124133
}
125134

126135
///优化img标签的src资源
127-
RegExp expImg = new RegExp("<img.*?(?:>|\/>)");
128-
RegExp expSrc = new RegExp("src=[\'\"]?([^\'\"]*)[\'\"]?");
129136
tags = expImg.allMatches(markdownData);
130137
if (tags != null && tags.length > 0) {
131138
for (Match m in tags) {
@@ -137,15 +144,16 @@ class GSYMarkdownWidget extends StatelessWidget {
137144
String srcString = srcMatch.group(0);
138145
if (srcString != null && srcString.contains("http")) {
139146
String newSrc = srcString.substring(srcString.indexOf("http"), srcString.length - 1) + "?raw=true";
140-
match = "![]($newSrc)";
147+
148+
///增加点击
149+
match = "[![]($newSrc)]($newSrc)";
141150
}
142151
}
143152
}
144153
mdDataCode = mdDataCode.replaceAll(imageTag, match);
145154
}
146155
}
147156

148-
149157
return mdDataCode;
150158
}
151159

0 commit comments

Comments
 (0)