spec-up-t 1.0.6 → 1.0.8

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.
@@ -0,0 +1,52 @@
1
+ /**
2
+ * Displays a modal with the given content.
3
+ *
4
+ * @param {string} content - The HTML content to display inside the modal.
5
+ */
6
+ function showModal(content) {
7
+ // Create the modal overlay
8
+ const overlay = document.createElement('div');
9
+ overlay.className = 'modal-overlay';
10
+
11
+ // Create the modal container
12
+ const modal = document.createElement('div');
13
+ modal.className = 'modal';
14
+
15
+ // Create the close button
16
+ const closeButton = document.createElement('button');
17
+ closeButton.className = 'modal-close';
18
+ closeButton.innerHTML = '×';
19
+ closeButton.onclick = closeModal;
20
+
21
+ // Add the content to the modal
22
+ modal.innerHTML = content;
23
+ modal.appendChild(closeButton);
24
+
25
+ // Add the modal to the overlay
26
+ overlay.appendChild(modal);
27
+
28
+ // Add the overlay to the document body
29
+ document.body.appendChild(overlay);
30
+
31
+ // Function to close the modal
32
+ function closeModal() {
33
+ document.body.removeChild(overlay);
34
+ }
35
+
36
+ // Close modal when clicked outside the modal
37
+ overlay.onclick = function (event) {
38
+ if (event.target === overlay) {
39
+ closeModal();
40
+ }
41
+ };
42
+
43
+ // Close modal with escape key
44
+ document.addEventListener('keydown', function (event) {
45
+ if (event.key === 'Escape') {
46
+ closeModal();
47
+ }
48
+ }, { once: true });
49
+ }
50
+
51
+ // // Example usage:
52
+ // showModal('<h2>This is a Modal</h2><p>You can put any content here.</p>');
@@ -13,6 +13,15 @@ var notyf = new Notyf({
13
13
  {
14
14
  type: 'success',
15
15
  background: '#1D6DAE',
16
- }
16
+ duration: 3000
17
+ },
18
+ {
19
+ type: 'error',
20
+ background: 'orange',
21
+ duration: 10000000,
22
+ dismissible: true
23
+ },
17
24
  ]
18
- });
25
+ });
26
+
27
+ // var notyf = new Notyf();
@@ -12,7 +12,8 @@
12
12
  "repo": "spec-up-xref-test-1",
13
13
  "site": "https://blockchainbird.github.io/spec-up-xref-test-1/",
14
14
  "commitHash": [
15
- "f66951f1d378490289caab9c51141b44a0438365"
15
+ "f66951f1d378490289caab9c51141b44a0438365",
16
+ "content": "[[def: AAL]]:\n\n~ See: [[ref: authenticator assurance level]].\n\n~ This is an addition, for testing purposes.\n"
16
17
  ]
17
18
  },
18
19
  {…}
@@ -25,15 +26,89 @@
25
26
  */
26
27
 
27
28
  function fetchCommitHashes() {
28
- /*****************/
29
- /* CONFIGURATION */
29
+ // Load GitHub API token from local storage if it exists
30
+ const savedToken = localStorage.getItem('githubToken');
31
+
32
+ // // Markdown parser
33
+ // const md = markdownit();
30
34
 
35
+ // A: Debounce function to delay execution, so the error message is not displayed too often, since we do not know of often and how many times the error will be triggered.
36
+ function debounce(func, wait) {
37
+ let timeout;
38
+ return function (...args) {
39
+ clearTimeout(timeout);
40
+ timeout = setTimeout(() => func.apply(this, args), wait);
41
+ };
42
+ }
31
43
 
32
- /* END CONFIGURATION */
33
- /*********************/
44
+ // B: Debounced “GitHub rate limit exceeded” error message
45
+ const debouncedError = debounce(() => {
46
+ notyf.error('GitHub rate limit exceeded. See <a target="_blank" rel="noopener" href="https://blockchainbird.github.io/spec-up-t-website/docs/github-token/">documentation</a> for more info.');
47
+ }, 3000); // Delay in milliseconds
48
+
49
+ // Fetch the content of a term-file from GitHub
50
+ function fetchGitHubContent(savedToken, match) {
51
+ // Create a headers object with the Authorization header if a GitHub API token is set
52
+ const headers = {};
53
+ if (savedToken && savedToken.length > 0) {
54
+ headers['Authorization'] = `token ${savedToken}`;
55
+ }
56
+
57
+ fetch('https://api.github.com/repos/' + match.owner + '/' + match.repo + '/contents/' + match.terms_dir + '/' + match.term.replace(/ /g, '-').toLowerCase() + '.md', { headers: headers })
58
+ .then(response => {
59
+ if (response.status === 403 && response.headers.get('X-RateLimit-Remaining') === '0') {
60
+ const resetTime = new Date(response.headers.get('X-RateLimit-Reset') * 1000);
61
+ console.error(`\n SPEC-UP-T: Github API rate limit exceeded. Try again after ${resetTime}. See https://blockchainbird.github.io/spec-up-t-website/docs/github-token/ for more info.` + "\n");
62
+
63
+ // Call the debounced error function
64
+ debouncedError();
65
+ return true;
66
+ } else {
67
+ console.log(`\n SPEC-UP-T: Github API rate limit: ${response.headers.get('X-RateLimit-Remaining')} requests remaining. See https://blockchainbird.github.io/spec-up-t-website/docs/github-token/ for more info.` + "\n");
68
+ }
69
+
70
+ return response.json();
71
+ })
72
+ .then(data => {
73
+ // Decode base64 encoded content
74
+ const decodedContent = atob(data.content);
75
+
76
+ // Diff the content of the current term-file with the content of stored version
77
+ // See https://www.npmjs.com/package/diff , examples
78
+ const diff = Diff.diffChars(match.content, decodedContent),
79
+ fragment = document.createDocumentFragment();
80
+
81
+ diff.forEach((part) => {
82
+ // green for additions, red for deletions
83
+ // grey for common parts
84
+ const color = part.added ? 'green' :
85
+ part.removed ? 'red' : 'grey';
86
+
87
+ const backgroundColor = part.added ? '#ddd' :
88
+ part.removed ? '#ddd' : 'white';
89
+
90
+ span = document.createElement('span');
91
+ span.style.color = color;
92
+ span.style.backgroundColor = backgroundColor;
93
+
94
+ span.appendChild(document
95
+ .createTextNode(part.value));
96
+ fragment.appendChild(span);
97
+ });
98
+ // Create a temporary container to hold the fragment
99
+ const tempContainer = document.createElement('div');
100
+ tempContainer.appendChild(fragment);
101
+ // Replace newlines with <br> tags
102
+ tempContainer.innerHTML = tempContainer.innerHTML.replace(/\n/g, '<br>');
103
+ showModal(tempContainer.innerHTML);
104
+ })
105
+ .catch(error => {
106
+ console.error('Error fetching content:', error);
107
+ });
108
+ }
34
109
 
35
110
  // get all elements with data-attribute “data-local-href”
36
- const elements = document.querySelectorAll('[data-local-href]');
111
+ const elements = document.querySelectorAll('.term-reference[data-local-href]');
37
112
 
38
113
  // for each element, get the value of the data-attribute “data-local-href”
39
114
  elements.forEach((element) => {
@@ -45,46 +120,63 @@ function fetchCommitHashes() {
45
120
 
46
121
  // allXrefs is an object that is available in the global scope
47
122
  allXrefs.xrefs.forEach((match) => {
123
+
124
+ //TODO: remove toLowerCase() or not?
48
125
  if (match.externalSpec === splitHref[1] && match.term.toLowerCase() === splitHref[2].toLowerCase()) {
49
- const commitHashShort = match.commitHash && match.commitHash[0] ? match.commitHash[0].substring(0, 7) : 'No hash';
126
+
127
+ // If no commit hash is found, display a message and return
128
+ if (!match.commitHash) {
129
+ const noXrefFoundMessage = document.createElement('span');
130
+ noXrefFoundMessage.classList.add('no-xref-found-message','btn');
131
+ noXrefFoundMessage.innerHTML = 'No xref found.';
132
+ element.parentNode.insertBefore(noXrefFoundMessage, element.nextSibling);
133
+
134
+ return
135
+ };
136
+
137
+ // To be used in the future
138
+ const commitHashShort = match.commitHash && match.commitHash ? match.commitHash.substring(0, 7) : 'No hash';
50
139
 
51
- // Diff of the latest commit hash of a term-file and the referenced commit hash
140
+ // Diff of the latest commit hash of a term file and the referenced commit hash
52
141
  const diff = document.createElement('a');
53
- diff.href = 'https://github.com/' + match.owner + '/' + match.repo + '/compare/' + match.commitHash[0] + '../main';
142
+ diff.href = 'https://github.com/' + match.owner + '/' + match.repo + '/compare/' + match.commitHash + '../main';
54
143
  diff.target = '_blank';
55
144
  diff.rel = 'noopener noreferrer';
56
145
  diff.classList.add('diff', 'xref-info-links', 'btn');
57
- // diff.style.cssText = 'display: inline-block; margin-left: 5px; margin-right: 5px; ';
58
- diff.innerHTML = 'Difference';
146
+ diff.innerHTML = '<svg icon><use xlink:href="#svg-github"></use></svg> &lt; &gt;';
59
147
  diff.title = 'A Diff between the current commit hash of the definition and the commit hash referenced when the link was created.';
60
148
  element.parentNode.insertBefore(diff, element.nextSibling);
61
149
 
62
-
63
150
  // Latest version of a term-file
64
151
  const latestVersion = document.createElement('a');
65
152
  latestVersion.href = 'https://github.com/' + match.owner + '/' + match.repo + '/blob/main/' + match.terms_dir + '/' + match.term.replace(/ /g, '-').toLowerCase() + '.md';
66
153
  latestVersion.target = '_blank';
67
154
  latestVersion.rel = 'noopener noreferrer';
68
155
  latestVersion.classList.add('latest-version', 'xref-info-links', 'btn');
69
- // latestVersion.style.cssText = 'display: inline-block; margin-left: 5px; margin-right: 5px; ';
70
- latestVersion.innerHTML = 'Current';
71
- latestVersion.title = "Go to the repo page of the definition's current version.";
156
+ latestVersion.innerHTML = '<svg icon><use xlink:href="#svg-github"></use></svg> NOW';
157
+ latestVersion.title = 'Go to the repo page of the definition‘s current version.';
72
158
  diff.parentNode.insertBefore(latestVersion, element.nextSibling);
73
159
 
74
-
75
- // The structure of the URL to a specific version of a file in a GitHub repository is as follows:
76
- // https://github.com/<username>/<repository>/blob/<commit-hash>/<file-path>
77
160
  // Exact commit hash at the time of referencing the file
78
161
  const exactCommitHash = document.createElement('a');
79
162
  exactCommitHash.href = 'https://github.com/' + match.owner + '/' + match.repo + '/blob/' + match.commitHash + '/' + match.terms_dir + '/' + match.term.replace(/ /g, '-').toLowerCase() + '.md';
80
163
  exactCommitHash.target = '_blank';
81
164
  exactCommitHash.rel = 'noopener noreferrer';
82
165
  exactCommitHash.classList.add('exact-commit-hash', 'xref-info-links', 'btn');
83
- // exactCommitHash.style.cssText = 'display: inline-block; margin-left: 5px; margin-right: 5px; ';
84
- // exactCommitHash.innerHTML = commitHashShort;
85
- exactCommitHash.innerHTML = "Referenced";
86
- exactCommitHash.title = "Go to the repo page of the definition's version referenced when the link was created.";
166
+ exactCommitHash.innerHTML = '<svg icon><use xlink:href="#svg-github"></use></svg> XREF';
167
+ exactCommitHash.title = 'Go to the repo page of the definition‘s version referenced when the link was created.';
87
168
  latestVersion.parentNode.insertBefore(exactCommitHash, element.nextSibling);
169
+
170
+ // Diff of the latest version and the referenced version in a modal
171
+ const showDiffModal = document.createElement('button');
172
+ showDiffModal.classList.add('show-diff-modal', 'xref-info-links', 'btn');
173
+ showDiffModal.innerHTML = '&lt; &gt;';
174
+ showDiffModal.title = 'Show diff between the latest version and the referenced version';
175
+ latestVersion.parentNode.insertBefore(showDiffModal, element.nextSibling);
176
+ showDiffModal.addEventListener('click', function (event) {
177
+ event.preventDefault();
178
+ fetchGitHubContent(savedToken, match);
179
+ });
88
180
  }
89
181
  });
90
182
  });
@@ -0,0 +1,30 @@
1
+ /**
2
+ * @author Kor Dwarshuis
3
+ * @contact kor@dwarshuis.com
4
+ * @created 2024-09-07
5
+ * @description This script adds a button next to the search bar that allows users to input their GitHub token.
6
+ */
7
+
8
+ function tokenInput() {
9
+ let buttonTokenInput = document.createElement("button");
10
+ buttonTokenInput.classList.add("button-token-input");
11
+ buttonTokenInput.classList.add("btn");
12
+ buttonTokenInput.innerHTML = "&#128273;";
13
+ document.querySelector('#container-search-h7vc6omi2hr2880').after(buttonTokenInput);
14
+ buttonTokenInput.addEventListener('click', () => {
15
+ const token = prompt('Please enter your GitHub token:');
16
+
17
+ if (!token) {
18
+ alert('GitHub token is not set.');
19
+ return;
20
+ }
21
+
22
+ // Save token to local storage
23
+ localStorage.setItem('githubToken', token);
24
+ console.log("GitHub token is set.");
25
+ });
26
+ }
27
+
28
+ document.addEventListener("DOMContentLoaded", function () {
29
+ tokenInput();
30
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "spec-up-t",
3
- "version": "1.0.6",
3
+ "version": "1.0.8",
4
4
  "description": "Technical specification drafting tool that generates rich specification documents from markdown. Forked from https://github.com/decentralized-identity/spec-up by Daniel Buchner (https://github.com/csuwildcat)",
5
5
  "main": "./index",
6
6
  "repository": {
@@ -28,6 +28,7 @@
28
28
  "dependencies": {
29
29
  "@traptitech/markdown-it-katex": "3.3.0",
30
30
  "axios": "0.21.2",
31
+ "diff": "^7.0.0",
31
32
  "find-pkg-dir": "2.0.0",
32
33
  "fs-extra": "8.1.0",
33
34
  "gulp": "4.0.2",
@@ -6,6 +6,8 @@
6
6
 
7
7
  ~ Xref example: [[xref: test-1, Aal]]
8
8
 
9
+ ~ This Xref example does not work: [[xref: does-not-exist, Foo]]
10
+
9
11
  ~ Donec aliquam et ligula id congue. Sed eu urna et tellus placerat viverra. Quisque ut posuere magna, nec accumsan augue. Nullam mauris tortor, semper finibus elementum maximus, imperdiet in felis. Suspendisse quis imperdiet nibh, eget ultrices justo. Pellentesque vitae malesuada justo. Vestibulum quis scelerisque lectus, non rutrum odio. Aenean leo orci, semper non massa sed, facilisis ornare ipsum. Morbi at sem orci. Integer eros mi, faucibus sed lorem id, pharetra imperdiet nisl. Integer viverra enim vel luctus lobortis. Ut turpis tellus, consequat nec lectus et, dictum elementum nunc. Integer rhoncus venenatis molestie. Donec egestas condimentum ligula in porttitor. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.
10
12
 
11
13
  ~ Extra text for testing purposes.
package/specs.json CHANGED
@@ -19,9 +19,6 @@
19
19
  "repo": "spec-up-t"
20
20
  },
21
21
  "external_specs": [
22
- {
23
- "PE": "https://identity.foundation/presentation-exchange"
24
- },
25
22
  {
26
23
  "test-1": "https://blockchainbird.github.io/spec-up-xref-test-1/"
27
24
  },
@@ -30,11 +27,6 @@
30
27
  }
31
28
  ],
32
29
  "external_specs_repos": [
33
- {
34
- "external_spec": "PE",
35
- "url": "https://github.com/decentralized-identity/presentation-exchange",
36
- "terms_dir": "spec"
37
- },
38
30
  {
39
31
  "external_spec": "test-1",
40
32
  "url": "https://github.com/blockchainbird/spec-up-xref-test-1",
@@ -11,6 +11,7 @@
11
11
  "assets/css/backToTop.css",
12
12
  "assets/css/notyf.css",
13
13
  "assets/css/collapse-definitions.css",
14
+ "assets/css/modal.css",
14
15
  "assets/css/index.css",
15
16
  "assets/css/bootstrap-parts.css"
16
17
  ],
@@ -28,6 +29,7 @@
28
29
  "assets/js/font-awesome.js",
29
30
  "assets/js/popper.js",
30
31
  "assets/js/tippy.js",
32
+ "assets/js/diff.min.js",
31
33
  "assets/js/edit-term-buttons.js",
32
34
  "assets/js/search.js",
33
35
  "assets/js/highlightMenuItems.js",
@@ -37,6 +39,8 @@
37
39
  "assets/js/copyAnchorToCliboard.js",
38
40
  "assets/js/notyf.js",
39
41
  "assets/js/collapse-definitions.js",
42
+ "assets/js/modal.js",
43
+ "assets/js/token-input.js",
40
44
  "assets/js/index.js"
41
45
  ]
42
46
  }