Skip to content

Commit

Permalink
Add a converter that finds gist URLs and converts these to gist short…
Browse files Browse the repository at this point in the history
…codes for Hugo

---

For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/ashishb/wp2hugo?shareId=XXXX-XXXX-XXXX-XXXX).
  • Loading branch information
lawrencegripper committed Sep 4, 2024
1 parent b64ec2c commit b66de0d
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/wp2hugo/internal/hugogenerator/hugo_generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,16 @@ func TestFootnote(t *testing.T) {
const expectedMarkdown = "Some text[^1] with a footnote\n\n[^1]: Here we are: the footnote."
assert.True(t, strings.Contains(hugoPage.Markdown(), expectedMarkdown))
}

func TestMarkdownExtractorWithLink1(t *testing.T) {
const sampleHTMLInput = `<p><a href="https://gist.github.com/lawrencegripper/8e701b0d201e65af0f8bc9b8b0b14207">Gist Link</a></p>`
const expectedMarkdownOutput = `{{< gist lawrencegripper 8e701b0d201e65af0f8bc9b8b0b14207 >}}`

url1, err := url.Parse("https://example.com")
assert.Nil(t, err)
page, err := NewPage(nil, *url1, "author", "Title", nil, false, nil, nil, nil, sampleHTMLInput, nil, nil)
assert.Nil(t, err)
md, err := page.getMarkdown(nil, sampleHTMLInput, nil)
assert.Nil(t, err)
assert.Equal(t, expectedMarkdownOutput, *md)
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ import (

var youtubeID = regexp.MustCompile(`youtube\.com/embed/([^\&\?\/]+)`)
var googleMapsID = regexp.MustCompile(`google\.com/maps/d/.*embed\?mid=([0-9A-Za-z-_]+)`)
var gistID = regexp.MustCompile(`gist\.github\.com/([^/]+)/([0-9a-f]+)`)

func getMarkdownConverter() *md.Converter {
converter := md.NewConverter("", true, nil)
converter.Use(getYouTubeForHugoConverter())
converter.Use(getGoogleMapsEmbedForHugoConverter())
converter.Use(convertCustomBRToNewline())
converter.Use(convertBrToNewline())
converter.Use(convertGistURLsToShortcodes())
return converter
}

Expand Down Expand Up @@ -75,3 +77,28 @@ func getGoogleMapsEmbedForHugoConverter() md.Plugin {
}
}
}

func convertGistURLsToShortcodes() md.Plugin {
return func(c *md.Converter) []md.Rule {
return []md.Rule{
{
Filter: []string{"a"},
Replacement: func(content string, selec *goquery.Selection, opt *md.Options) *string {
href := selec.AttrOr("href", "")
parts := gistID.FindStringSubmatch(href)
if len(parts) != 3 {
return nil
}
user := parts[1]
id := parts[2]
text := fmt.Sprintf("{{< gist %s %s >}}", user, id)
log.Debug().
Str("user", user).
Str("id", id).
Msg("Gist URL found")
return &text
},
},
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,20 @@ const _textWithIframe = `
<p><iframe src="https://www.google.com/maps/d/u/0/embed?mid=1lcjyzfxxXcdDP3XkrikfqIJryfFi4ZA&amp;ehbc=2E312F" width="640" height="480"></iframe></p>
`

const _textWithGist = `
<p><a href="https://gist.github.com/lawrencegripper/8e701b0d201e65af0f8bc9b8b0b14207">Gist Link</a></p>
`

func TestIframe(t *testing.T) {
converter := getMarkdownConverter()
result, err := converter.ConvertString(_textWithIframe)
assert.NoError(t, err)
assert.Contains(t, result, `{{< googlemaps src="1lcjyzfxxXcdDP3XkrikfqIJryfFi4ZA" width=640 height=480 >}}`)
}

func TestGist(t *testing.T) {
converter := getMarkdownConverter()
result, err := converter.ConvertString(_textWithGist)
assert.NoError(t, err)
assert.Contains(t, result, `{{< gist lawrencegripper 8e701b0d201e65af0f8bc9b8b0b14207 >}}`)
}

0 comments on commit b66de0d

Please sign in to comment.