loading up the forgejo repo on tangled to test page performance
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

fix: inline file preview for rendered files (#6572)

This fixes the inline file preview for rendered files (e.g., markdown). [Here, a live issue in v11](https://v11.next.forgejo.org/mahlzahn/test-inline-file-preview/issues/1) and [the same in v7 (with even more bugs)](https://v7.next.forgejo.org/mahlzahn/test-inline-file-preview/issues/1).

It fixes
1. the inline preview for possibly rendered files, when the link is specified with `?display=source`. This happens, e.g., if you are watching a (e.g., markdown) file in source and then want to link some of its lines.
2. the link to the source file inside the inline preview for possible rendered files (currently it links to the rendered version and then the `#L…` cannot point to the correct lines). This is done by always adding `?display=source` to the link.

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/6572
Reviewed-by: Gusted <gusted@noreply.codeberg.org>
Co-authored-by: Robert Wolff <mahlzahn@posteo.de>
Co-committed-by: Robert Wolff <mahlzahn@posteo.de>

authored by

Robert Wolff
Robert Wolff
and committed by
Gusted
95f23161 f45a9836

+112 -2
+7 -1
modules/markup/file_preview.go
··· 77 77 78 78 commitSha := node.Data[m[4]:m[5]] 79 79 filePath := node.Data[m[6]:m[7]] 80 + urlFullSource := urlFull 81 + if strings.HasSuffix(filePath, "?display=source") { 82 + filePath = strings.TrimSuffix(filePath, "?display=source") 83 + } else if Type(filePath) != "" { 84 + urlFullSource = node.Data[m[0]:m[6]] + filePath + "?display=source#" + node.Data[m[8]:m[1]] 85 + } 80 86 hash := node.Data[m[8]:m[9]] 81 87 82 88 preview.start = m[0] ··· 113 119 titleBuffer.WriteString(" &ndash; ") 114 120 } 115 121 116 - err = html.Render(titleBuffer, createLink(urlFull, filePath, "muted")) 122 + err = html.Render(titleBuffer, createLink(urlFullSource, filePath, "muted")) 117 123 if err != nil { 118 124 log.Error("failed to render filepathLink: %v", err) 119 125 }
+103
modules/markup/html_test.go
··· 1026 1026 localMetas, 1027 1027 ) 1028 1028 }) 1029 + 1030 + commitFileURL := util.URLJoin(markup.TestRepoURL, "src", "commit", "c9913120ed2c1e27c1d7752ecdb7a504dc7cf6be", "path", "to", "file.md") 1031 + 1032 + t.Run("rendered file with ?display=source", func(t *testing.T) { 1033 + testRender( 1034 + commitFileURL+"?display=source"+"#L1-L2", 1035 + `<p></p>`+ 1036 + `<div class="file-preview-box">`+ 1037 + `<div class="header">`+ 1038 + `<div>`+ 1039 + `<a href="http://localhost:3000/gogits/gogs/src/commit/c9913120ed2c1e27c1d7752ecdb7a504dc7cf6be/path/to/file.md?display=source#L1-L2" class="muted" rel="nofollow">path/to/file.md</a>`+ 1040 + `</div>`+ 1041 + `<span class="text small grey">`+ 1042 + `Lines 1 to 2 in <a href="http://localhost:3000/gogits/gogs/src/commit/c9913120ed2c1e27c1d7752ecdb7a504dc7cf6be" class="text black" rel="nofollow">c991312</a>`+ 1043 + `</span>`+ 1044 + `</div>`+ 1045 + `<div class="ui table">`+ 1046 + `<table class="file-preview">`+ 1047 + `<tbody>`+ 1048 + `<tr>`+ 1049 + `<td class="lines-num"><span data-line-number="1"></span></td>`+ 1050 + `<td class="lines-code chroma"><code class="code-inner"><span class="gh"># A`+"\n"+`</span></code></td>`+ 1051 + `</tr>`+ 1052 + `<tr>`+ 1053 + `<td class="lines-num"><span data-line-number="2"></span></td>`+ 1054 + `<td class="lines-code chroma"><code class="code-inner"><span class="gh"></span>B`+"\n"+`</code></td>`+ 1055 + `</tr>`+ 1056 + `</tbody>`+ 1057 + `</table>`+ 1058 + `</div>`+ 1059 + `</div>`+ 1060 + `<p></p>`, 1061 + localMetas, 1062 + ) 1063 + }) 1064 + 1065 + t.Run("rendered file without ?display=source", func(t *testing.T) { 1066 + testRender( 1067 + commitFileURL+"#L1-L2", 1068 + `<p></p>`+ 1069 + `<div class="file-preview-box">`+ 1070 + `<div class="header">`+ 1071 + `<div>`+ 1072 + `<a href="http://localhost:3000/gogits/gogs/src/commit/c9913120ed2c1e27c1d7752ecdb7a504dc7cf6be/path/to/file.md?display=source#L1-L2" class="muted" rel="nofollow">path/to/file.md</a>`+ 1073 + `</div>`+ 1074 + `<span class="text small grey">`+ 1075 + `Lines 1 to 2 in <a href="http://localhost:3000/gogits/gogs/src/commit/c9913120ed2c1e27c1d7752ecdb7a504dc7cf6be" class="text black" rel="nofollow">c991312</a>`+ 1076 + `</span>`+ 1077 + `</div>`+ 1078 + `<div class="ui table">`+ 1079 + `<table class="file-preview">`+ 1080 + `<tbody>`+ 1081 + `<tr>`+ 1082 + `<td class="lines-num"><span data-line-number="1"></span></td>`+ 1083 + `<td class="lines-code chroma"><code class="code-inner"><span class="gh"># A`+"\n"+`</span></code></td>`+ 1084 + `</tr>`+ 1085 + `<tr>`+ 1086 + `<td class="lines-num"><span data-line-number="2"></span></td>`+ 1087 + `<td class="lines-code chroma"><code class="code-inner"><span class="gh"></span>B`+"\n"+`</code></td>`+ 1088 + `</tr>`+ 1089 + `</tbody>`+ 1090 + `</table>`+ 1091 + `</div>`+ 1092 + `</div>`+ 1093 + `<p></p>`, 1094 + localMetas, 1095 + ) 1096 + }) 1097 + 1098 + commitFileURL = util.URLJoin(markup.TestRepoURL, "src", "commit", "190d9492934af498c3f669d6a2431dc5459e5b20", "path", "to", "file.go") 1099 + 1100 + t.Run("normal file with ?display=source", func(t *testing.T) { 1101 + testRender( 1102 + commitFileURL+"?display=source"+"#L2-L3", 1103 + `<p></p>`+ 1104 + `<div class="file-preview-box">`+ 1105 + `<div class="header">`+ 1106 + `<div>`+ 1107 + `<a href="http://localhost:3000/gogits/gogs/src/commit/190d9492934af498c3f669d6a2431dc5459e5b20/path/to/file.go?display=source#L2-L3" class="muted" rel="nofollow">path/to/file.go</a>`+ 1108 + `</div>`+ 1109 + `<span class="text small grey">`+ 1110 + `Lines 2 to 3 in <a href="http://localhost:3000/gogits/gogs/src/commit/190d9492934af498c3f669d6a2431dc5459e5b20" class="text black" rel="nofollow">190d949</a>`+ 1111 + `</span>`+ 1112 + `</div>`+ 1113 + `<div class="ui table">`+ 1114 + `<table class="file-preview">`+ 1115 + `<tbody>`+ 1116 + `<tr>`+ 1117 + `<td class="lines-num"><span data-line-number="2"></span></td>`+ 1118 + `<td class="lines-code chroma"><code class="code-inner"><span class="nx">B</span>`+"\n"+`</code></td>`+ 1119 + `</tr>`+ 1120 + `<tr>`+ 1121 + `<td class="lines-num"><span data-line-number="3"></span></td>`+ 1122 + `<td class="lines-code chroma"><code class="code-inner"><span class="nx">C</span>`+"\n"+`</code></td>`+ 1123 + `</tr>`+ 1124 + `</tbody>`+ 1125 + `</table>`+ 1126 + `</div>`+ 1127 + `</div>`+ 1128 + `<p></p>`, 1129 + localMetas, 1130 + ) 1131 + }) 1029 1132 }
modules/markup/tests/repo/repo1_filepreview/objects/0b/b53b56d70d253ce75c257d3cd6334a41ef2b6c

This is a binary file and will not be displayed.

modules/markup/tests/repo/repo1_filepreview/objects/35/75ed7948fe86ab56b0a76f796f7995222bec65

This is a binary file and will not be displayed.

modules/markup/tests/repo/repo1_filepreview/objects/3c/95f14e5a0ab2c5ba9ee9a47ddc261af4968043

This is a binary file and will not be displayed.

+1
modules/markup/tests/repo/repo1_filepreview/objects/72/1f0ce13d83f93d431b849a554a62948b85f573
··· 1 + x��K�1@]��$�J�a�z�JR�@w+��s��������۲�����"@VL&J3%f-�GD�q2>F�jBOEݹ�:�g�\1���ꦒk���EM6D�,Ÿ\��Ǹ�:\6��O�lmȩ�;ϭ|�!G��E��6Z��z�Y�β �m�wٛ���i�.x-o��"���L�
modules/markup/tests/repo/repo1_filepreview/objects/72/e0a44ea5761c9055995db18019e459576b3b27

This is a binary file and will not be displayed.

modules/markup/tests/repo/repo1_filepreview/objects/72/e1c77b65c7baa0e848557089148833fb54705e

This is a binary file and will not be displayed.

modules/markup/tests/repo/repo1_filepreview/objects/8b/ccd5176c25898b57da2551e076f769054e0d8e

This is a binary file and will not be displayed.

modules/markup/tests/repo/repo1_filepreview/objects/c9/8762531dd068cd818300a5f5c7dca5da79b510

This is a binary file and will not be displayed.

modules/markup/tests/repo/repo1_filepreview/objects/c9/913120ed2c1e27c1d7752ecdb7a504dc7cf6be

This is a binary file and will not be displayed.

+1 -1
modules/markup/tests/repo/repo1_filepreview/refs/heads/master
··· 1 - 4c1aaf56bcb9f39dcf65f3f250726850aed13cd6 1 + c9913120ed2c1e27c1d7752ecdb7a504dc7cf6be