-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Prevent Manually Added Content in CLI Docs to be Overwritten #9125
Merged
leecalcote
merged 14 commits into
meshery:master
from
aboobakersiddiqr63:aboobakersiddiqr63/docs-manualcont-changes
Nov 22, 2023
Merged
Changes from 9 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
6fd171b
fix: Prevent Manually Added Content To be Overwritten
aboobakersiddiqr63 26136e8
Added test for the getManuallyAddedContentMap method
aboobakersiddiqr63 ca6353f
Minor change in test.doc passed filename directly
aboobakersiddiqr63 73d569d
Added Documentation Content to Preserve Manually Added Content
aboobakersiddiqr63 3977467
Added hyperlink to CLI Documentation to Preserver Manually Added Content
aboobakersiddiqr63 d88c6dc
minor change
aboobakersiddiqr63 b43c313
Empty Commit
aboobakersiddiqr63 997e5eb
Changed the logic to check for {% include}
aboobakersiddiqr63 1322fda
Added Docs Changes after the logic change
aboobakersiddiqr63 f34721d
Added example oh how to preserve the manually added content
aboobakersiddiqr63 979747d
Merge branch 'master' into aboobakersiddiqr63/docs-manualcont-changes
alphaX86 bfb8611
Merge branch 'master' into aboobakersiddiqr63/docs-manualcont-changes
alphaX86 3c38c86
Edit doc page to reflect the instructions clear
alphaX86 b623613
Fix docs bug
alphaX86 File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -22,6 +22,7 @@ import ( | |||||
"os" | ||||||
"path" | ||||||
"path/filepath" | ||||||
"regexp" | ||||||
"strings" | ||||||
"time" | ||||||
|
||||||
|
@@ -133,7 +134,7 @@ func printOptions(buf *bytes.Buffer, cmd *cobra.Command) error { | |||||
} | ||||||
|
||||||
// GenMarkdownCustom creates custom markdown output. | ||||||
func GenMarkdownCustom(cmd *cobra.Command, w io.Writer) error { | ||||||
func GenMarkdownCustom(cmd *cobra.Command, w io.Writer, manuallyAddedContent map[int]string) error { | ||||||
// InitDefaultHelpCmd add the help command to the command tree. | ||||||
cmd.InitDefaultHelpCmd() | ||||||
// InitDefaultHelpFlag add the help flag to the command tree. | ||||||
|
@@ -176,6 +177,7 @@ func GenMarkdownCustom(cmd *cobra.Command, w io.Writer) error { | |||||
buf.WriteString(strings.Replace(examples[i], "// ", "", -1) + "\n") | ||||||
} else { | ||||||
// Code Block Line | ||||||
|
||||||
buf.WriteString(fmt.Sprintf("<pre class='codeblock-pre'>\n<div class='codeblock'>\n%s\n\n</div>\n</pre> \n\n", examples[i])) | ||||||
} | ||||||
} | ||||||
|
@@ -197,6 +199,13 @@ func GenMarkdownCustom(cmd *cobra.Command, w io.Writer) error { | |||||
buf.WriteString("\n\n") | ||||||
} | ||||||
|
||||||
if len(manuallyAddedContent) > 0 { | ||||||
for _, content := range manuallyAddedContent { | ||||||
buf.WriteString("\n") | ||||||
buf.WriteString("{% include " + content + " %}" + "\n") | ||||||
} | ||||||
} | ||||||
|
||||||
if hasSeeAlso(cmd) { | ||||||
buf.WriteString("## See Also\n\n") | ||||||
if cmd.HasParent() { | ||||||
|
@@ -206,9 +215,10 @@ func GenMarkdownCustom(cmd *cobra.Command, w io.Writer) error { | |||||
} | ||||||
}) | ||||||
} | ||||||
buf.WriteString("Go back to [command reference index](/reference/mesheryctl/) ") | ||||||
buf.WriteString("Go back to [command reference index](/reference/mesheryctl/), if you want to add content manually to the CLI documentation, please refer to the [instruction](/project/contributing/contributing-cli#preserving-manually-added-documentation-with-cobra-cli) for guidance.") | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
buf.WriteString("\n") | ||||||
} | ||||||
|
||||||
if !cmd.DisableAutoGenTag { | ||||||
buf.WriteString("###### Auto generated by spf13/cobra on " + time.Now().Format("2-Jan-2006") + "\n") | ||||||
} | ||||||
|
@@ -243,6 +253,9 @@ func GenMarkdownTreeCustom(cmd *cobra.Command, dir string, filePrepender, linkHa | |||||
|
||||||
basename := strings.Replace(cmd.CommandPath(), " ", "-", -1) + ".md" | ||||||
filename := filepath.Join(dir, basename) | ||||||
|
||||||
manuallyAddedContent, _ := getManuallyAddedContentMap(filename) | ||||||
|
||||||
f, err := os.Create(filename) | ||||||
if err != nil { | ||||||
return err | ||||||
|
@@ -254,13 +267,37 @@ func GenMarkdownTreeCustom(cmd *cobra.Command, dir string, filePrepender, linkHa | |||||
return err | ||||||
} | ||||||
|
||||||
err = GenMarkdownCustom(cmd, f) | ||||||
err = GenMarkdownCustom(cmd, f, manuallyAddedContent) | ||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
return nil | ||||||
} | ||||||
|
||||||
func getManuallyAddedContentMap(filename string) (map[int]string, error) { | ||||||
manuallyAddedContentMap := make(map[int]string) | ||||||
|
||||||
_, err := os.Stat(filename) | ||||||
if err != nil { | ||||||
return manuallyAddedContentMap, err | ||||||
} | ||||||
|
||||||
existingContent, err := os.ReadFile(filename) | ||||||
if err != nil { | ||||||
return manuallyAddedContentMap, err | ||||||
} | ||||||
|
||||||
content := string(existingContent) | ||||||
|
||||||
includePattern := regexp.MustCompile(`\{\%\s*include\s+([^%]+)\s+\%\}`) | ||||||
includeMatches := includePattern.FindAllStringSubmatch(content, -1) | ||||||
for i, match := range includeMatches { | ||||||
// Store the include content in the map with order as the key | ||||||
manuallyAddedContentMap[i] = strings.TrimSpace(match[1]) | ||||||
} | ||||||
return manuallyAddedContentMap, nil | ||||||
} | ||||||
|
||||||
// GenYamlTreeCustom creates custom yaml output. | ||||||
func GenYamlTreeCustom(cmd *cobra.Command, dir string, filePrepender, linkHandler func(string) string) error { | ||||||
for _, c := range cmd.Commands() { | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
{% raw %}{% include mesheryctl/ %}{% endraw %}