Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions cmd/app/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,15 @@ func exec(ctx context.Context, vip *viper.Viper) error {
pluginTransformations = append(pluginTransformations, personaPlugin.PluginNodeTransformations()...)
}
if options.Alias.AliasesEnabled {
aliasPlugin := alias.Alias{}
aliasPlugin := alias.Alias{SectionFilesName: options.Hugo.SectionFilesName}
pluginTransformations = append(pluginTransformations, aliasPlugin.PluginNodeTransformations()...)
}
if options.Markdown.MarkdownEnabled {
markdownPlugin := manifestmarkdown.Markdown{}
pluginTransformations = append(pluginTransformations, markdownPlugin.PluginNodeTransformations()...)
}
if options.Docsy.EditThisPageEnabled {
docsyPlugin := docsy.Docsy{}
docsyPlugin := docsy.Docsy{SectionFilesName: options.Hugo.SectionFilesName}
pluginTransformations = append(pluginTransformations, docsyPlugin.PluginNodeTransformations()...)
}

Expand All @@ -93,7 +93,7 @@ func exec(ctx context.Context, vip *viper.Viper) error {

additionalNodePlugins := []nodeplugins.Interface{}
if options.Persona.PersonaFilterEnabled {
additionalNodePlugins = append(additionalNodePlugins, &personanodeplugin.Plugin{Root: documentNodes[0], Writer: config.Writer})
additionalNodePlugins = append(additionalNodePlugins, &personanodeplugin.Plugin{Root: documentNodes[0], Writer: config.Writer, IndexFileNames: config.Hugo.IndexFileNames, SectionFilesName: config.Hugo.SectionFilesName})
}
// Stage 1
reactorWGStage1 := &sync.WaitGroup{}
Expand Down
4 changes: 4 additions & 0 deletions cmd/app/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ func configureFlags(command *cobra.Command, vip *viper.Viper) {
"When building a Hugo-compliant documentation bundle, files with filename matching one form this list (in that order) will be renamed to _index.md. Only useful with --hugo=true")
_ = vip.BindPFlag("hugo-section-files", command.Flags().Lookup("hugo-section-files"))

command.Flags().String("hugo-section-files-name", "_index.md",
"The output filename for section/index files. Files matching --hugo-section-files are renamed to this value. Set to 'index.md' for VitePress compatibility. Only useful with --hugo=true")
_ = vip.BindPFlag("hugo-section-files-name", command.Flags().Lookup("hugo-section-files-name"))

command.Flags().StringSlice("content-files-formats", []string{},
"Supported content format extensions (example: .md)")
_ = vip.BindPFlag("content-files-formats", command.Flags().Lookup("content-files-formats"))
Expand Down
5 changes: 3 additions & 2 deletions cmd/app/initilization.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,9 @@ func getReactorConfig(options Options, hugo hugo.Hugo, rhs []repositoryhost.Inte
}

config.Writer = &writers.FSWriter{
Root: config.DestinationPath,
Hugo: config.Hugo.Enabled,
Root: config.DestinationPath,
Hugo: config.Hugo.Enabled,
SectionFilesName: config.Hugo.SectionFilesName,
}

if len(config.GhInfoDestination) > 0 {
Expand Down
1 change: 1 addition & 0 deletions cmd/hugo/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ type Hugo struct {
PrettyURLs bool `mapstructure:"hugo-pretty-urls"`
BaseURL string `mapstructure:"hugo-base-url"`
IndexFileNames []string `mapstructure:"hugo-section-files"`
SectionFilesName string `mapstructure:"hugo-section-files-name"`
HugoStructuralDirs []string `mapstructure:"hugo-structural-dirs"`
}
16 changes: 11 additions & 5 deletions pkg/manifest/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ func (n *Node) NodePath() string {
return must.Succeed(link.Build(n.Path, n.Name()))
}

// HugoPrettyPath returns hugo pretty path
func (n *Node) HugoPrettyPath() string {
// HugoPrettyPath returns hugo pretty path using the configured index file names
// and section files name to trim index suffixes from URLs.
func (n *Node) HugoPrettyPath(indexFileNames []string, sectionFilesName string) string {
name := n.Name()
if n.Type == "dir" {
return must.Succeed(link.Build(n.Path, name, "/"))
Expand All @@ -66,9 +67,14 @@ func (n *Node) HugoPrettyPath() string {
return must.Succeed(link.Build(n.Path, name))
}
name = strings.TrimSuffix(name, ".md")
name = strings.TrimSuffix(name, "_index")
// TODO: use IndexFileNames instead
name = strings.TrimSuffix(name, "README")
if sectionFilesName != "" {
name = strings.TrimSuffix(name, strings.TrimSuffix(sectionFilesName, ".md"))
} else {
name = strings.TrimSuffix(name, "_index")
}
for _, ifn := range indexFileNames {
name = strings.TrimSuffix(name, strings.TrimSuffix(ifn, ".md"))
}
return must.Succeed(link.Build(n.Path, name, "/"))
}

Expand Down
14 changes: 10 additions & 4 deletions pkg/manifestplugins/alias/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,19 @@ import (
)

// Alias is the object representing the alias plugin
type Alias struct{}
type Alias struct {
SectionFilesName string
}

// PluginNodeTransformations returns the node transformations for the alias plugin
func (d *Alias) PluginNodeTransformations() []manifest.NodeTransformation {
return []manifest.NodeTransformation{calculateAliases}
return []manifest.NodeTransformation{d.calculateAliases}
}
func calculateAliases(node *manifest.Node, parent *manifest.Node, _ registry.Interface) (bool, error) {
func (d *Alias) calculateAliases(node *manifest.Node, parent *manifest.Node, _ registry.Interface) (bool, error) {
sectionFilesName := d.SectionFilesName
if sectionFilesName == "" {
sectionFilesName = "_index.md"
}
var (
nodeAliases []interface{}
childAliases []interface{}
Expand All @@ -37,7 +43,7 @@ func calculateAliases(node *manifest.Node, parent *manifest.Node, _ registry.Int
return false, fmt.Errorf("node \n\n%s\n has invalid alias format", child)
}
childAliasSuffix := strings.TrimSuffix(child.Name(), ".md")
if child.Name() == "_index.md" {
if child.Name() == sectionFilesName {
childAliasSuffix = ""
}
nodeAlias := fmt.Sprintf("%s", nodeAliasI)
Expand Down
14 changes: 10 additions & 4 deletions pkg/manifestplugins/docsy/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,22 @@ import (
)

// Docsy is the object representing the docsy plugin
type Docsy struct{}
type Docsy struct {
SectionFilesName string
}

// PluginNodeTransformations returns the node transformations for the docsy plugin
func (d *Docsy) PluginNodeTransformations() []manifest.NodeTransformation {
return []manifest.NodeTransformation{editThisPage}
return []manifest.NodeTransformation{d.editThisPage}
}

func editThisPage(node *manifest.Node, _ *manifest.Node, r registry.Interface) (bool, error) {
func (d *Docsy) editThisPage(node *manifest.Node, _ *manifest.Node, r registry.Interface) (bool, error) {
sectionFilesName := d.SectionFilesName
if sectionFilesName == "" {
sectionFilesName = "_index.md"
}
isNotFile := node.Type != "file"
isIndexFileWithoutSource := (node.File == "_index.md" || node.File == "index.md") && node.Source == ""
isIndexFileWithoutSource := node.File == sectionFilesName && node.Source == ""
hasMultipleSource := len(node.MultiSource) > 0
isNotMarkdown := node.Processor != "markdown"

Expand Down
2 changes: 1 addition & 1 deletion pkg/nodeplugins/markdown/document/document_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func (d *Worker) process(ctx context.Context, b *bytes.Buffer, n *manifest.Node)
}
}
frontmatter.MoveMultiSourceFrontmatterToTopDocument(docs)
frontmatter.ComputeNodeTitle(firstDoc, n, d.hugo.IndexFileNames, d.hugo.Enabled)
frontmatter.ComputeNodeTitle(firstDoc, n, d.hugo.IndexFileNames, d.hugo.Enabled, d.hugo.SectionFilesName)
frontmatter.MergeDocumentAndNodeFrontmatter(firstDoc, n)
}
for _, cnt := range fullContent {
Expand Down
15 changes: 9 additions & 6 deletions pkg/nodeplugins/markdown/document/frontmatter/frontmatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func MergeDocumentAndNodeFrontmatter(nodeAst NodeMeta, node *manifest.Node) {
// it is eligible to be index file, and then normalizes either
// as a title - removing `-`, `_`, `.md` and converting to title
// case.
func ComputeNodeTitle(nodeAst NodeMeta, node *manifest.Node, IndexFileNames []string, hugoEnabled bool) {
func ComputeNodeTitle(nodeAst NodeMeta, node *manifest.Node, IndexFileNames []string, hugoEnabled bool, sectionFilesName string) {
if !hugoEnabled || nodeAst == nil {
return
}
Expand All @@ -80,9 +80,9 @@ func ComputeNodeTitle(nodeAst NodeMeta, node *manifest.Node, IndexFileNames []st
}
title := node.Name()
// index node with parent
if nodeIsIndexFile(node.Name(), IndexFileNames) && node.Parent() != nil && node.Parent().Path != "" {
if nodeIsIndexFile(node.Name(), IndexFileNames, sectionFilesName) && node.Parent() != nil && node.Parent().Path != "" {
title = node.Parent().Name()
} else if nodeIsIndexFile(node.Name(), IndexFileNames) && node.Parent() != nil && node.Parent().Path == "" {
} else if nodeIsIndexFile(node.Name(), IndexFileNames, sectionFilesName) && node.Parent() != nil && node.Parent().Path == "" {
// root index node
title = "Root"
}
Expand All @@ -97,13 +97,16 @@ func ComputeNodeTitle(nodeAst NodeMeta, node *manifest.Node, IndexFileNames []st
}

// Compares a node name to the configured list of index file
// and a default name '_index.md' to determine if this node
// and the configured section files name to determine if this node
// is an index document node.
func nodeIsIndexFile(name string, IndexFileNames []string) bool {
func nodeIsIndexFile(name string, IndexFileNames []string, sectionFilesName string) bool {
for _, s := range IndexFileNames {
if strings.EqualFold(name, s) {
return true
}
}
return name == "_index.md"
if sectionFilesName == "" {
sectionFilesName = "_index.md"
}
return name == sectionFilesName
}
Original file line number Diff line number Diff line change
Expand Up @@ -174,15 +174,15 @@ var _ = Describe("Document frontmatter", func() {
Context("top level node", func() {
It("removes _,- and .md in the general case", func() {
node = nodes[1]
frontmatter.ComputeNodeTitle(nodeAst, node, indexFileNames, hugoEnabled)
frontmatter.ComputeNodeTitle(nodeAst, node, indexFileNames, hugoEnabled, "")
setMeta := nodeAst.SetMetaArgsForCall(0)
Expect(setMeta).To(Equal(map[string]interface{}{
"title": "File Node 1",
}))
})
It("has title Root if file is index", func() {
node = nodes[2]
frontmatter.ComputeNodeTitle(nodeAst, node, indexFileNames, hugoEnabled)
frontmatter.ComputeNodeTitle(nodeAst, node, indexFileNames, hugoEnabled, "")
setMeta := nodeAst.SetMetaArgsForCall(0)
Expect(setMeta).To(Equal(map[string]interface{}{
"title": "Root",
Expand All @@ -191,15 +191,15 @@ var _ = Describe("Document frontmatter", func() {
Context("node with parent", func() {
It("removes _,- and .md in the general case", func() {
node = nodes[4]
frontmatter.ComputeNodeTitle(nodeAst, node, indexFileNames, hugoEnabled)
frontmatter.ComputeNodeTitle(nodeAst, node, indexFileNames, hugoEnabled, "")
setMeta := nodeAst.SetMetaArgsForCall(0)
Expect(setMeta).To(Equal(map[string]interface{}{
"title": "File Node 2",
}))
})
It("uses parents name if file is index", func() {
node = nodes[5]
frontmatter.ComputeNodeTitle(nodeAst, node, indexFileNames, hugoEnabled)
frontmatter.ComputeNodeTitle(nodeAst, node, indexFileNames, hugoEnabled, "")
setMeta := nodeAst.SetMetaArgsForCall(0)
Expect(setMeta).To(Equal(map[string]interface{}{
"title": "Parent Dir",
Expand Down
2 changes: 1 addition & 1 deletion pkg/nodeplugins/markdown/linkresolver/link_resolving.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (l *LinkResolver) ResolveResourceLink(resourceLink string, node *manifest.N
// construct destination from node path
websiteLink := destinationNode.NodePath()
if l.Hugo.Enabled {
websiteLink = destinationNode.HugoPrettyPath()
websiteLink = destinationNode.HugoPrettyPath(l.Hugo.IndexFileNames, l.Hugo.SectionFilesName)
}
for _, structuralDir := range l.Hugo.HugoStructuralDirs {
websiteLink = strings.TrimPrefix(websiteLink, structuralDir+"/")
Expand Down
16 changes: 9 additions & 7 deletions pkg/nodeplugins/persona/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ var jsTemplate string

// Plugin is the node plugin object
type Plugin struct {
Root *manifest.Node
Writer writers.Writer
Root *manifest.Node
Writer writers.Writer
IndexFileNames []string
SectionFilesName string
}

// Processor returns the persona processor
Expand All @@ -33,7 +35,7 @@ func (Plugin) Processor() string {

// Process processes the node that will be constructed as the js file
func (p *Plugin) Process(node *manifest.Node) error {
linkToPersonaMap := urlToPersonas(p.Root, map[string]string{})
linkToPersonaMap := urlToPersonas(p.Root, map[string]string{}, p.IndexFileNames, p.SectionFilesName)
t, err := template.New("webpage").Parse(jsTemplate)
if err != nil {
log.Fatalf("Error parsing template: %v", err)
Expand All @@ -47,8 +49,8 @@ func (p *Plugin) Process(node *manifest.Node) error {
return p.Writer.Write(node.Name(), node.Path, renderedTemplate.Bytes(), node, []string{})
}

func urlToPersonas(node *manifest.Node, res map[string]string) map[string]string {
if node.Frontmatter["persona"] != nil && strings.HasPrefix(node.HugoPrettyPath(), "content") {
func urlToPersonas(node *manifest.Node, res map[string]string, indexFileNames []string, sectionFilesName string) map[string]string {
if node.Frontmatter["persona"] != nil && strings.HasPrefix(node.HugoPrettyPath(indexFileNames, sectionFilesName), "content") {
// bubble persona up
persona, ok := node.Frontmatter["persona"].(string)
must.BeTrue(ok)
Expand All @@ -57,7 +59,7 @@ func urlToPersonas(node *manifest.Node, res map[string]string) map[string]string
return res
}

url := node.HugoPrettyPath()
url := node.HugoPrettyPath(indexFileNames, sectionFilesName)
trimmedPath := strings.TrimPrefix(url, "content")
components := strings.Split(trimmedPath, "/")
var subpaths []string
Expand All @@ -77,7 +79,7 @@ func urlToPersonas(node *manifest.Node, res map[string]string) map[string]string
}
}
for _, child := range node.Structure {
urlToPersonas(child, res)
urlToPersonas(child, res, indexFileNames, sectionFilesName)
}
return res
}
17 changes: 11 additions & 6 deletions pkg/writers/fswriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,22 @@ import (

// FSWriter is implementation of Writer interface for writing blobs to the file system
type FSWriter struct {
Root string
Ext string
Hugo bool
Root string
Ext string
Hugo bool
SectionFilesName string
}

func (f *FSWriter) Write(name, path string, docBlob []byte, node *manifest.Node, IndexFileNames []string) error {
sectionFilesName := f.SectionFilesName
if sectionFilesName == "" {
sectionFilesName = "_index.md"
}
if slices.Contains(IndexFileNames, name) {
name = "_index.md"
name = sectionFilesName
}
//generate _index.md content
if f.Hugo && name == "_index.md" && node != nil && node.Frontmatter != nil && docBlob == nil {
//generate index content
if f.Hugo && name == sectionFilesName && node != nil && node.Frontmatter != nil && docBlob == nil {
buf := bytes.Buffer{}
_, _ = buf.Write([]byte("---\n"))
fm, err := yaml.Marshal(node.Frontmatter)
Expand Down
Loading