File tree Expand file tree Collapse file tree 5 files changed +45
-3
lines changed Expand file tree Collapse file tree 5 files changed +45
-3
lines changed Original file line number Diff line number Diff line change 9
9
10
10
require "qiita/markdown/embed/code_pen"
11
11
require "qiita/markdown/embed/tweet"
12
+ require "qiita/markdown/embed/asciinema"
12
13
require "qiita/markdown/transformers/filter_attributes"
13
14
require "qiita/markdown/transformers/filter_script"
14
15
require "qiita/markdown/transformers/strip_invalid_node"
Original file line number Diff line number Diff line change
1
+ module Qiita
2
+ module Markdown
3
+ module Embed
4
+ module Asciinema
5
+ SCRIPT_HOST = "asciinema.org" . freeze
6
+ end
7
+ end
8
+ end
9
+ end
Original file line number Diff line number Diff line change @@ -26,7 +26,7 @@ class UserInputSanitizer < HTML::Pipeline::Filter
26
26
"li" => %w[ id ] ,
27
27
"p" => Embed ::CodePen ::ATTRIBUTES ,
28
28
"q" => %w[ cite ] ,
29
- "script" => %w[ async src ] ,
29
+ "script" => %w[ async src id ] ,
30
30
"sup" => %w[ id ] ,
31
31
"td" => %w[ colspan rowspan style ] ,
32
32
"th" => %w[ colspan rowspan style ] ,
Original file line number Diff line number Diff line change @@ -2,11 +2,15 @@ module Qiita
2
2
module Markdown
3
3
module Transformers
4
4
class FilterScript
5
- WHITE_LIST = [
5
+ URL_WHITE_LIST = [
6
6
Embed ::CodePen ::SCRIPT_URLS ,
7
7
Embed ::Tweet ::SCRIPT_URL ,
8
8
] . flatten . freeze
9
9
10
+ HOST_WHITE_LIST = [
11
+ Embed ::Asciinema ::SCRIPT_HOST ,
12
+ ] . flatten . freeze
13
+
10
14
def self . call ( *args )
11
15
new ( *args ) . transform
12
16
end
@@ -17,7 +21,7 @@ def initialize(env)
17
21
18
22
def transform
19
23
if name == "script"
20
- if WHITE_LIST . include? ( node [ "src" ] )
24
+ if URL_WHITE_LIST . include? ( node [ "src" ] ) || HOST_WHITE_LIST . include? ( host_of ( node [ "src" ] ) )
21
25
node [ "async" ] = "async" unless node . attributes . key? ( "async" )
22
26
node . children . unlink
23
27
else
@@ -35,6 +39,12 @@ def name
35
39
def node
36
40
@env [ :node ]
37
41
end
42
+
43
+ def host_of ( url )
44
+ Addressable ::URI . parse ( url ) . host if url
45
+ rescue Addressable ::URI ::InvalidURIError
46
+ nil
47
+ end
38
48
end
39
49
end
40
50
end
Original file line number Diff line number Diff line change 1385
1385
end
1386
1386
end
1387
1387
1388
+ context "with HTML embed code for Asciinema" do
1389
+ let ( :markdown ) do
1390
+ <<-MARKDOWN . strip_heredoc
1391
+ <script id="example" src="https://asciinema.org/a/example.js"></script>
1392
+ MARKDOWN
1393
+ end
1394
+
1395
+ if allowed
1396
+ it "does not sanitize embed code" do
1397
+ should eq <<-HTML . strip_heredoc
1398
+ < script id ="example " src ="https://asciinema.org/a/example.js "> </ script >
1399
+ HTML
1400
+ end
1401
+ else
1402
+ it "forces async attribute on script" do
1403
+ should eq <<-HTML . strip_heredoc
1404
+ < script id ="example " src ="https://asciinema.org/a/example.js " async ="async "> </ script >
1405
+ HTML
1406
+ end
1407
+ end
1408
+ end
1409
+
1388
1410
context "with embed code for Tweet" do
1389
1411
let ( :markdown ) do
1390
1412
<<-MARKDOWN . strip_heredoc
You can’t perform that action at this time.
0 commit comments