forked from hashicorp/terraform-ls
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move descriptions to the documentation field, update to terraform-json 0.5.0 that has additional description support, etc.
- Loading branch information
Showing
21 changed files
with
376 additions
and
81 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package mdplain | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
) | ||
|
||
type replacement struct { | ||
re *regexp.Regexp | ||
sub string | ||
} | ||
|
||
var replacements = []replacement{ | ||
// rules heavily inspired by: https://github.com/stiang/remove-markdown/blob/master/index.js | ||
// back references were removed | ||
|
||
// Header | ||
{regexp.MustCompile(`\n={2,}`), "\n"}, | ||
// Fenced codeblocks | ||
{regexp.MustCompile(`~{3}.*\n`), ""}, | ||
// Strikethrough | ||
{regexp.MustCompile("~~"), ""}, | ||
// Fenced codeblocks | ||
{regexp.MustCompile("`{3}.*\\n"), ""}, | ||
// Remove HTML tags | ||
{regexp.MustCompile(`<[^>]*>`), ""}, | ||
// Remove setext-style headers | ||
{regexp.MustCompile(`^[=\-]{2,}\s*$`), ""}, | ||
// Remove footnotes? | ||
{regexp.MustCompile(`\[\^.+?\](\: .*?$)?`), ""}, | ||
{regexp.MustCompile(`\s{0,2}\[.*?\]: .*?$`), ""}, | ||
// Remove images | ||
{regexp.MustCompile(`\!\[(.*?)\][\[\(].*?[\]\)]`), "$1"}, | ||
// Remove inline links | ||
{regexp.MustCompile(`\[(.*?)\][\[\(].*?[\]\)]`), "$1"}, | ||
// Remove blockquotes | ||
{regexp.MustCompile(`^\s{0,3}>\s?`), ""}, | ||
// Remove reference-style links? | ||
{regexp.MustCompile(`^\s{1,2}\[(.*?)\]: (\S+)( ".*?")?\s*$`), ""}, | ||
// Remove atx-style headers | ||
{regexp.MustCompile(`^(\n)?\s{0,}#{1,6}\s+| {0,}(\n)?\s{0,}#{0,} {0,}(\n)?\s{0,}$`), "$1$2$3"}, | ||
// Remove emphasis (repeat the line to remove double emphasis) | ||
{regexp.MustCompile(`([*_]{1,3})([^\t\n\f\r *_].*?[^\t\n\f\r *_]{0,1})([*_]{1,3})`), "$2"}, | ||
{regexp.MustCompile(`([*_]{1,3})([^\t\n\f\r *_].*?[^\t\n\f\r *_]{0,1})([*_]{1,3})`), "$2"}, | ||
// Remove code blocks | ||
{regexp.MustCompile("(`{3,})(.*?)(`{3,})"), "$2"}, | ||
// Remove inline code | ||
{regexp.MustCompile("`(.+?)`"), "$1"}, | ||
// Replace two or more newlines with exactly two? Not entirely sure this belongs here... | ||
{regexp.MustCompile(`\n{2,}`), "\n\n"}, | ||
} | ||
|
||
// Clean runs a VERY naive cleanup of markdown text to make it more palatable as plain text. | ||
func Clean(markdown string) string { | ||
// TODO: maybe use https://github.com/russross/blackfriday/tree/v2, write custom renderer or | ||
// generate HTML then process that to plaintext using https://github.com/jaytaylor/html2text | ||
|
||
fmt.Printf("cleaning %q\n", markdown) | ||
|
||
result := markdown | ||
before := markdown | ||
|
||
for _, r := range replacements { | ||
result = r.re.ReplaceAllString(result, r.sub) | ||
if before != result { | ||
fmt.Printf("RE: %q, %q to %q\n", r.re, before, result) | ||
} | ||
before = result | ||
} | ||
|
||
return string(result) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package mdplain_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-ls/internal/mdplain" | ||
) | ||
|
||
func TestClean(t *testing.T) { | ||
for _, c := range []struct { | ||
markdown string | ||
expected string | ||
}{ | ||
{"", ""}, | ||
|
||
{"_foo_", "foo"}, | ||
{"__foo__", "foo"}, | ||
{"foo_bar", "foo_bar"}, | ||
|
||
{"*foo*", "foo"}, | ||
{"**foo**", "foo"}, | ||
{"Desc **2**", "Desc 2"}, | ||
{"1 * 3 = 3", "1 * 3 = 3"}, | ||
|
||
{"## Header", "Header"}, | ||
{"Header\n====\n\nSome text", "Header\n\nSome text"}, | ||
|
||
{"* item 1\n* item 2\n\n\nSome text", "* item 1\n* item 2\n\nSome text"}, | ||
} { | ||
t.Run(c.expected, func(t *testing.T) { | ||
actual := mdplain.Clean(c.markdown) | ||
|
||
if c.expected != actual { | ||
t.Fatalf("expected:\n%s\n\ngot:\n%s\n", c.expected, actual) | ||
} | ||
}) | ||
} | ||
} |
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.