Skip to content

Commit 2eeefb6

Browse files
committed
fix regex matche
1 parent bb33040 commit 2eeefb6

File tree

7 files changed

+43
-23
lines changed

7 files changed

+43
-23
lines changed

src/Blogifier.Admin/Components/PostEditorComponent.razor

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@
114114
async ValueTask<string?> GetValueAsync()
115115
{
116116
var content = await _editorJsInterop.GetEditorValueAsync();
117-
var imgsMatches = StringHelper.MatchesMarkdownImgBlob(content);
117+
var imgsMatches = StringHelper.MarkdownImgBlobGeneratedRegex().Matches(content);
118118

119119
if (imgsMatches.Count > 0)
120120
{
@@ -179,12 +179,14 @@
179179

180180
protected async Task ChangeCoverAsync()
181181
{
182-
await _commonJsInterop.TriggerClickAsync();
182+
await _commonJsInterop.TriggerClickAsync(_inputCovereference?.Element);
183183
}
184184

185185
protected async Task LoadCovereFile(InputFileChangeEventArgs args)
186186
{
187-
187+
var element = _inputCovereference?.Element;
188+
var blobInfo = await _commonJsInterop.GetInputFileBlobInfoAsync(element);
189+
Post.Cover = blobInfo.Url;
188190
}
189191

190192
protected Task RemoveCoverAsync()
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace Blogifier.Admin;
2+
3+
public class FrontBlobInfo
4+
{
5+
public string FileName { get; set; } = default!;
6+
public string Url { get; set; } = default!;
7+
}

src/Blogifier.Admin/Interop/CommonJsInterop.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using Microsoft.AspNetCore.Components;
12
using Microsoft.JSInterop;
23
using System;
34
using System.Threading.Tasks;
@@ -25,10 +26,16 @@ public async ValueTask SetTitleAsync(string content)
2526
await module.InvokeVoidAsync("setTitle", content);
2627
}
2728

28-
public async ValueTask TriggerClickAsync()
29+
public async ValueTask TriggerClickAsync(ElementReference? element)
2930
{
3031
var module = await moduleTask.Value;
31-
await module.InvokeVoidAsync("triggerClick");
32+
await module.InvokeVoidAsync("triggerClick", element);
33+
}
34+
35+
public async ValueTask<FrontBlobInfo> GetInputFileBlobInfoAsync(ElementReference? inputUpload)
36+
{
37+
var module = await moduleTask.Value;
38+
return await module.InvokeAsync<FrontBlobInfo>("getInputFileBlobInfo", inputUpload);
3239
}
3340

3441
public async ValueTask DisposeAsync()

src/Blogifier.Admin/assets/js/common.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export function writeCookie() {
4545
document.cookie = name + "=" + value + expires + "; path=/";
4646
}
4747

48-
export function setTooltip(){
48+
export function setTooltip() {
4949
setTimeout(function () {
5050
let options = { "trigger": "hover", fallbackPlacements: ['bottom'] };
5151
let tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'));
@@ -77,3 +77,10 @@ export function startClock() {
7777
time();
7878
setInterval(time, 60 * 1000);
7979
}
80+
81+
export function getInputFileBlobInfo(inputElement) {
82+
const file = inputElement.files[0];
83+
const fileName = file.name;
84+
const url = URL.createObjectURL(file);
85+
return { fileName, url };
86+
}

src/Blogifier.Shared/Helper/StringHelper.cs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,25 @@ namespace Blogifier.Helper;
44

55
public static partial class StringHelper
66
{
7-
87
[GeneratedRegex("<script[^>]*>[\\s\\S]*?</script>", RegexOptions.Compiled)]
98
private static partial Regex HtmlScriptGeneratedRegex();
109
public static string RemoveHtmlScriptTags(string input) => HtmlScriptGeneratedRegex().Replace(input, string.Empty);
1110

12-
1311
[GeneratedRegex("<img[^>]*>[\\s\\S]*?>", RegexOptions.Compiled)]
1412
private static partial Regex HtmlImgGeneratedRegex();
1513
public static string RemoveHtmlImgTags(string input) => HtmlImgGeneratedRegex().Replace(input, string.Empty);
1614

17-
1815
[GeneratedRegex("<img.+?src=[\"'](.+?)[\"'].+?>", RegexOptions.Compiled)]
19-
private static partial Regex HtmlImgSrcGeneratedRegex();
20-
public static Match MatchHtmlImgSrc(string input) => HtmlImgSrcGeneratedRegex().Match(input);
16+
public static partial Regex HtmlImgSrcGeneratedRegex();
2117

2218
[GeneratedRegex("<img[^>]*?src\\s*=\\s*[\"']?([^'\" >]+?)[ '\"][^>]*?>", RegexOptions.Compiled)]
23-
private static partial Regex HtmlImgTagsGeneratedRegex();
24-
public static MatchCollection MatchesHtmlImgTags(string input) => HtmlImgTagsGeneratedRegex().Matches(input);
25-
19+
public static partial Regex HtmlImgTagsGeneratedRegex();
2620

2721
[GeneratedRegex("(?i)<a\\b[^>]*?>(?<text>.*?)</a>", RegexOptions.Compiled)]
28-
private static partial Regex HtmlFileGeneratedRegex();
29-
public static MatchCollection MatchesHtmlFile(string input) => HtmlFileGeneratedRegex().Matches(input);
30-
22+
public static partial Regex HtmlFileGeneratedRegex();
3123

3224
[GeneratedRegex("!\\[[^\\]]*\\]\\((blob:[^)]+)\\)", RegexOptions.Compiled)]
33-
private static partial Regex MarkdownImgBlobGeneratedRegex();
34-
public static MatchCollection MatchesMarkdownImgBlob(string input) => MarkdownImgBlobGeneratedRegex().Matches(input);
35-
25+
public static partial Regex MarkdownImgBlobGeneratedRegex();
3626

3727
[GeneratedRegex(@"!\[(?<filename>[^\]]+)\]\(data:image\/(?<type>.+);base64,(?<data>.+?)\)", RegexOptions.Compiled)]
3828
public static partial Regex MarkdownDataImageBase64BlobGeneratedRegex();

src/Blogifier.Shared/Resources/Resource.zh-CN.resx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,4 +507,10 @@
507507
<data name="actions" xml:space="preserve">
508508
<value>操作</value>
509509
</data>
510+
<data name="change" xml:space="preserve">
511+
<value>修改</value>
512+
</data>
513+
<data name="reset" xml:space="preserve">
514+
<value>重置</value>
515+
</data>
510516
</root>

src/Blogifier/Storages/StorageManager.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,15 @@ public async Task<string> UploadsFoHtmlAsync(DateTime uploadAt, int userid, Uri
108108

109109
public async Task<string> UploadImagesFoHtml(DateTime uploadAt, int userid, Uri baseAddress, string content)
110110
{
111-
var matches = StringHelper.MatchesHtmlImgTags(content);
111+
var matches = StringHelper.HtmlImgTagsGeneratedRegex().Matches(content);
112112
if (matches.Any())
113113
{
114114
var contentBuilder = new StringBuilder(content);
115+
var htmlImgSrcRegex = StringHelper.HtmlImgSrcGeneratedRegex();
115116
foreach (Match match in matches.Cast<Match>())
116117
{
117118
var tag = match.Value;
118-
var matchUrl = StringHelper.MatchHtmlImgSrc(tag);
119+
var matchUrl = htmlImgSrcRegex.Match(tag);
119120
var urlString = matchUrl.Groups[1].Value;
120121
var storage = await UploadAsync(uploadAt, userid, baseAddress, urlString);
121122
var uploadTag = $"![{storage.Name}]({storage.Slug})";
@@ -128,7 +129,7 @@ public async Task<string> UploadImagesFoHtml(DateTime uploadAt, int userid, Uri
128129

129130
public async Task<string> UploadFilesFoHtml(DateTime uploadAt, int userid, Uri baseAddress, string content)
130131
{
131-
var matches = StringHelper.MatchesHtmlFile(content);
132+
var matches = StringHelper.HtmlFileGeneratedRegex().Matches(content);
132133
if (matches.Any())
133134
{
134135
var contentBuilder = new StringBuilder(content);

0 commit comments

Comments
 (0)