diff options
author | wxiaoguang <wxiaoguang@gmail.com> | 2024-04-03 01:48:27 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-04-02 17:48:27 +0000 |
commit | ca5c895efb91d2c2f17a83460e1753101c6f6bb1 (patch) | |
tree | 351c56aa353b6147e335dbdb3892e513690ea0e8 /modules/indexer | |
parent | eb505b128c7b9b2459f2a5d20b5740017125178b (diff) | |
download | gitea-ca5c895efb91d2c2f17a83460e1753101c6f6bb1.tar.gz gitea-ca5c895efb91d2c2f17a83460e1753101c6f6bb1.zip |
Render embedded code preview by permlink in markdown (#30234)
The permlink in markdown will be rendered as a code preview block, like GitHub
Co-authored-by: silverwind <me@silverwind.io>
Diffstat (limited to 'modules/indexer')
-rw-r--r-- | modules/indexer/code/search.go | 16 |
1 files changed, 9 insertions, 7 deletions
diff --git a/modules/indexer/code/search.go b/modules/indexer/code/search.go index 5f35e8073b..74c957dde6 100644 --- a/modules/indexer/code/search.go +++ b/modules/indexer/code/search.go @@ -22,7 +22,7 @@ type Result struct { UpdatedUnix timeutil.TimeStamp Language string Color string - Lines []ResultLine + Lines []*ResultLine } type ResultLine struct { @@ -70,16 +70,18 @@ func writeStrings(buf *bytes.Buffer, strs ...string) error { return nil } -func HighlightSearchResultCode(filename string, lineNums []int, code string) []ResultLine { +func HighlightSearchResultCode(filename, language string, lineNums []int, code string) []*ResultLine { // we should highlight the whole code block first, otherwise it doesn't work well with multiple line highlighting - hl, _ := highlight.Code(filename, "", code) + hl, _ := highlight.Code(filename, language, code) highlightedLines := strings.Split(string(hl), "\n") // The lineNums outputted by highlight.Code might not match the original lineNums, because "highlight" removes the last `\n` - lines := make([]ResultLine, min(len(highlightedLines), len(lineNums))) + lines := make([]*ResultLine, min(len(highlightedLines), len(lineNums))) for i := 0; i < len(lines); i++ { - lines[i].Num = lineNums[i] - lines[i].FormattedContent = template.HTML(highlightedLines[i]) + lines[i] = &ResultLine{ + Num: lineNums[i], + FormattedContent: template.HTML(highlightedLines[i]), + } } return lines } @@ -122,7 +124,7 @@ func searchResult(result *internal.SearchResult, startIndex, endIndex int) (*Res UpdatedUnix: result.UpdatedUnix, Language: result.Language, Color: result.Color, - Lines: HighlightSearchResultCode(result.Filename, lineNums, formattedLinesBuffer.String()), + Lines: HighlightSearchResultCode(result.Filename, result.Language, lineNums, formattedLinesBuffer.String()), }, nil } |