mirror of
				https://github.com/superseriousbusiness/gotosocial.git
				synced 2025-10-31 14:32:24 -05:00 
			
		
		
		
	[bugfix] Markdown formatting updates (#743)
* add minify dependency specifically for markdown * rearrange markdown formatting * update markdown tests
This commit is contained in:
		
					parent
					
						
							
								f2a6ae3ef8
							
						
					
				
			
			
				commit
				
					
						879b4abde7
					
				
			
		
					 42 changed files with 7259 additions and 19 deletions
				
			
		|  | @ -23,17 +23,39 @@ import ( | |||
| 
 | ||||
| 	"github.com/russross/blackfriday/v2" | ||||
| 	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel" | ||||
| 	"github.com/superseriousbusiness/gotosocial/internal/log" | ||||
| 	"github.com/tdewolff/minify/v2" | ||||
| 	"github.com/tdewolff/minify/v2/html" | ||||
| ) | ||||
| 
 | ||||
| func (f *formatter) FromMarkdown(ctx context.Context, md string, mentions []*gtsmodel.Mention, tags []*gtsmodel.Tag) string { | ||||
| 	// do the markdown parsing *first* | ||||
| 	contentBytes := blackfriday.Run([]byte(md)) | ||||
| var m *minify.M | ||||
| 
 | ||||
| func (f *formatter) FromMarkdown(ctx context.Context, md string, mentions []*gtsmodel.Mention, tags []*gtsmodel.Tag) string { | ||||
| 	// format tags nicely | ||||
| 	content := f.ReplaceTags(ctx, string(contentBytes), tags) | ||||
| 	content := f.ReplaceTags(ctx, md, tags) | ||||
| 
 | ||||
| 	// format mentions nicely | ||||
| 	content = f.ReplaceMentions(ctx, content, mentions) | ||||
| 
 | ||||
| 	return SanitizeHTML(content) | ||||
| 	// parse markdown | ||||
| 	contentBytes := blackfriday.Run([]byte(content)) | ||||
| 
 | ||||
| 	// clean anything dangerous out of it | ||||
| 	content = SanitizeHTML(string(contentBytes)) | ||||
| 
 | ||||
| 	if m == nil { | ||||
| 		m = minify.New() | ||||
| 		m.Add("text/html", &html.Minifier{ | ||||
| 			KeepEndTags:    true, | ||||
| 			KeepQuotes:     true, | ||||
| 			KeepWhitespace: true, | ||||
| 		}) | ||||
| 	} | ||||
| 
 | ||||
| 	minified, err := m.String("text/html", content) | ||||
| 	if err != nil { | ||||
| 		log.Errorf("error minifying markdown text: %s", err) | ||||
| 	} | ||||
| 
 | ||||
| 	return minified | ||||
| } | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue