forked from forem/forem
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add codesandbox support (forem#1052)
- Loading branch information
1 parent
5043e3f
commit ad25882
Showing
2 changed files
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
class CodesandboxTag < LiquidTagBase | ||
def initialize(tag_name, id, tokens) | ||
super | ||
@id = parse_id(id) | ||
end | ||
|
||
def render(_context) | ||
'<iframe src="https://codesandbox.io/embed/' + @id + '" | ||
style="width:100%; height:calc(300px + 8vw); border:0; border-radius: 4px; overflow:hidden;" | ||
sandbox="allow-same-origin allow-scripts allow-forms allow-top-navigation-by-user-activation" | ||
</iframe>' | ||
end | ||
|
||
private | ||
|
||
def parse_id(input) | ||
input_no_space = input.delete(" ") | ||
if valid_id?(input_no_space) | ||
input_no_space | ||
else | ||
raise StandardError, "Invalid repl.it Id" | ||
end | ||
end | ||
|
||
def valid_id?(id) | ||
id =~ /\A[a-zA-Z0-9\-]{0,60}\Z/ | ||
end | ||
end | ||
|
||
Liquid::Template.register_tag("codesandbox", CodesandboxTag) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters