diff --git a/relnote/relnote.go b/relnote/relnote.go index 37bd88f0..10c21336 100644 --- a/relnote/relnote.go +++ b/relnote/relnote.go @@ -151,34 +151,47 @@ func Merge(fsys fs.FS) (*md.Document, error) { if err != nil { return nil, err } - doc := &md.Document{} + doc := &md.Document{Links: map[string]*md.Link{}} for _, filename := range filenames { - fd, err := parseFile(fsys, filename) + newdoc, err := parseFile(fsys, filename) if err != nil { return nil, err } - if len(fd.Blocks) == 0 { + if len(newdoc.Blocks) == 0 { continue } if len(doc.Blocks) > 0 { // Put a blank line between the current and new blocks. lastLine := lastBlock(doc).Pos().EndLine - delta := lastLine + 2 - fd.Blocks[0].Pos().StartLine - for _, b := range fd.Blocks { + delta := lastLine + 2 - newdoc.Blocks[0].Pos().StartLine + for _, b := range newdoc.Blocks { addLines(b, delta) } } // Append non-empty blocks to the result document. - for _, b := range fd.Blocks { + for _, b := range newdoc.Blocks { if _, ok := b.(*md.Empty); !ok { doc.Blocks = append(doc.Blocks, b) } } - // TODO(jba): merge links + // Merge link references. + for key, link := range newdoc.Links { + if doc.Links[key] != nil { + return nil, fmt.Errorf("duplicate link reference %q; second in %s", key, filename) + } + doc.Links[key] = link + } // TODO(jba): add headings for package sections under "Minor changes to the library". } // Remove headings with empty contents. doc.Blocks = removeEmptySections(doc.Blocks) + if len(doc.Blocks) > 0 && len(doc.Links) > 0 { + // Add a blank line to separate the links. + lastPos := doc.Blocks[len(doc.Blocks)-1].Pos() + lastPos.StartLine += 2 + lastPos.EndLine += 2 + doc.Blocks = append(doc.Blocks, &md.Empty{Position: lastPos}) + } return doc, nil } diff --git a/relnote/testdata/links.txt b/relnote/testdata/links.txt new file mode 100644 index 00000000..454ea4d4 --- /dev/null +++ b/relnote/testdata/links.txt @@ -0,0 +1,23 @@ +-- f1.md -- +First. + +Second. + +[a]: u1 +[b]: u2 +-- f2.md -- +Third. + +[c]: u3 +[d]: u4 +-- want -- +First. + +Second. + +Third. + +[a]: u1 +[b]: u2 +[c]: u3 +[d]: u4