Skip to content

Commit b0e1e99

Browse files
authored
Merge pull request #51 from AryaHassanli/disco-fix-initial-xref
Auto-insert basic xrefstyle attribute before first section
2 parents 453cc52 + fc37d5a commit b0e1e99

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

disco/baller.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,34 @@ func (b *Baller) disco(cxt context.Context, doc *asciidoc.Document) error {
133133
}
134134

135135
func (b *Baller) discoBallTopLevelSection(dc *discoContext, top *asciidoc.Section, docType matter.DocType) error {
136+
if b.options.AddXrefstyle {
137+
var xrefstyleEntry *asciidoc.AttributeEntry
138+
var topIndex int = -1
139+
140+
for i, el := range dc.doc.Elements {
141+
if el == top {
142+
topIndex = i
143+
break
144+
}
145+
if ae, ok := el.(*asciidoc.AttributeEntry); ok && ae.Name == "xrefstyle" {
146+
xrefstyleEntry = ae
147+
}
148+
}
149+
150+
if xrefstyleEntry == nil {
151+
ae := asciidoc.NewAttributeEntry("xrefstyle")
152+
ae.Elements = asciidoc.Elements{asciidoc.NewString("basic")}
153+
if topIndex != -1 {
154+
dc.doc.Elements = append(dc.doc.Elements, nil, nil)
155+
copy(dc.doc.Elements[topIndex+2:], dc.doc.Elements[topIndex:])
156+
dc.doc.Elements[topIndex] = ae
157+
dc.doc.Elements[topIndex+1] = &asciidoc.NewLine{}
158+
} else {
159+
dc.doc.Elements = append(asciidoc.Elements{ae, &asciidoc.NewLine{}}, dc.doc.Elements...)
160+
}
161+
}
162+
}
163+
136164
if b.options.ReorderSections {
137165
sectionOrder, ok := matter.TopLevelSectionOrders[docType]
138166
if !ok {

disco/option.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type DiscoOptions struct {
2222
NormalizeAnchors bool `default:"false" aliases:"normalizeAnchors" help:"rewrite anchors and references without labels" group:"Discoballing:"`
2323
RemoveMandatoryFallbacks bool `default:"true" aliases:"removeMandatoryFallbacks" help:"remove fallback values for mandatory fields" group:"Discoballing:"`
2424
RenameSections bool `default:"false" help:"rename sections to disco-ball standard names" group:"Discoballing:"`
25+
AddXrefstyle bool `default:"true" aliases:"addXrefstyle" help:"add xrefstyle attribute before the first section" group:"Discoballing:"`
2526
}
2627

2728
var DefaultOptions = DiscoOptions{
@@ -44,4 +45,5 @@ var DefaultOptions = DiscoOptions{
4445
NormalizeAnchors: false,
4546
RemoveMandatoryFallbacks: true,
4647
RenameSections: false,
48+
AddXrefstyle: true,
4749
}

0 commit comments

Comments
 (0)