Skip to content

Commit

Permalink
Merge pull request #6 from maksii/betterreleasetoas
Browse files Browse the repository at this point in the history
marked lib to render github release and images. css for toast
  • Loading branch information
maksii authored Jun 16, 2024
2 parents 3fb95ca + 40218db commit 9217073
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 34 deletions.
5 changes: 5 additions & 0 deletions app/static/css/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,9 @@ main > .container-fluid {

.card-body {
min-height: 190px;
}

.toast-body {
max-height: 300px;
overflow-y: auto;
}
79 changes: 45 additions & 34 deletions app/static/js/titles.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,42 +289,53 @@ $(document).ready(function() {

const repo = 'maksii/Toloka2Web'; // Change this to your repository

function checkReleaseStatus() {
const lastCheckedRelease = localStorage.getItem('lastCheckedRelease');
fetch(`https://api.github.com/repos/${repo}/releases/latest`)
.then(response => response.json())
.then(data => {
const latestRelease = data.tag_name;
if (lastCheckedRelease !== latestRelease) {
showReleaseToast(data.name, data.body, latestRelease);
}
})
.catch(error => console.error('Error fetching release data:', error));
}

function showReleaseToast(title, content, latestRelease) {
const toastContainer = document.querySelector('.toast-container');
const toastHTML = `
<div class="toast" role="alert" aria-live="assertive" aria-atomic="true" data-bs-autohide="false">
<div class="toast-header">
<strong class="me-auto">New Release: ${title}</strong>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">
${content}
</div>
function checkReleaseStatus() {
const lastCheckedRelease = localStorage.getItem('lastCheckedRelease');
fetch(`https://api.github.com/repos/${repo}/releases/latest`)
.then(response => response.json())
.then(data => {
const latestRelease = data.tag_name;
if (lastCheckedRelease !== latestRelease) {
const formattedContent = formatContent(data.body);
showReleaseToast(data.name, formattedContent, latestRelease);
}
})
.catch(error => console.error('Error fetching release data:', error));
}

function showReleaseToast(title, content, latestRelease) {
const toastContainer = document.querySelector('.toast-container');
const toastHTML = `
<div class="toast" role="alert" aria-live="assertive" aria-atomic="true" data-bs-autohide="false">
<div class="toast-header">
<strong class="me-auto">New Release: ${title}</strong>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
`;
toastContainer.insertAdjacentHTML('beforeend', toastHTML);
const toastElement = new bootstrap.Toast(toastContainer.lastElementChild);
toastElement.show();

// Add event listener for when the toast is hidden
toastContainer.lastElementChild.addEventListener('hidden.bs.toast', function () {
localStorage.setItem('lastCheckedRelease', latestRelease);
});
}
<div class="toast-body">
${content}
</div>
</div>
`;
toastContainer.insertAdjacentHTML('beforeend', toastHTML);
const toastElement = new bootstrap.Toast(toastContainer.lastElementChild);
toastElement.show();

// Add event listener for when the toast is hidden
toastContainer.lastElementChild.addEventListener('hidden.bs.toast', function () {
localStorage.setItem('lastCheckedRelease', latestRelease);
});
}

function formatContent(markdown) {
// Convert markdown to HTML
let html = marked.parse(markdown);

// Modify image sizes
html = html.replace(/<img /g, '<img style="max-width:100%;height:auto;" ');

// Sanitize HTML
return DOMPurify.sanitize(html);
}
checkReleaseStatus();

});
Expand Down
4 changes: 4 additions & 0 deletions app/templates/index.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
{% extends 'base.html' %}

{% block additional_head %}
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/dompurify/dist/purify.min.js"></script>

<script src="{{ url_for('static', filename='/js/titles.js') }}"></script>
<script src="{{ url_for('static', filename='/js/toloka.js') }}"></script>

{% endblock %}

{% block content %}
Expand Down

0 comments on commit 9217073

Please sign in to comment.