Skip to content

Instantly share code, notes, and snippets.

@ttscoff
Last active February 26, 2024 07:05
Show Gist options
  • Star 45 You must be signed in to star a gist
  • Fork 18 You must be signed in to fork a gist
  • Save ttscoff/3900158 to your computer and use it in GitHub Desktop.
Save ttscoff/3900158 to your computer and use it in GitHub Desktop.
SearchLink creates Markdown links from automatic searches based on special syntax.
@n8henrie
Copy link

n8henrie commented Aug 9, 2013

Fixed it. The ! in the regex was breaking the ability to non-greedy match non-searchlink Markdown urls. By taking the ! out of the regex you match all urls, then only process ones where the first character of the link is !.

My suggested fix for lines 163-232:

input.gsub!(/\[(.*?)\]\((.+?)\)/) do |match|
    link_text = $1
    link_info = $2
    search_type = ''
    search_terms = ''
    if link_info[0,1] == '!'
            if link_info =~ /\!(.+) "(.*?)"$/
                search_type = $1
                search_terms = $2
            else
                search_word = link_info.match(/\!(.+)/)
                search_type = search_word[1] unless search_word.nil?
                search_terms = link_text
            end

            url = false
            title = false

            case search_type
            when /^a$/
                az_url, title = google("site:amazon.com #{search_terms}", false)
                url, title = amazon_affiliatize(az_url, amazon_partner)

            when /^g$/ # google lucky search
                url, title = google(search_terms)

            when /^wiki$/
                url, title = wiki(search_terms)

            when /^def$/ # wikipedia/dictionary search
                # title, definition, definition_link, wiki_link = zero_click(search_terms)
                # if search_type == 'def' && definition_link != ''
                #   url = definition_link
                #   title = definition.gsub(/'+/,"'")
                # elsif wiki_link != ''
                #   url = wiki_link
                #   title = "Wikipedia: #{title}"
                # end
                url, title = google("define " + search_terms, true)

            when /^masd?$/ # Mac App Store search (mas = itunes link, masd = developer link)
                dev = search_type =~ /d$/
                url, title = itunes('macSoftware',search_terms, dev, itunes_affiliate)

            when /^itud?$/ # iTunes app search
                dev = search_type =~ /d$/
                url, title = itunes('iPadSoftware',search_terms, dev, itunes_affiliate)

            when /^s$/ # software search (google)
                url, title = google("(software OR app OR mac) #{search_terms}")
                link_text = title if link_text == ''

            when /^isong$/ # iTunes Song Search
                url, title = itunes('song', search_terms, false)

            when /^iart$/ # iTunes Artist Search
                url, title = itunes('musicArtist', search_terms, false)

            when /^ialb$/ # iTunes Album Search
                url, title = itunes('album', search_terms, false)

            when /^lsong$/ # Last.fm Song Search
                url, title = lastfm('track', search_terms)

            when /^lart$/ # Last.fm Artist Search
                url, title = lastfm('artist', search_terms)
            else
                if search_type =~ /.+?\.\w{2,4}$/
                    url, title = google("site:#{search_type} #{search_terms}")
                end
            end
        end

@jdkram
Copy link

jdkram commented Jan 17, 2014

Thanks for this.

I was looking up Amazon affiliate links and different sources claim different information is required in the affiliate URL. This article makes a claim for much shorter links which only include the affiliate tag.

@dean-s-oliver
Copy link

The google scholar search !gs does not seem to be working correctly. No matter what paper I search for, the result is a 2009 Supreme Court decision.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment