gomarkdown / markdown

markdown parser and HTML renderer for Go
Other
1.37k stars 172 forks source link

Suffx struct member for CrossRerefence #223

Closed miekg closed 2 years ago

miekg commented 2 years ago

I think I need a Suffix byte slice in CrossReference, see https://github.com/mmarkdown/mmark/issues/173#issuecomment-1046577657 (although not sure yet).

This would mean the following code wise:

--- /home/miek/src/github.com/gomarkdown/markdown/ast/node.go   2021-11-05 09:51:19.166656752 +0100
+++ ast/node.go 2022-02-22 10:18:57.123281334 +0100
@@ -272,6 +272,7 @@
    Container

    Destination []byte // Destination is where the reference points to
+   Suffix      []byte // Potential citation suffix, i.e. (#id, see title)
 }

 // Citation is a citation node.

and

--- /home/miek/src/github.com/gomarkdown/markdown/parser/ref.go 2021-11-05 09:51:19.178656558 +0100
+++ parser/ref.go   2022-02-22 10:25:14.595734744 +0100
@@ -26,7 +26,7 @@
            case c == ')':
                break Loop
            case !isAlnum(c):
-               if c == '_' || c == '-' || c == ':' {
+               if c == '_' || c == '-' || c == ':' || c == ',' || c == ' ' { // allow ',' and ' ' for any suffixes
                    i++
                    continue
                }
@@ -43,9 +43,19 @@
        }

        id := data[2:i]
-       node := &ast.CrossReference{}
-       node.Destination = id
+       // ',' and ' ' or only valid in the suffix, so check if we have one and then re-validate
+       comma := bytes.Index(id, []byte{','})
+       if comma < 0 && bytes.Contains(id, []byte{' '}) {
+           return 0, nil
+       }
+       suffix := []byte{}
+       if comma > 0 {
+           suffix = id[comma+1:]
+           suffix = bytes.TrimSpace(suffix)
+           id = id[:comma]
+       }

+       node := &ast.CrossReference{Destination: id, Suffix: suffix}
        return i + 1, node

    case '!': // index
kjk commented 2 years ago

lgtm

miekg commented 2 years ago

I'm holding off to making this change to actually see where the IETF XML3 is heading towards.

Closing for now.