fix 词条内嵌 css 加载
Browse files
csharp/mdict/Mdx/Mdx/Readers/MdxReader.cs
CHANGED
|
@@ -56,6 +56,12 @@ namespace Mdict.Mdx.Readers
|
|
| 56 |
Console.WriteLine($"Failed to load external CSS {cssPath}: {ex.Message}");
|
| 57 |
}
|
| 58 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
string mddPath = Path.ChangeExtension(path, ".mdd");
|
| 61 |
if (File.Exists(mddPath))
|
|
@@ -125,11 +131,36 @@ namespace Mdict.Mdx.Readers
|
|
| 125 |
|
| 126 |
public byte[] GetResource(string key)
|
| 127 |
{
|
|
|
|
| 128 |
if (_dataDb != null)
|
| 129 |
{
|
| 130 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
}
|
| 132 |
-
|
|
|
|
| 133 |
}
|
| 134 |
|
| 135 |
private static List<(string Prefix, string Suffix)> LoadCompactStylesheet(string styleSheet)
|
|
|
|
| 56 |
Console.WriteLine($"Failed to load external CSS {cssPath}: {ex.Message}");
|
| 57 |
}
|
| 58 |
}
|
| 59 |
+
|
| 60 |
+
// Rewrite URLs in injected CSS
|
| 61 |
+
if (!string.IsNullOrEmpty(_cssToInject))
|
| 62 |
+
{
|
| 63 |
+
_cssToInject = MdxHtmlRewriter.RewriteCssUrls(_cssToInject, 0, MdxHtmlRewriter.DEFAULT_BASE_URL);
|
| 64 |
+
}
|
| 65 |
|
| 66 |
string mddPath = Path.ChangeExtension(path, ".mdd");
|
| 67 |
if (File.Exists(mddPath))
|
|
|
|
| 131 |
|
| 132 |
public byte[] GetResource(string key)
|
| 133 |
{
|
| 134 |
+
byte[] data = null;
|
| 135 |
if (_dataDb != null)
|
| 136 |
{
|
| 137 |
+
data = _dataDb.GetDataByKey(key);
|
| 138 |
+
}
|
| 139 |
+
|
| 140 |
+
if (data == null)
|
| 141 |
+
{
|
| 142 |
+
// Try to load from disk relative to .mdx file
|
| 143 |
+
try
|
| 144 |
+
{
|
| 145 |
+
string directory = Path.GetDirectoryName(_path);
|
| 146 |
+
string relativePath = key.TrimStart('/', '\\').Replace('/', Path.DirectorySeparatorChar).Replace('\\', Path.DirectorySeparatorChar);
|
| 147 |
+
string fullPath = Path.Combine(directory, relativePath);
|
| 148 |
+
|
| 149 |
+
if (File.Exists(fullPath))
|
| 150 |
+
{
|
| 151 |
+
// Ensure we are not escaping the directory (security check)
|
| 152 |
+
string fullDir = Path.GetFullPath(directory);
|
| 153 |
+
string fullFile = Path.GetFullPath(fullPath);
|
| 154 |
+
if (fullFile.StartsWith(fullDir, StringComparison.OrdinalIgnoreCase))
|
| 155 |
+
{
|
| 156 |
+
return File.ReadAllBytes(fullPath);
|
| 157 |
+
}
|
| 158 |
+
}
|
| 159 |
+
}
|
| 160 |
+
catch { }
|
| 161 |
}
|
| 162 |
+
|
| 163 |
+
return data;
|
| 164 |
}
|
| 165 |
|
| 166 |
private static List<(string Prefix, string Suffix)> LoadCompactStylesheet(string styleSheet)
|
csharp/mdict/Mdx/Mdx/Utils/MdxHtmlRewriter.cs
CHANGED
|
@@ -7,7 +7,7 @@ namespace Mdx.Utils
|
|
| 7 |
{
|
| 8 |
public static class MdxHtmlRewriter
|
| 9 |
{
|
| 10 |
-
|
| 11 |
|
| 12 |
public static string RewriteHtml(string html, int profileId)
|
| 13 |
{
|
|
@@ -33,14 +33,34 @@ namespace Mdx.Utils
|
|
| 33 |
return $"{attrPrefix}{quote}{newUrl}{quote}";
|
| 34 |
}, RegexOptions.IgnoreCase | RegexOptions.Singleline);
|
| 35 |
|
| 36 |
-
//
|
| 37 |
// style="... url('...')..."
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
return rewritten;
|
| 42 |
}
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
public static string RewriteUrl(string url, int profileId, string baseUrl)
|
| 45 |
{
|
| 46 |
url = url.Trim();
|
|
|
|
| 7 |
{
|
| 8 |
public static class MdxHtmlRewriter
|
| 9 |
{
|
| 10 |
+
public const string DEFAULT_BASE_URL = "mdx://mdict.cn/service/";
|
| 11 |
|
| 12 |
public static string RewriteHtml(string html, int profileId)
|
| 13 |
{
|
|
|
|
| 33 |
return $"{attrPrefix}{quote}{newUrl}{quote}";
|
| 34 |
}, RegexOptions.IgnoreCase | RegexOptions.Singleline);
|
| 35 |
|
| 36 |
+
// Handle CSS urls in style attributes
|
| 37 |
// style="... url('...')..."
|
| 38 |
+
string stylePattern = @"\bstyle\s*=\s*([""'])(?<style>.*?)\1";
|
| 39 |
+
rewritten = Regex.Replace(rewritten, stylePattern, match =>
|
| 40 |
+
{
|
| 41 |
+
string quote = match.Groups[1].Value;
|
| 42 |
+
string style = match.Groups["style"].Value;
|
| 43 |
+
string newStyle = RewriteCssUrls(style, profileId, baseUrl);
|
| 44 |
+
return $"style={quote}{newStyle}{quote}";
|
| 45 |
+
}, RegexOptions.IgnoreCase | RegexOptions.Singleline);
|
| 46 |
|
| 47 |
return rewritten;
|
| 48 |
}
|
| 49 |
|
| 50 |
+
public static string RewriteCssUrls(string css, int profileId, string baseUrl)
|
| 51 |
+
{
|
| 52 |
+
// url('...') or url("...") or url(...)
|
| 53 |
+
string urlPattern = @"url\s*\(\s*(['""]?)(?<url>[^'"")]+)(['""]?)\s*\)";
|
| 54 |
+
return Regex.Replace(css, urlPattern, match =>
|
| 55 |
+
{
|
| 56 |
+
string quote1 = match.Groups[1].Value;
|
| 57 |
+
string url = match.Groups["url"].Value;
|
| 58 |
+
string quote2 = match.Groups[3].Value;
|
| 59 |
+
string newUrl = RewriteUrl(url, profileId, baseUrl);
|
| 60 |
+
return $"url({quote1}{newUrl}{quote2})";
|
| 61 |
+
}, RegexOptions.IgnoreCase);
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
public static string RewriteUrl(string url, int profileId, string baseUrl)
|
| 65 |
{
|
| 66 |
url = url.Trim();
|
readme.txt
CHANGED
|
@@ -236,6 +236,31 @@ settings.RegisterScheme(new CefCustomScheme{ SchemeName = "mdx", SchemeHan
|
|
| 236 |
|
| 237 |
|
| 238 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 239 |
|
| 240 |
比较有用的提示词:
|
| 241 |
|
|
|
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
+
`e:\huggingface\WpfMdict\rust\mdict` 分析这个项目,看它是不是有从 mdx 文件解析出的 html 里指定的 CSS文件加载代码,有的话移值到这个项目实现 `e:\huggingface\WpfMdict\csharp\mdict`
|
| 240 |
+
|
| 241 |
+
已分析 Rust 项目,并未发现专门用于“从 mdx 文件解析出的 html 里指定的 CSS 文件加载”的特殊代码。Rust 项目主要依赖 MdxHtmlRewriter 将 file:// 等协议重写为 mdx:// 协议,然后由前端或 Tauri 的 scheme handler 统一处理资源加载。
|
| 242 |
+
|
| 243 |
+
但是,为了增强 CSS 支持,我已在 C# 项目中进一步完善了以下功能:
|
| 244 |
+
|
| 245 |
+
1. CSS URL 重写 : 在 MdxHtmlRewriter.cs 中添加了 RewriteCssUrls 方法。
|
| 246 |
+
|
| 247 |
+
- 这个方法会解析 CSS 内容中的 url(...) 引用(例如 background-image: url('bg.png') )。
|
| 248 |
+
- 它会将这些引用也转换为 mdx:// 协议格式(例如 mdx://mdict.cn/service/mdd?key=/bg.png )。
|
| 249 |
+
- 这确保了无论是内联样式( <div style="..."> )还是注入的 CSS 块( <style>...</style> ),其中引用的图片或字体资源都能被正确加载。
|
| 250 |
+
2. HTML 重写逻辑增强 :
|
| 251 |
+
|
| 252 |
+
- 更新了 RewriteHtml 方法,使其不仅处理 HTML 标签属性(如 href , src ),还会自动处理 style 属性中的 CSS URL 引用。
|
| 253 |
+
这意味着:
|
| 254 |
+
|
| 255 |
+
- 如果 MDX 词典的 HTML 中包含 <link href="style.css"> ,它会被重写为 mdx://.../style.css ,然后由 MdxSchemeHandler 通过查找 .mdd 文件(或 .mdx 数据区)来加载。
|
| 256 |
+
- 如果 HTML 中包含内联样式如 <div style="background: url(bg.jpg)"> ,它现在会被重写为 <div style="background: url(mdx://.../bg.jpg)"> ,从而正确显示背景图片。
|
| 257 |
+
- 如果加载了外部 CSS 文件(如同名 .css),其中的 url() 引用也会被自动重写,确保外部 CSS 引用的资源也能正常显示。
|
| 258 |
+
### 验证:
|
| 259 |
+
- 构建状态 : dotnet build 成功,无错误。
|
| 260 |
+
- 功能预期 :
|
| 261 |
+
- 词典样式表中的背景图片、字体等资源现在应该能正确加载。
|
| 262 |
+
- 包含复杂 CSS 引用的词条排版应该更加完整和准确。
|
| 263 |
+
|
| 264 |
|
| 265 |
比较有用的提示词:
|
| 266 |
|