How can I make spotlight index markdown files?
Asked Answered
F

5

20

I typically use the .markdown or .md extension for markdown documents. Unfortunately spotlight refuses to index them unless they have the .txt file extension.

I've seen a possible solution involving editing Info.plist files on the textmate blog. Is there a better way?

Update: I just discovered QuickLook generator for Markdown files which adds spotlight support and nice HTML quicklook previews. It works a treat!

Fiora answered 13/12, 2008 at 20:21 Comment(2)
This doesn't seem "programming related", but I sure am glad the "watchdogs" didn't shoot you down! I needed this info. +1.Philoctetes
The current QL gen project is found here: github.com/toland/qlmarkdown . The old one (linked to above) did not like XCode 7...Legist
S
6

You can do this without disabling SIP by creating a copy of the system RichText.mdimporter, modifying its Info.plist and saving it in /Library/Spotlight.

cp -r /System/Library/Spotlight/RichText.mdimporter .
patch -p2 RichText.mdimporter/Contents/Info.plist < Markdown.patch
mv RichText.mdimporter Markdown.mdimporter
sudo cp -R Markdown.mdimporter /Library/Spotlight
mdimport -r /Library/Spotlight/Markdown.mdimporter

Markdown.patch

diff -ru RichText.mdimporter/Contents/Info.plist Markdown.mdimporter/Contents/Info.plist
--- RichText.mdimporter/Contents/Info.plist 2015-11-23 16:14:12.000000000 +0200
+++ Markdown.mdimporter/Contents/Info.plist 2015-11-23 16:10:03.000000000 +0200
@@ -13,15 +13,7 @@
            <string>MDImporter</string>
            <key>LSItemContentTypes</key>
            <array>
-               <string>public.rtf</string>
-               <string>public.html</string>
-               <string>public.xml</string>
-               <string>public.plain-text</string>
-               <string>com.apple.traditional-mac-plain-text</string>
-               <string>com.apple.rtfd</string>
-               <string>com.apple.webarchive</string>
-               <string>org.oasis-open.opendocument.text</string>
-               <string>org.openxmlformats.wordprocessingml.document</string>
+               <string>net.daringfireball.markdown</string>
            </array>
        </dict>
    </array>
@@ -30,11 +22,11 @@
    <key>CFBundleGetInfoString</key>
    <string>1.0, Copyright (c) 2004-2010 Apple Inc.</string>
    <key>CFBundleIdentifier</key>
-   <string>com.apple.MDImporter.RichText</string>
+   <string>com.apple.MDImporter.Markdown</string>
    <key>CFBundleInfoDictionaryVersion</key>
    <string>6.0</string>
    <key>CFBundleName</key>
-   <string>Rich Text Sniffer</string>
+   <string>Markdown Sniffer</string>
    <key>CFBundleShortVersionString</key>
    <string>6.9</string>
    <key>CFBundleSupportedPlatforms</key>
Stalemate answered 23/11, 2015 at 14:27 Comment(2)
All you really need to do is copy rich text and add <string>net.daringfireball.markdown</string>, but either way, this answer works like a charm. Thanks!Sodamide
@Sodamide Yes that worked for me on High Sierra. cp -r /System/Library/Spotlight/RichText.mdimporter ., then add that line, then sudo cp -R RichText.mdimporter /Library/Spotlight and mdimport -r /Library/Spotlight/RichText.mdimporterMaxey
M
4

You'll have to write a Spotlight importer. There's an Xcode project template that will set the basic stuff up for you and get you started; I think there's also a developer example.

Your importer will need a UTExportedTypeDeclarations section in its Info.plist that describes a Uniform Type Identifier for markdown files with you path extension. Then it's just a matter of having your importer pass Spotlight the appropriate data for a markdown file.

Menorah answered 13/12, 2008 at 20:25 Comment(0)
L
2

The following works for Mac OS X 10.11 El Capitan:

As we cannot edit system files in El Capitan, we have to temporary disable SIP (System Integrity Protection) (for full details have a look at this Lifehacker article).

  • Reboot your Mac into Recovery Mode by restarting your computer and holding down command + R until the Apple logo appears on your screen
  • Click Utilities > Terminal
  • In the Terminal window, type in csrutil disable and press Enter
  • Restart your Mac

Now we can edit the file located at /System/Library/Spotlight/RichText.mdimporter/Contents/Info.plist:

  • sudo open -a TextEdit /System/Library/Spotlight/RichText.mdimporter/Contents/Info.plist
  • Add <string>net.daringfireball.markdown</string> under LSItemContentTypes (for more infos see original post)

When you are finished you may want to reindex the folders containing the Markdown files, have a look at this article for that: Spotlight: How to re-index folders or volumes. And don’t forget to turn SIP back on by repeating the first part and running csrutil enable.

Limon answered 29/10, 2015 at 1:53 Comment(0)
I
0

This gist explains how to modify the RichText.mdimporter to import source and markdown files.

Iterative answered 10/4, 2014 at 21:26 Comment(0)
D
-5

You could write a Importer, but there's an easier way. Markdown is just text, which Spotlight handles. If you give your markdown files the extension ".txt", Spotlight will pick them up.

In my case, I just named mine all to end with ".mdwn.txt" and taught emacs' markdown-mode to activate for files matching this extension.

Dacron answered 13/12, 2008 at 21:2 Comment(1)
The OP specifically shrugged off this approach in his question.Tourniquet

© 2022 - 2024 — McMap. All rights reserved.