MacDownApp / macdown

Open source Markdown editor for macOS.
https://macdown.uranusjr.com/
9.45k stars 1.09k forks source link

Able to open files that are not text #89

Closed aaronraimist closed 10 years ago

aaronraimist commented 10 years ago

You are able to open videos and images with FIle > Open. In a Apple app other file types would be grayed out. In TextEdit though it does allow you to open images and videos but just produces an output like this

âPNG

IHDR––ö8ƒy
AiCCPICC ProfileH
ùñwTSŸáœΩ7Ω–" %Ùz   “;HQâIÄPÜÑ&vDF)VdT¿Gá"cEÉÇb◊  ÚP∆¡QDE›åk    Ô≠5Ûfiö˝«YflŸÁ∑◊ŸgÔ}◊∫P¸Ç¬tXÄ4°XÓΡ\Àƒ˜X¿·ffG¯D‘¸Ω=ôô®H∆≥ˆÓ.Ädª€,øP&s÷ˇë"7C$
E’6<~&ÂîS≥≈2ˇ Ùï)2Ü12°   ¢¨"„ƒØlˆßÊ+ª…òó&‰°YŒº4ûåªPfiö%·£å°\ò%‡g£|eΩTIö˜(””¯úL0ô_ÃÁ&°lâ2EÓâÚîƒ9ºrã˘9hûx¶g‰äâIb¶◊òiÂË»f˙Ò≥S˘b1+î√M·àxLœÙ¥é0ÄØoñE%YmôhëÌ≠ÌÌY÷Êh˘øŸfl~S˝=»z˚UÒ&ÏœûAåûYfllϨ/Ωˆ$Zõ≥æïU¥m@·¨OÔ Ú¥fiúÛÜl^탂'ãÏÏlsük.+Ë7˚üÇo øÜ9˜ôÀÓ˚V;¶?Å#I3eE¶߶KDÃÃóœd˝˜ˇ„¿9iÕ…√,úü¿ÒÖËUQËî  ÑâhªÖ<ÅXê.d
Ñ’·6'~ùkhu_}Ö9P∏I»o=C#$n?z}Î[1
»æºh≠ëØsè2z˛Á˙\än·LA"Sʈèdr%¢,£flÑl¡êt†
4Å.0,`
Ä3pfi ÑÄHñ.Hi@≤A>ÿ
A1ÿvÉjp‘Åz–NÇ6p\W¿

Sublime Text 2 shows output like this:

8950 4e47 0d0a 1a0a 0000 000d 4948 4452
0000 07d0 0000 07d0 0806 0000 009a 38c4
7900 000a 4169 4343 5049 4343 2050 726f
6669 6c65 0000 480d 9d96 7754 53d9 1687
cfbd 37bd d012 2220 25f4 1a7a 0920 d23b
4815 0451 8949 8050 0286 8426 7644 0546
1411 2956 6454 c001 4787 2263 4514 0b83
8262 d709 f210 50c6 c151 4445 e5dd 8c6b
09ef ad35 f3de 9afd c759 dfd9 e7b7 d7d9
67ef 7dd7 ba00 50fc 8204 c274 5801 8034
a158 14ee ebc1 5c12 13cb c4f7 0218 1001
0e58 01c0 e166 6604 47f8 4402 d4fc bd3d
9999 a848 c6b3 f6ee 2e80 64bb db2c bf50
2673 d6ff 7f91 2237 4324 0600 0a45 d536
3c7e 2617 e502 9453 b3c5 1932 ff04 caf4
9529 3286 3132 16a1 09a2 ac22 e3c4 af6c
f6a7 e62b bbc9 9897 26e4 a11a 59ce 19bc
349e 8cbb 50de 9a25 e1a3 8c04 a15c 9825
e067 a37c 0765 bd54 499a 00e5 f728 d3d3
f89c 4c00 3014 995f cce7 26a1 6c89 3245
1419 ee89 f202 0008 94c4 39bc 720e 8bf9
3968 9e00 78a6 67e4 8a04 8949 62a6 11d7
9869 e5e8 c866 faf1 b353 f962 312b 94c3
4de1 8878 4ccf f4b4 0c8e 3017 80af 6f96
4501 2559 6d99 6891 edad 1ced ed59 d6e6
68f9 bfd9 df1e 7e53 fd3d c87a fb55 f126

Both Atom and MacDown allow you to open images and videos but show no output in the editor or preview windows.

I think either you should not be able to open these files, because they are grayed out or a warning message appears telling you that is a file MacDown doesn't support, or a new text file should be created with that image or video linked. Something like ![FILENAME](/Users/aaronraimist/Pictures/...)

EDIT: GitHub messed up the first time I wrote this so if you got an email with only half of this message that is why.

uranusjr commented 10 years ago

This is pretty tricky. Originally MacDown was implemented to be able to open text files only, but I found out that many text editors out there doesn’t really handle UTI (which OS X application uses to identify file formats) well, and due to numerous requests (#7, #12, #15, #54, etc.) I decided that it’s better to just be able to open everything. And to take care of this issue MacDown needs to keep being able to open them in the future. But obviously the current behaviour when opening non-text document is not ideal. Something needs to be done.

The second solution (convert to link) you mentioned feels counter-intuitive to me because it doesn’t really open the original document, but creates another one. The first one (what Atom and Sublime Text 2 do) requires us to implement a separate document class (NSDocument subclass) for files we don’t really do anything about, and seems, although fine, an overkill to me.

Another way I can think of is to add extra checks in -readFromData:ofType:error:(NSError **), and if the data can’t be converted to string with UTF-8 (i.e. when -initWithData:encoding: returns nil, which results in an empty document) we just refuse to open it (maybe raise an error dialog as you mentioned). This won’t handle extreme edge cases in which arbitrary data happen to be valid UTF-8, but should stop most hazard with minimal work.

Thoughts?