Skip to content

Commit

Permalink
Better mime recognition for inline images.
Browse files Browse the repository at this point in the history
  • Loading branch information
sbarex committed Jan 20, 2021
1 parent b9eaaf2 commit 8096e30
Show file tree
Hide file tree
Showing 19 changed files with 4,604 additions and 126 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Changelog
=======

### 1.0b17
Bugfix:
- Better mime recognition for inline images.

### 1.0b16
Bugfix:
- Fix on heads extension.
Expand Down
11 changes: 10 additions & 1 deletion QLExtension/PreviewViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,16 @@ extension PreviewViewController: WKScriptMessageHandler {
return
}

guard let data = get_base64_image(src.cString(using: .utf8)) else {
guard let data = get_base64_image(
src.cString(using: .utf8),
{ (path: UnsafePointer<Int8>?, context: UnsafeMutableRawPointer?) -> UnsafeMutablePointer<Int8>? in
let magic_file = Settings.shared.getResourceBundle().path(forResource: "magic", ofType: "mgc")?.cString(using: .utf8)

let r = magic_get_mime_by_file(path, magic_file)
return r
},
nil
) else {
return
}
defer {
Expand Down
8 changes: 4 additions & 4 deletions QLMarkdown.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1059,7 +1059,7 @@
CODE_SIGN_ENTITLEMENTS = QLExtension/QLExtension.entitlements;
CODE_SIGN_IDENTITY = "-";
CODE_SIGN_STYLE = Manual;
CURRENT_PROJECT_VERSION = 16;
CURRENT_PROJECT_VERSION = 17;
DEVELOPMENT_TEAM = "";
ENABLE_HARDENED_RUNTIME = NO;
HEADER_SEARCH_PATHS = (
Expand Down Expand Up @@ -1095,7 +1095,7 @@
CODE_SIGN_ENTITLEMENTS = QLExtension/QLExtension.entitlements;
CODE_SIGN_IDENTITY = "-";
CODE_SIGN_STYLE = Manual;
CURRENT_PROJECT_VERSION = 16;
CURRENT_PROJECT_VERSION = 17;
DEVELOPMENT_TEAM = "";
ENABLE_HARDENED_RUNTIME = NO;
HEADER_SEARCH_PATHS = (
Expand Down Expand Up @@ -1250,7 +1250,7 @@
CODE_SIGN_IDENTITY = "-";
CODE_SIGN_STYLE = Manual;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 16;
CURRENT_PROJECT_VERSION = 17;
DEVELOPMENT_TEAM = "";
ENABLE_HARDENED_RUNTIME = NO;
FRAMEWORK_SEARCH_PATHS = (
Expand Down Expand Up @@ -1295,7 +1295,7 @@
CODE_SIGN_IDENTITY = "-";
CODE_SIGN_STYLE = Manual;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 16;
CURRENT_PROJECT_VERSION = 17;
DEVELOPMENT_TEAM = "";
ENABLE_HARDENED_RUNTIME = NO;
FRAMEWORK_SEARCH_PATHS = (
Expand Down
2 changes: 1 addition & 1 deletion QLMarkdown/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,7 @@
<rect key="frame" x="0.0" y="0.0" width="1011" height="700"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<tabView translatesAutoresizingMaskIntoConstraints="NO" id="buS-Ph-8Qs">
<tabView initialItem="XgS-T3-Gpv" translatesAutoresizingMaskIntoConstraints="NO" id="buS-Ph-8Qs">
<rect key="frame" x="13" y="592" width="985" height="114"/>
<font key="font" metaFont="system"/>
<tabViewItems>
Expand Down
6 changes: 6 additions & 0 deletions QLMarkdown/Settings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,12 @@ class Settings {
if self.inlineImageExtension, let ext = cmark_find_syntax_extension("inlineimage") {
cmark_parser_attach_syntax_extension(parser, ext)
cmark_syntax_extension_inlineimage_set_wd(ext, baseDir.cString(using: .utf8))
cmark_syntax_extension_inlineimage_set_mime_callback(ext, { (path, context) in
let magic_file = Settings.shared.getResourceBundle().path(forResource: "magic", ofType: "mgc")?.cString(using: .utf8)
let r = magic_get_mime_by_file(path, magic_file)
return r
}, nil)

if let l = log {
os_log(
"Enabled markdown `local inline image` extension with working path set to `%{public}s.",
Expand Down
6 changes: 5 additions & 1 deletion QLMarkdown/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1159,7 +1159,11 @@ extension ViewController: WKScriptMessageHandler {
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
if message.name == "scrollHandler", let dict = message.body as? [String : AnyObject], let p = dict["scroll"] as? Int {
self.prev_scroll = p
} else if message.name == "imageExtensionHandler", let dict = message.body as? [String : AnyObject], let src = dict["src"] as? String, let id = dict["id"] as? String, let data = get_base64_image(src.cString(using: .utf8)) {
} else if message.name == "imageExtensionHandler", let dict = message.body as? [String : AnyObject], let src = dict["src"] as? String, let id = dict["id"] as? String, let data = get_base64_image(src.cString(using: .utf8), { (path, context) in
let magic_file = Settings.shared.getResourceBundle().path(forResource: "magic", ofType: "mgc")?.cString(using: .utf8)
let r = magic_get_mime_by_file(path, magic_file)
return r
}, nil) {

let response: [String: String] = [
"src": src,
Expand Down
2 changes: 1 addition & 1 deletion cmark-gfm/MIMEtype/MIMEtype.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ static const char *mime_from_magic(FILE *fd) {
return NULL;
}
ret = mime_search(mimes0,LEN(mimes0), buf);
if (ret) {
if (ret && strlen(ret) > 0) {
return ret;
}
ret = mime_search(mimes4, LEN(mimes4), buf+4);
Expand Down
Loading

0 comments on commit 8096e30

Please sign in to comment.