-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: use json in chipstatus shortcode and update related content * feat: add question icon and document its purpose * feat: add logic to process generated_time key as ISO8601 time * feat: add chipstatus shortcode with generated_time * feat: change time format * chore: update json key names
- Loading branch information
Showing
2 changed files
with
53 additions
and
21 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
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 |
---|---|---|
@@ -1,23 +1,45 @@ | ||
<div id="content">Loading...</div> | ||
{{ $uniqueID := .Get "jsonKey" | urlize }} <!-- Generate a unique ID --> | ||
<span id="content-{{ $uniqueID }}">Loading...</span> | ||
|
||
{{ $contentPath := .Get "contentPath" }} | ||
{{ $jsonKey := .Get "jsonKey" }} | ||
{{ $fullURL := printf "%s%s" .Site.BaseURL $contentPath }} | ||
|
||
<script> | ||
const url = '{{ .Get "url" }}'; // Get the URL from the shortcode parameter | ||
document.addEventListener("DOMContentLoaded", function () { | ||
const url = '{{ $fullURL }}'; | ||
const jsonKey = '{{ $jsonKey }}'; | ||
const uniqueID = '{{ $uniqueID }}'; | ||
|
||
fetch(url) // Fetch the JSON file | ||
.then(response => { | ||
if (!response.ok) { | ||
throw new Error('Error fetching the JSON data'); | ||
} | ||
return response.json(); | ||
}) | ||
.then(data => { | ||
if (!data[jsonKey]) { | ||
throw new Error(`Key "${jsonKey}" not found in the JSON data`); | ||
} | ||
|
||
fetch(url) // Fetch the markdown content | ||
.then(response => { | ||
if (!response.ok) { | ||
throw new Error('Error fetching the markdown data'); | ||
} | ||
return response.text(); | ||
}) | ||
.then(markdown => { | ||
// Convert the markdown to HTML | ||
let htmlContent = marked.parse(markdown); | ||
let content; | ||
if (jsonKey === "timestamp") { | ||
// Convert ISO8601 time to desired local time format | ||
const isoTime = data[jsonKey]; | ||
const options = { day: 'numeric', month: 'short', year: 'numeric', hour: 'numeric', minute: '2-digit', hour12: true }; | ||
content = new Intl.DateTimeFormat('en-GB', options).format(new Date(isoTime)); | ||
} else { | ||
// Extract the value for the key and convert the markdown to HTML | ||
let markdown = data[jsonKey]; | ||
content = marked.parse(markdown); | ||
} | ||
|
||
// Inject the HTML into the content div | ||
document.getElementById('content').innerHTML = htmlContent; | ||
}) | ||
.catch(error => { | ||
document.getElementById('content').textContent = 'Error loading markdown content: ' + error.message; | ||
}); | ||
// Inject the content into the unique content span | ||
document.getElementById(`content-${uniqueID}`).innerHTML = content; | ||
}) | ||
.catch(error => { | ||
document.getElementById(`content-${uniqueID}`).textContent = 'Error loading content: ' + error.message; | ||
}); | ||
}); | ||
</script> |