jdvp-codetabs-commonmark 1.0.1 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/assets/codeblock.js +23 -1
- data/lib/jdvp-codetabs-commonmark.rb +13 -6
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1831135ee1c5f3b8ef79afe62136ff4a68070bfcd3f551e597732c67daa426c4
|
4
|
+
data.tar.gz: a357ed5a71666e8a4bc51afa52b64693651f15f5da45249e3177443dfabf1490
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: da8f6c7ee8baceab2730a59eee35b1d1569661f6e98e1a404c817df3ae26f9153645ca2cf8bfbb7e0485db1b1cc55adaef8ee452776e004ae168bc0a677cfec0
|
7
|
+
data.tar.gz: 313a67b159981c167b4242e6a3238341190f2b52e14080064111ebabecb3cc48e4b4510ac979e58e987272b8ee5ec2b70265a453c6a8d9cfde6d08c046848532
|
data/assets/codeblock.js
CHANGED
@@ -25,8 +25,30 @@ function selectTab(codeLangClass, tabClass, tabIndex) {
|
|
25
25
|
});
|
26
26
|
};
|
27
27
|
|
28
|
-
function
|
28
|
+
function isNumeric(str) {
|
29
|
+
return !isNaN(str) && !isNaN(parseFloat(str))
|
30
|
+
}
|
31
|
+
|
32
|
+
function getRange(text) {
|
33
|
+
if (!text?.length) {
|
34
|
+
return [];
|
35
|
+
}
|
36
|
+
var elements = text.split("-").map(i => parseInt(i));
|
37
|
+
var a = Math.min(...elements);
|
38
|
+
var b = Math.max(...elements);
|
39
|
+
return Array.from(new Array(Math.abs(a - b) + 1), (x, i) => i + Math.min(a, b));
|
40
|
+
};
|
41
|
+
|
42
|
+
function copyText(codeBlockClass, copyRange) {
|
43
|
+
var lines = [...new Set(copyRange.replace(/\s/g, "").split(",").flatMap(item => getRange(item)))];
|
44
|
+
lines.sort();
|
45
|
+
|
29
46
|
var copiedText = document.getElementsByClassName(codeBlockClass)[0].innerText;
|
47
|
+
if (lines !== undefined && lines.length > 0) {
|
48
|
+
copiedText = lines.map(lineNumber => copiedText.split("\n")[lineNumber])
|
49
|
+
.filter(item => item?.length)
|
50
|
+
.join("\n")
|
51
|
+
}
|
30
52
|
navigator.clipboard.writeText(copiedText);
|
31
53
|
var snackbar = document.getElementById("code_copied_snackbar");
|
32
54
|
snackbar.classList.add("show")
|
@@ -19,6 +19,13 @@ class CodeTabsCustomerRenderer < JekyllCommonMarkCustomRenderer
|
|
19
19
|
#Get a unique ID per code block in order to allow code copying
|
20
20
|
individual_code_block_id = SecureRandom.uuid
|
21
21
|
|
22
|
+
if (is_copy_action_enabled(node))
|
23
|
+
if (!@added_copy_snackbar)
|
24
|
+
out("<div id=\"code_copied_snackbar\">Copied!</div>")
|
25
|
+
@added_copy_snackbar = true
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
22
29
|
#Create a header if necessary and then creates the wrapper for each item
|
23
30
|
#This allows tabs to be selected individaully
|
24
31
|
if (is_header_item)
|
@@ -34,11 +41,7 @@ class CodeTabsCustomerRenderer < JekyllCommonMarkCustomRenderer
|
|
34
41
|
#Changing theme button is added to all code blocks, but the copy button is configurable.
|
35
42
|
out("<div class=\"code_switcher_code_action_container\">")
|
36
43
|
if (is_copy_action_enabled(node))
|
37
|
-
|
38
|
-
out("<div id=\"code_copied_snackbar\">Copied!</div>")
|
39
|
-
@added_copy_snackbar = true
|
40
|
-
end
|
41
|
-
out("<button class=\"code_switcher_copy_button\" title=\"Copy\" onclick=\"copyText(\'#{individual_code_block_id}\')\"></button>")
|
44
|
+
out("<button class=\"code_switcher_copy_button\" title=\"Copy\" onclick=\"copyText(\'#{individual_code_block_id}\', \'#{get_code_copy_Lines(node)}\')\"></button>")
|
42
45
|
end
|
43
46
|
out("<button class=\"code_switcher_theme_button\" onclick=\"updateTheme(true)\"></button>")
|
44
47
|
out("</div>")
|
@@ -62,7 +65,7 @@ class CodeTabsCustomerRenderer < JekyllCommonMarkCustomRenderer
|
|
62
65
|
#Splits the code fence into the language and extra info
|
63
66
|
#Removes the codeCopyEnabled item which is just a flag used to enable showing a copy action button
|
64
67
|
def split_lanugage_fence_info(node)
|
65
|
-
node&.fence_info&.sub(
|
68
|
+
node&.fence_info&.sub(/ codeCopyEnabled=?"?([\ \-\,0-9]*)"?/, "")&.split(/[\s,]/, 2)
|
66
69
|
end
|
67
70
|
|
68
71
|
#Gets the language used in the code fence (the part typically immediately after a triple backtick in markdown)
|
@@ -87,6 +90,10 @@ class CodeTabsCustomerRenderer < JekyllCommonMarkCustomRenderer
|
|
87
90
|
node&.fence_info&.include?("codeCopyEnabled") || false
|
88
91
|
end
|
89
92
|
|
93
|
+
def get_code_copy_Lines(node)
|
94
|
+
node&.fence_info[/ codeCopyEnabled=?"?([\ \-\,0-9]*)"?/, 1] || ""
|
95
|
+
end
|
96
|
+
|
90
97
|
#Creates the tab header portion of the code switcher
|
91
98
|
def create_tabbed_code_header(node)
|
92
99
|
uuid = SecureRandom.uuid
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jdvp-codetabs-commonmark
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- JD Porterfield
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-02-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: securerandom
|