├── .gitignore ├── LICENSE ├── README.md ├── cmd └── strip │ └── main.go ├── go.mod ├── options.go ├── strip.go └── strip_test.go /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | *.swp 3 | cmd/strip/strip 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Write.as 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # go-strip-markdown 2 | 3 | [![GoDoc](https://godoc.org/github.com/writeas/go-strip-markdown?status.svg)](https://godoc.org/github.com/writeas/go-strip-markdown) 4 | 5 | A Markdown stripper written in Go (golang). 6 | 7 | ## Usage 8 | You could create a simple command-line utility: 9 | 10 | ```go 11 | package main 12 | 13 | import ( 14 | "fmt" 15 | "github.com/writeas/go-strip-markdown" 16 | "os" 17 | ) 18 | 19 | func main() { 20 | if len(os.Args) < 2 { 21 | os.Exit(1) 22 | } 23 | fmt.Println(stripmd.Strip(os.Args[1])) 24 | } 25 | ``` 26 | 27 | You could pass it Markdown and get pure, beauteous text in return: 28 | 29 | ```bash 30 | ./strip "# A Tale of Text Formatting 31 | 32 | _One fateful day_ a developer was presented with [Markdown](https://daringfireball.net/projects/markdown/). 33 | And they wanted **none of it**." 34 | 35 | # A Tale of Text Formatting 36 | # 37 | # One fateful day a developer was presented with Markdown. 38 | # And they wanted none of it. 39 | ``` 40 | 41 | ## Inspiration 42 | This was largely based off of [remove-markdown](https://github.com/stiang/remove-markdown), a Markdown stripper written in Javascript. 43 | 44 | ## Used by 45 | 46 | This library is used in these projects: 47 | 48 | * [WriteFreely](https://github.com/writeas/writefreely) 49 | 50 | ## License 51 | MIT. 52 | -------------------------------------------------------------------------------- /cmd/strip/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "github.com/writeas/go-strip-markdown" 6 | "os" 7 | ) 8 | 9 | func main() { 10 | if len(os.Args) < 2 { 11 | os.Exit(1) 12 | } 13 | fmt.Print(stripmd.Strip(os.Args[1])) 14 | } 15 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/writeas/go-strip-markdown/v2 2 | 3 | go 1.13 4 | -------------------------------------------------------------------------------- /options.go: -------------------------------------------------------------------------------- 1 | package stripmd 2 | 3 | type Options struct { 4 | SkipImages bool 5 | } 6 | -------------------------------------------------------------------------------- /strip.go: -------------------------------------------------------------------------------- 1 | // Package stripmd strips Markdown from text 2 | package stripmd 3 | 4 | import ( 5 | "regexp" 6 | ) 7 | 8 | var ( 9 | listLeadersReg = regexp.MustCompile(`(?m)^([\s\t]*)([\*\-\+]|\d\.)\s+`) 10 | 11 | headerReg = regexp.MustCompile(`\n={2,}`) 12 | strikeReg = regexp.MustCompile(`~~`) 13 | codeReg = regexp.MustCompile("`{3}" + `.*\n`) 14 | 15 | htmlReg = regexp.MustCompile("<(.*?)>") 16 | emphReg = regexp.MustCompile(`\*\*([^*]+)\*\*`) 17 | emphReg2 = regexp.MustCompile(`\*([^*]+)\*`) 18 | emphReg3 = regexp.MustCompile(`__([^_]+)__`) 19 | emphReg4 = regexp.MustCompile(`_([^_]+)_`) 20 | setextHeaderReg = regexp.MustCompile(`^[=\-]{2,}\s*$`) 21 | footnotesReg = regexp.MustCompile(`\[\^.+?\](\: .*?$)?`) 22 | footnotes2Reg = regexp.MustCompile(`\s{0,2}\[.*?\]: .*?$`) 23 | imagesReg = regexp.MustCompile(`\!\[(.*?)\]\s?[\[\(].*?[\]\)]`) 24 | linksReg = regexp.MustCompile(`\[(.*?)\][\[\(].*?[\]\)]`) 25 | blockquoteReg = regexp.MustCompile(`>\s*`) 26 | refLinkReg = regexp.MustCompile(`^\s{1,2}\[(.*?)\]: (\S+)( ".*?")?\s*$`) 27 | atxHeaderReg = regexp.MustCompile(`(?m)^\#{1,6}\s*([^#]+)\s*(\#{1,6})?$`) 28 | atxHeaderReg2 = regexp.MustCompile(`([\*_]{1,3})(\S.*?\S)?P1`) 29 | atxHeaderReg3 = regexp.MustCompile("(?m)(`{3,})" + `(.*?)?P1`) 30 | atxHeaderReg4 = regexp.MustCompile(`^-{3,}\s*$`) 31 | atxHeaderReg5 = regexp.MustCompile("`(.+?)`") 32 | atxHeaderReg6 = regexp.MustCompile(`\n{2,}`) 33 | ) 34 | 35 | // Strip returns the given string sans any Markdown. 36 | // Where necessary, elements are replaced with their best textual forms, so 37 | // for example, hyperlinks are stripped of their URL and become only the link 38 | // text, and images lose their URL and become only the alt text. 39 | func Strip(s string) string { 40 | return StripOptions(s, Options{}) 41 | } 42 | 43 | func StripOptions(s string, opts Options) string { 44 | res := s 45 | res = listLeadersReg.ReplaceAllString(res, "$1") 46 | 47 | res = headerReg.ReplaceAllString(res, "\n") 48 | res = strikeReg.ReplaceAllString(res, "") 49 | res = codeReg.ReplaceAllString(res, "") 50 | 51 | res = emphReg.ReplaceAllString(res, "$1") 52 | res = emphReg2.ReplaceAllString(res, "$1") 53 | res = emphReg3.ReplaceAllString(res, "$1") 54 | res = emphReg4.ReplaceAllString(res, "$1") 55 | res = htmlReg.ReplaceAllString(res, "$1") 56 | res = setextHeaderReg.ReplaceAllString(res, "") 57 | res = footnotesReg.ReplaceAllString(res, "") 58 | res = footnotes2Reg.ReplaceAllString(res, "") 59 | if opts.SkipImages { 60 | res = imagesReg.ReplaceAllString(res, "") 61 | } else { 62 | res = imagesReg.ReplaceAllString(res, "$1") 63 | } 64 | res = linksReg.ReplaceAllString(res, "$1") 65 | res = blockquoteReg.ReplaceAllString(res, " ") 66 | res = refLinkReg.ReplaceAllString(res, "") 67 | res = atxHeaderReg.ReplaceAllString(res, "$1") 68 | res = atxHeaderReg2.ReplaceAllString(res, "$2") 69 | res = atxHeaderReg3.ReplaceAllString(res, "$2") 70 | res = atxHeaderReg4.ReplaceAllString(res, "") 71 | res = atxHeaderReg5.ReplaceAllString(res, "$1") 72 | res = atxHeaderReg6.ReplaceAllString(res, "\n\n") 73 | return res 74 | } 75 | -------------------------------------------------------------------------------- /strip_test.go: -------------------------------------------------------------------------------- 1 | package stripmd 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | ) 7 | 8 | func TestStripMarkdown(t *testing.T) { 9 | // Same tests as github.com/stiang/remove-markdown 10 | in := `## This is a heading ## 11 | 12 | This is an _emphasized paragraph_ with [a link](http://www.disney.com/). Here's an _[emphasized link](https://write.as)_. 13 | 14 | ### This is another heading 15 | 16 | In ` + "`Getting Started` we **set up** `something`" + ` __foo__. 17 | 18 | * Some list 19 | * With items 20 | * Even indented` 21 | 22 | out := `This is a heading 23 | 24 | This is an emphasized paragraph with a link. Here's an emphasized link. 25 | 26 | This is another heading 27 | 28 | In Getting Started we set up something foo. 29 | 30 | Some list 31 | With items 32 | Even indented` 33 | 34 | if res := Strip(in); res != out { 35 | t.Errorf("Original:\n\n%s\n\nGot:\n\n%s", in, res) 36 | } 37 | 38 | // More extensive tests 39 | in = `# Markdown is simple 40 | It lets you _italicize words_ and **bold them**, too. You can ~~cross things out~~ and add __emphasis__ to your writing. 41 | 42 | But you can also link to stuff, like [Write.as](https://write.as)! 43 | 44 | ## Organize text with headers 45 | Create sections in your text just like this. 46 | 47 | ### Use lists 48 | You might already write lists like this: 49 | 50 | * Get groceries 51 | * Go for a walk 52 | 53 | And sometimes you need to do things in a certain order: 54 | 55 | 1. Put on clothes 56 | 2. Put on shoes 57 | 3. Go for a walk 58 | 59 | ### Highlight text 60 | You can quote interesting people: 61 | 62 | > Live long and prosper. 63 | 64 | You can even share ` + "`code stuff`." 65 | 66 | out = `Markdown is simple 67 | It lets you italicize words and bold them, too. You can cross things out and add emphasis to your writing. 68 | 69 | But you can also link to stuff, like Write.as! 70 | 71 | Organize text with headers 72 | Create sections in your text just like this. 73 | 74 | Use lists 75 | You might already write lists like this: 76 | 77 | Get groceries 78 | Go for a walk 79 | 80 | And sometimes you need to do things in a certain order: 81 | 82 | Put on clothes 83 | Put on shoes 84 | Go for a walk 85 | 86 | Highlight text 87 | You can quote interesting people: 88 | 89 | Live long and prosper. 90 | 91 | You can even share code stuff.` 92 | 93 | if res := Strip(in); res != out { 94 | t.Errorf("Original:\n\n%s\n\nGot:\n\n%s", in, res) 95 | } 96 | 97 | in = "![] (https://write.as/favicon.ico)" 98 | out = "" 99 | if res := Strip(in); res != out { 100 | t.Errorf("Original:\n\n%s\n\nGot:\n\n%s", in, res) 101 | } 102 | 103 | in = "![Some image] (https://write.as/favicon.ico)" 104 | out = "Some image" 105 | if res := Strip(in); res != out { 106 | t.Errorf("Original:\n\n%s\n\nGot:\n\n%s", in, res) 107 | } 108 | 109 | in = "![Some image](https://write.as/favicon.ico)" 110 | out = "Some image" 111 | if res := Strip(in); res != out { 112 | t.Errorf("Original:\n\n%s\n\nGot:\n\n%s", in, res) 113 | } 114 | } 115 | 116 | func ExampleStrip() { 117 | fmt.Println(Strip(`# Hello, world! 118 | 119 | This is [a Go library](https://github.com/writeas/go-strip-markdown) for stripping **Markdown** from _any_ text.`)) 120 | 121 | // Output: 122 | // Hello, world! 123 | // 124 | // This is a Go library for stripping Markdown from any text. 125 | } 126 | --------------------------------------------------------------------------------