~kota/goldmark-gemtext

111d944b702c61575334eba9e275886cf2ba6abf — Dakota Walsh 4 months ago 608984f master v0.3.2
implement image link replacers
5 files changed, 23 insertions(+), 8 deletions(-)

M inline.go
M option.go
M render.go
M render_test.go
M test_data/renderLinkReplacers.gmi
M inline.go => inline.go +8 -1
@@ 64,8 64,15 @@ func (r *GemRenderer) renderEmphasis(w util.BufWriter, source []byte, node ast.N
func (r *GemRenderer) renderImage(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
	n := node.(*ast.Image)
	if entering {
		// Apply link replacers.
		destination := n.Destination
		for _, r := range r.config.LinkReplacers {
			s := r.replace(string(destination), LinkImage)
			destination = []byte(s)
		}

		fmt.Fprintf(w, "=> ")
		fmt.Fprintf(w, "%s ", n.Destination)
		fmt.Fprintf(w, "%s ", destination)
	} else {
		if n.NextSibling() != nil && n.FirstChild() != nil {
			fmt.Fprintf(w, "\n")

M option.go => option.go +2 -0
@@ 212,6 212,8 @@ const (
	// goldmark extension to detect this type of link:
	// https://git.sr.ht/~kota/goldmark-wiki
	LinkWiki
	// LinkImage is a markdown image link.
	LinkImage
)

// Set LinkReplacers.

M render.go => render.go +2 -1
@@ 101,7 101,8 @@ func linkOnly(source []byte, node ast.Node) bool {
}

// linkPrint is a helper function that prints a link's text to a writer, applies
// any regex replacers.
// any regex replacers. Images are not handled by this function as they operate
// slightly differently.
// Returns false if a link was not printed.
func linkPrint(w io.Writer, source []byte, node ast.Node, replacers []LinkReplacer) bool {
	// I know the logic is nearly duplicated in *ast.Link and *wast.Wiki, but I

M render_test.go => render_test.go +5 -0
@@ 94,6 94,11 @@ func TestNew(t *testing.T) {
					regexp.MustCompile(`https?`),
					"autolinks",
				},
				{
					LinkImage,
					regexp.MustCompile(`https?`),
					"image",
				},
			}),
		},
	}

M test_data/renderLinkReplacers.gmi => test_data/renderLinkReplacers.gmi +6 -6
@@ 151,16 151,16 @@ Paragraph with a few links in it.

## Images

=> https://octodex.github.com/images/minion.png Minion
=> https://octodex.github.com/images/stormtroopocat.jpg Stormtroopocat
=> image://octodex.github.com/images/minion.png Minion
=> image://octodex.github.com/images/stormtroopocat.jpg Stormtroopocat

=> https://octodex.github.com/images/minion.png 1
=> https://octodex.github.com/images/stormtroopocat.jpg 2
=> https://octodex.github.com/images/minion.png 3
=> image://octodex.github.com/images/minion.png 1
=> image://octodex.github.com/images/stormtroopocat.jpg 2
=> image://octodex.github.com/images/minion.png 3

Like links, Images also have a footnote style syntax

=> https://octodex.github.com/images/dojocat.jpg Alt text
=> image://octodex.github.com/images/dojocat.jpg Alt text

With a reference later in the document defining the URL location: