HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
Any URLs added here will be added as <link>
s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
Any URL's added here will be added as <script>
s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
Search for and use JavaScript packages from npm here. By selecting a package, an import
statement will be added to the top of the JavaScript editor for this package.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<input type="text" id="searchInput" placeholder="Enter your search query" />
<button class="search-btn">Search</button>
<div id="searchResults"></div>
// Sample data
let documents = [
"August Host is a tech company based in Taunggyi, Myanmar, uses advanced technology to help clients achieve their software and hardware solutions.",
"August Host is located at No 13/13, Myat Lay Street, Taunggyi; Myanmar.",
"August Host Dev Team use Git, Trello, React, Javascript, Typescript, GraphQL, Laravel, Nextjs, Wordpress, Vue, Cockpit CMS, Headless CMS, Serverless, SSG, SSR.",
"August Host was founded in 2018 by tech enthusiasts Ronald Aug and Kyaw Swar Aye, August Host emerged as the first web agency in Taunggyi, Myanmar. Ronald, with a background in tech and a passion for remote work culture, decided to establish the agency in his hometown, where he met Kyaw Swar Aye, a budding programmer eager to learn app development. Together, they envisioned filling the gap in the market by becoming pioneers in web development in the region.",
"Success Story: While August Host is still on its journey to success, the agency has made significant strides in the industry. Despite facing challenges such as the political instability in Myanmar, the team remains dedicated to their vision of delivering top-quality digital solutions to clients across South East Asia.",
"Vision: August Host aspires to become a renowned web agency not only in Myanmar but also in the wider South East Asia region. By consistently providing innovative and exceptional services, the agency aims to establish itself as a leader in the industry, setting new standards for digital excellence.",
"The mission of August Host is to help clients transform their tasks from paper to digital, enhancing efficiency and speeding up daily operations. By offering high-quality digital services from Taunggyi to clients across South East Asia, the agency aims to make a positive impact on businesses seeking to thrive in the digital age.",
"August Host has had the privilege of working with esteemed clients such as Plan International, Myanmar Private Equity and Venture Capital Association (MPEVCA), and others. One notable project includes the development of the Yangon Evacuation Map for Plan International, providing vital safety information for residents in Myanmar.",
"The talented team at August Host includes Ronald Aug, Kyaw Swar Aye, Poe Eain, and Thura, each bringing a unique set of skills and expertise to the table. From full stack development to app programming and design, the team collaborates to deliver tailored digital solutions to meet client needs.",
"Challenges: Despite facing challenges in the form of political instability and other obstacles, August Host remains committed to pushing boundaries and overcoming hurdles to achieve success. The agency continues to strive for excellence in the face of adversity, demonstrating resilience and determination in their pursuit of digital innovation.",
"August Host offers a range of services including web development, app programming, digital marketing, SEO, and tech consulting. Using modern tools and technologies such as PHP, JS, Typescript, and Serverless, the agency creates custom websites and applications for clients, while also providing consultation on advertising, SEO, and domain email creation."
];
// Transform data
documents = documents.map((d, i) => {
return { id: i + 1, content: d };
});
const searchBtn = document.querySelector('.search-btn');
searchBtn.addEventListener('click', () => {
const queryInput = document.getElementById('searchInput');
const query = queryInput.value.toLowerCase();
const results = performSearch(query);
// Display the search results
const searchResults = document.getElementById('searchResults');
searchResults.innerHTML = '';
results.forEach((result) => {
const doc = documents.find((d) => d.id === result.id);
const resultElement = document.createElement('div');
resultElement.textContent = `${doc.content} (Similarity: ${result.similarity.toFixed(2)})`;
searchResults.appendChild(resultElement);
});
});
function performSearch(query) {
// Preprocess the documents
const docTokensArray = documents.map((doc) => tokenize(doc.content.toLowerCase()));
console.log(docTokensArray);
// Calculate TF-IDF vectors for documents
const tfidfVectors = docTokensArray.map((tokens) => calculateTfIdf(tokens, docTokensArray));
// Tokenize the query and calculate its TF-IDF vector
const queryTokens = tokenize(query);
console.log(queryTokens);
const queryTfIdf = calculateTfIdf(queryTokens, docTokensArray);
// Calculate the cosine similarity between the query and each document
const results = tfidfVectors.map((tfidfVector, index) => {
const cosineSimilarity = calculateCosineSimilarity(queryTfIdf, tfidfVector);
return { id: documents[index].id, similarity: cosineSimilarity };
});
// Sort the results by similarity in descending order
results.sort((a, b) => b.similarity - a.similarity);
return results;
}
function tokenize(text) {
return text.split(/\W+/).filter(token => token.length > 0);
}
function calculateTfIdf(tokens, docTokensArray) {
const tf = {};
const df = {};
const idf = {};
tokens.forEach((token) => {
tf[token] = (tf[token] || 0) + 1;
});
docTokensArray.forEach((docTokens) => {
const uniqueTokens = new Set(docTokens);
uniqueTokens.forEach((token) => {
df[token] = (df[token] || 0) + 1;
});
});
Object.keys(tf).forEach((token) => {
idf[token] = Math.log(docTokensArray.length / (df[token] || 1));
});
const tfidf = {};
Object.keys(tf).forEach((token) => {
tfidf[token] = tf[token] * idf[token];
});
return tfidf;
}
function calculateCosineSimilarity(vectorA, vectorB) {
const dotProduct = Object.keys(vectorA).reduce((sum, key) => sum + (vectorA[key] * (vectorB[key] || 0)), 0);
const magnitudeA = Math.sqrt(Object.values(vectorA).reduce((sum, value) => sum + value ** 2, 0));
const magnitudeB = Math.sqrt(Object.values(vectorB).reduce((sum, value) => sum + value ** 2, 0));
return dotProduct / (magnitudeA * magnitudeB);
}
Also see: Tab Triggers