-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Support asciicast files as new markup #22448
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 15 commits
b795c6c
9f81e08
c7b31a1
a76bfed
9530aae
1c613dc
ca060fc
a35e1b5
7aa33b5
3599acc
662aad5
87f8502
b27835a
aacb4ca
4ecd738
6a7b580
22399ec
4869640
f849825
b0a17f6
cbb0fb5
c63a167
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Copyright 2023 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package asciicast | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"net/url" | ||
"regexp" | ||
|
||
"code.gitea.io/gitea/modules/markup" | ||
"code.gitea.io/gitea/modules/setting" | ||
) | ||
|
||
func init() { | ||
markup.RegisterRenderer(Renderer{}) | ||
} | ||
|
||
// Renderer implements markup.Renderer for asciicast files. | ||
// See https://github.com/asciinema/asciinema/blob/develop/doc/asciicast-v2.md | ||
type Renderer struct{} | ||
|
||
// Name implements markup.Renderer | ||
func (Renderer) Name() string { | ||
return "asciicast" | ||
} | ||
|
||
// Extensions implements markup.Renderer | ||
func (Renderer) Extensions() []string { | ||
return []string{".cast"} | ||
} | ||
|
||
const ( | ||
playerClassName = "asciinema-player-container" | ||
playerSrcAttr = "data-asciinema-player-src" | ||
) | ||
|
||
// SanitizerRules implements markup.Renderer | ||
func (Renderer) SanitizerRules() []setting.MarkupSanitizerRule { | ||
return []setting.MarkupSanitizerRule{ | ||
{Element: "div", AllowAttr: "class", Regexp: regexp.MustCompile(playerClassName)}, | ||
{Element: "div", AllowAttr: playerSrcAttr}, | ||
} | ||
} | ||
|
||
// Render implements markup.Renderer | ||
func (Renderer) Render(ctx *markup.RenderContext, _ io.Reader, output io.Writer) error { | ||
rawURL := fmt.Sprintf("%s/%s/%s/raw/%s/%s", | ||
setting.AppSubURL, | ||
url.PathEscape(ctx.Metas["user"]), | ||
url.PathEscape(ctx.Metas["repo"]), | ||
ctx.Metas["BranchNameSubURL"], | ||
url.PathEscape(ctx.RelativePath), | ||
) | ||
|
||
_, err := io.WriteString(output, fmt.Sprintf( | ||
`<div class="%s" %s="%s"></div>`, | ||
playerClassName, | ||
playerSrcAttr, | ||
rawURL, | ||
)) | ||
return err | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export async function renderAsciinemaPlayer() { | ||
const els = document.querySelectorAll('.asciinema-player-container'); | ||
if (!els.length) return; | ||
|
||
const player = await import(/* webpackChunkName: "asciinema" */'asciinema-player'); | ||
|
||
for (const el of els) { | ||
player.create(el.getAttribute('data-asciinema-player-src'), el, { | ||
// poster (a preview frame) to display until the playback is started. | ||
// Set it to 1 hour (also means the end if the video is shorter) to make the preview frame show more. | ||
poster: 'npt:1:0:0', | ||
wolfogre marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -470,6 +470,10 @@ | |
pre { | ||
overflow: auto; | ||
} | ||
|
||
.asciicast { | ||
padding: 5px !important; | ||
} | ||
} | ||
|
||
.sidebar { | ||
|
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,10 @@ | ||||||||||
@import "../asciinema-player/dist/bundle/asciinema-player.css"; | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The JS is loaded async now, the CSS should also be loaded async .... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you please show me how to do that? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can add it to the JS as a second import, and then do gitea/web_src/js/markup/math.js Lines 19 to 22 in 7ddc11d
|
||||||||||
|
||||||||||
.asciinema-player-container { | ||||||||||
width: 100%; | ||||||||||
height: auto; | ||||||||||
} | ||||||||||
|
||||||||||
.asciinema-terminal { | ||||||||||
overflow: hidden !important; | ||||||||||
} |
Uh oh!
There was an error while loading. Please reload this page.