Skip to content

Commit

Permalink
Fix empty links appearing before code links
Browse files Browse the repository at this point in the history
Bumped version
Updated README
  • Loading branch information
chamilad committed Oct 19, 2019
1 parent b780226 commit 2adbc21
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Makefile
BINARY="m2h"
TARGET="build"
VERSION="v0.4-snapshot"
VERSION="v0.4"

export GO111MODULE=on
LDFLAGS=-ldflags "-extldflags '-static' -s -w"
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,12 @@ Following are the fork specific features.
* Convert preformatted code blocks correctly by parsing embedded line break tags
* Corrects Medium export glitch where an empty line within a preformatted block generates two preformatted blocks
* Convert Slideshare Medium embeds to HTML embeds within Markdown.
* Convert Twitter Medium embeds to Tweet embeds (using Hugo Shortcodes)
* Handle edge cases like bolded inline code which doesn't get converted well during Hugo site generation
* Render `figcaption`
* Customized footer from Medium export information

> Hugo shortcodes are not used as a way to embed external resources like Gists to keep the Markdown generation reusable across different static site generators.
> The use of Hugo shortcodes as a way to embed external resources like Gists were kept to a minimum to keep the Markdown generation reusable across different static site generators.

## Usage
Expand Down
3 changes: 0 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
//
package main

// TODO:
// 1. process embeds other than gists

import (
"flag"
"fmt"
Expand Down
42 changes: 36 additions & 6 deletions rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ import (
var ruleOverrides = []md.Rule{
// converter rule to convert github gists to markdown code blocks
{
//<figure name="3f51" id="3f51" class="graf graf--figure graf--iframe graf-after--p"><script src="https://gist.github.com/chamilad/63cfa08c052e795c8e95bb7b43643f6a.js"></script></figure>
//<figure name="3f51" id="3f51" class="graf graf--figure graf--iframe graf-after--p">
// <script src="https://gist.github.com/chamilad/63cfa08c052e795c8e95bb7b43643f6a.js"></script>
// </figure>
Filter: []string{"script"},
Replacement: func(content string, selec *goquery.Selection, options *md.Options) *string {
codeContentType := ""
Expand Down Expand Up @@ -119,7 +121,12 @@ var ruleOverrides = []md.Rule{
}

// otherwise render a markdown code block with content type
codeblock := fmt.Sprintf("\n\n%s%s\n%s\n%s\n\n", options.Fence, codeContentType, codeContent, options.Fence)
codeblock := fmt.Sprintf(
"\n\n%s%s\n%s\n%s\n\n",
options.Fence,
codeContentType,
codeContent,
options.Fence)

printDot()
return md.String(codeblock)
Expand Down Expand Up @@ -182,15 +189,22 @@ var ruleOverrides = []md.Rule{

// convert slideshare links to proper iframe embeds
{
// <figure name="9af0" id="9af0" class="graf graf--figure graf--iframe graf-after--blockqu ote"><iframe src="https://www.slideshare.net/slideshow/embed_code/key/8br68UFQtb7qpF" width="600" height="500" frameborder="0" scrolling="no"></iframe></figure>
// <figure name="9af0" id="9af0" class="graf graf--figure graf--iframe graf-after--blockquote">
// <iframe src="https://www.slideshare.net/slideshow/embed_code/key/8br68UFQtb7qpF" width="600" height="500"
// frameborder="0" scrolling="no"></iframe>
// </figure>
Filter: []string{"iframe"},
Replacement: func(content string, selec *goquery.Selection, options *md.Options) *string {
src, exists := selec.Attr("src")
if !exists || !strings.Contains(src, "slideshare.net") {
return nil
}

return md.String(fmt.Sprintf("<iframe src=\"%s\" width=\"595\" height=\"485\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\" style=\"border:1px solid #CCC; border-width:1px; margin-bottom:5px; \" allowfullscreen> </iframe>\n", src))
return md.String(fmt.Sprintf(
"<iframe src=\"%s\" width=\"595\" height=\"485\" frameborder=\"0\" marginwidth=\"0\" "+
"marginheight=\"0\" scrolling=\"no\" style=\"border:1px solid #CCC; border-width:1px; "+
"margin-bottom:5px; \" allowfullscreen> </iframe>\n",
src))

},
AdvancedReplacement: nil,
Expand Down Expand Up @@ -264,14 +278,17 @@ var ruleOverrides = []md.Rule{
}

imgSrc, _ := selec.Attr("src")
fHtml := fmt.Sprintf("<figure><img src=\"%s\"><figcaption>%s</figcaption></figure>", imgSrc, figcaption.Text())
fHtml := fmt.Sprintf(
"<figure><img src=\"%s\"><figcaption>%s</figcaption></figure>\n",
imgSrc,
figcaption.Text())

return md.String(fHtml)
},
AdvancedReplacement: nil,
},

// remove figcaption
// remove figcaption since this will be processed during the img tag processing
{
Filter: []string{"figcaption"},
Replacement: func(content string, selec *goquery.Selection, options *md.Options) *string {
Expand Down Expand Up @@ -317,6 +334,19 @@ var ruleOverrides = []md.Rule{
},
AdvancedReplacement: nil,
},

// empty hrefs
{
Filter: []string{"a"},
Replacement: func(content string, selec *goquery.Selection, options *md.Options) *string {
if strings.TrimSpace(selec.Text()) != "" {
return nil
}

return md.String("")
},
AdvancedReplacement: nil,
},
}

// readCodeContent reads the text of a given Selection, honouring the br tags found within the text. This is
Expand Down

0 comments on commit 2adbc21

Please sign in to comment.