diff --git a/assets/css/custom.css b/assets/css/custom.css index eb10b047..ecf8c151 100644 --- a/assets/css/custom.css +++ b/assets/css/custom.css @@ -314,3 +314,28 @@ img[alt=diagram] { background-color: rgba(var(--color-primary-600), 1); border-radius: 0.09rem; } + +.p-20 { + padding: 20px; +} + +.rounded-md { + border-radius: 0.375rem; +} + + +.prose :where(ul):not(:where([class~="not-prose"] *)) { + list-style-type: disc; +} + +.p-12 { + padding: 3rem; +} + +.pt-20 { + padding-top: 5rem; +} + +.pb-8 { + padding-bottom: 2rem; +} \ No newline at end of file diff --git a/assets/js/formula-suggest.js b/assets/js/formula-suggest.js new file mode 100644 index 00000000..ee47be7b --- /dev/null +++ b/assets/js/formula-suggest.js @@ -0,0 +1,162 @@ +function sanitizeInput(input) { + return DOMPurify.sanitize(input); + } + // Function to render the preview + function renderPreview() { + const title = sanitizeInput(document.getElementById('title').value); + const body = sanitizeInput(document.getElementById('body').value); + const latex = sanitizeInput(document.getElementById('latex').value); + const tags = sanitizeInput(document.getElementById('tags').value).split(',').map(tag => tag.trim()); + const parsedBody = marked.parse(body); + + const tagsHTML = tags + .map((tag) => { + return ` + + ${tag} + + `; + }) + .join(' '); // Join the tags with a space + + const previewContent = ` +
+
+

+ ${title} +

+
+ ${tagsHTML} +
+
+ ${parsedBody} +
+
+
+ `; + + document.getElementById('preview-content').innerHTML = previewContent; + document.getElementById('preview').classList.remove('hidden'); + + // Render LaTeX + if (typeof renderMathInElement !== 'undefined') { + renderMathInElement(document.getElementById('preview-content'), { + delimiters: [ + { left: '$$', right: '$$', display: true }, + { left: '\\(', right: '\\)', display: false } + ] + }); + } + } + + // Function to send data to Discord webhook + function submitToDiscord() { + const title = sanitizeInput(document.getElementById('title').value); + const description = sanitizeInput(document.getElementById('description').value); + const tags = sanitizeInput(document.getElementById('tags').value); + const latex = sanitizeInput(document.getElementById('latex').value); + const body = sanitizeInput(document.getElementById('body').value); + + const webhookURL = 'https://discord.com/api/webhooks/1059998584889688134/eUqXbInp90bcFdL1A3ly141TAtn9jiPjbYwCzSfjPV2-4kx2UIX3M-soCJxWTrvSNbLP'; // Replace with your Discord webhook URL + + const data = { + embeds: [ + { + title: "New stemformulas.com formula suggestion", + fields: [ + { + name: "Title", + value: title, + inline: true, + }, + { + name: "Description", + value: description, + inline: true, + }, + { + name: "Tags", + value: tags, + inline: true, + }, + { + name: "LaTeX", + value: `\`\`\`latex\n${latex}\n\`\`\``, + inline: false, + }, + { + name: "Body", + value: `\`\`\`markdown\n${body}\n\`\`\``, + inline: false, + }, + ], + color: 0x00ff00, // Green color for the embed + }, + ], + }; + + fetch(webhookURL, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(data), + }) + .then((response) => { + if (response.ok) { + alert('Submission sent successfully. Thank you!'); + } else { + alert('Failed to send submission. Please try again.'); + } + }) + .catch((error) => { + console.error('Error:', error); + alert('An error occurred. Please try again.'); + }); + } + + // Attach the form submission handler + document.getElementById('formula-suggestion-form').addEventListener('submit', function(event) { + event.preventDefault(); + renderPreview(); + // Show the "Submit" button once the preview has been rendered once + document.getElementById('submit-button').classList.remove('hidden'); + }); + + function handleRateLimit() { + // limit submissions so we can't get spammed + const submitButton = document.getElementById('submit-button'); + const lastSubmissionTime = localStorage.getItem('lastSubmissionTime'); + const currentTime = new Date().getTime(); + + // Check if 5 minutes have passed since the last submission + const timeLeft = 120000 - (currentTime - lastSubmissionTime); + if (lastSubmissionTime && timeLeft > 0) { + alert(`Please wait ${Math.ceil(timeLeft / 1000)} seconds before submitting again.`); + return; + } + + // Disable the button and store the current time + submitButton.disabled = true; + localStorage.setItem('lastSubmissionTime', currentTime); + + // Submit the data to Discord + submitToDiscord(); + + // Re-enable the button after the correct time has elapsed + setTimeout(() => { + submitButton.disabled = false; + }, timeLeft); + } + + // Attach the "Submit" button click handler + document.getElementById('submit-button').addEventListener('click', function() { + handleRateLimit(); + }); + + // Render the preview when the page loads + document.addEventListener('DOMContentLoaded', function() { + renderPreview(); + }); \ No newline at end of file diff --git a/assets/js/search.js b/assets/js/search.js index 69d1ba6a..8da48413 100644 --- a/assets/js/search.js +++ b/assets/js/search.js @@ -1,5 +1,6 @@ var fuse; var onMainPage = document.getElementById("search-query-main"); +var onSubmitPage = document.getElementById("formula-suggestion-form") var showButtons = document.querySelectorAll("[id^='search-button']"); if(onMainPage){ var input = document.getElementById("search-query-main"); @@ -41,6 +42,12 @@ if (!(onMainPage)){ document.addEventListener("keydown", function (event) { // Forward slash to open search wrapper if (event.key == "/") { + // ignore if we're on the suggest page + if (onSubmitPage){ + event.preventDefault(); + return; + } + if (!searchVisible) { event.preventDefault(); displaySearch(); diff --git a/config/_default/menus.en.toml b/config/_default/menus.en.toml index 532a3bc1..d32a5f2f 100644 --- a/config/_default/menus.en.toml +++ b/config/_default/menus.en.toml @@ -26,8 +26,8 @@ weight = 30 [[main]] - name = "suggest" - pageRef = "suggest" + name = "submit" + pageRef = "submit" weight = 40 [[main]] diff --git a/content/about/_index.md b/content/about/_index.md index 19d0b902..345757f9 100644 --- a/content/about/_index.md +++ b/content/about/_index.md @@ -5,7 +5,7 @@ showTableOfContents: true --- Stemformulas is a website dedicated to providing a single place to look for STEM formulas. It has a long way to go to become that, but the foundation is laid out. -It was made by a few engineering students who were frustrated with the current state of formula searching online. This frustration lead to the focus of the features of the site, which include: +It was made by a few engineering students (lead by [@linguinelabs](https://x.com/linguinelabs)) who were frustrated with the current state of formula searching online. This frustration lead to the focus of the features of the site, which include: - The search bar being in focus on site load, so you can search for a formula quickly - LaTeX being copyable by just clicking on it on any formula's page @@ -24,7 +24,8 @@ It is hosted on [GitHub Pages](https://pages.github.com/), deployed conveniently ## Contributing If you want to add a formula to this site, there are two ways you can do so. -1. Submit a suggestion for a formula on our [Google Form](https://forms.gle/EWjwFmiEQrrjsZEF9) (can also be filled out on our [suggest](/suggest) page). This form is also used for general feedback, e.g. if one of our pages has a mistake. +1. Submit a formula on our [suggest](/suggest) page). This has been revamped to have a preview for ease of use. -2. Create a pull request on the -[GitHub repo](https://github.com/stemformulas/stemformulas.github.io). More detailed instructions can be found in the [README](https://github.com/stemformulas/stemformulas.github.io#adding-a-formula-by-submitting-a-pull-request). +2. Create a pull request directly on the +[GitHub repo](https://github.com/stemformulas/stemformulas.github.io). This is of course easier for us to merge, but requires more effort from you. +More detailed contribution instructions can be found in the [README](https://github.com/stemformulas/stemformulas.github.io?tab=readme-ov-file#adding-a-formula). diff --git a/content/suggest/_index.md b/content/submit/_index.md similarity index 71% rename from content/suggest/_index.md rename to content/submit/_index.md index b4e36478..d3af8ed4 100644 --- a/content/suggest/_index.md +++ b/content/submit/_index.md @@ -2,6 +2,10 @@ title: "Suggest" description: "Make a suggestion" --- -
+{{}} + + + + diff --git a/layouts/_default/submit.html b/layouts/_default/submit.html new file mode 100644 index 00000000..597f5703 --- /dev/null +++ b/layouts/_default/submit.html @@ -0,0 +1,133 @@ +{{ define "main" }} + + +
+
+

+ submit a formula +

+

+ Replace the example fields below, preview your submission, and then submit! +

+
+ +
+
+
+
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ + +
+ + +
+
+
+ + +
+ + {{ $js := resources.Get "js/formula-suggest.js" | fingerprint }} + +{{ end }} \ No newline at end of file diff --git a/layouts/_default/term.html b/layouts/_default/term.html index 09e73237..c61fdb1d 100644 --- a/layouts/_default/term.html +++ b/layouts/_default/term.html @@ -3,21 +3,22 @@ + + document.addEventListener("DOMContentLoaded", function() { + renderMathInElement(document.body, { + // customised options + // • auto-render specific keys, e.g.: + delimiters: [ + {left: '$$', right: '$$', display: true}, + ], + // • rendering keys, e.g.: + fleqn : true, + maxSize: 100, + displayMode: true, + }); + }); +
{{ if .Params.showBreadcrumbs | default (.Site.Params.list.showBreadcrumbs | default false) }} {{ partial "breadcrumbs.html" . }} diff --git a/layouts/partials/head.html b/layouts/partials/head.html index cd74c4a0..b3a66546 100644 --- a/layouts/partials/head.html +++ b/layouts/partials/head.html @@ -141,4 +141,7 @@ {{ partial "extend-head.html" .Site }} {{ end }} + + +