Skip to content

Commit 488d1e6

Browse files
committed
Add script tag only if used
1 parent f14c8a5 commit 488d1e6

2 files changed

Lines changed: 6 additions & 1 deletion

File tree

exe/rubydown

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,18 @@ end
1414

1515
class EvalDoc < Kramdown::Document
1616
attr_accessor :context, :stdout
17+
attr_reader :javascripts
1718

1819
def initialize(text, *opts)
1920
super(text, *opts)
2021

22+
@javascripts = {}
2123
self.context = get_context
2224
self.root = scan_el(self.root)
2325
end
2426

27+
REQUIREJS_SCRIPT_TAG = '<script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.min.js"></script>'
28+
2529
private
2630
def scan_el(el)
2731
new_children = []
@@ -34,6 +38,7 @@ class EvalDoc < Kramdown::Document
3438
if is_code && child_el.options[:lang] == 'ruby'
3539
code = child_el.value.gsub(/^ *ruby *\n/, '')
3640
new_children << eval_and_elementize(code)
41+
@javascripts[:requirejs] ||= REQUIREJS_SCRIPT_TAG
3742
end
3843
end
3944

templates/template.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
display: none;
1717
}
1818
</style>
19-
<script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.min.js"></script>
19+
<%= doc.javascripts.values.join('') %>
2020
</head>
2121
<body>
2222
<article class='markdown-body'>

0 commit comments

Comments
 (0)