<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Angelo Corleone</title>
    <description>The latest articles on DEV Community by Angelo Corleone (@angelocorleone).</description>
    <link>https://dev.to/angelocorleone</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F702032%2F23210f27-e531-48e8-9947-712326f9702c.png</url>
      <title>DEV Community: Angelo Corleone</title>
      <link>https://dev.to/angelocorleone</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/angelocorleone"/>
    <language>en</language>
    <item>
      <title>Add basic search to your Hugo website</title>
      <dc:creator>Angelo Corleone</dc:creator>
      <pubDate>Sat, 11 Sep 2021 10:13:15 +0000</pubDate>
      <link>https://dev.to/angelocorleone/add-basic-search-to-your-hugo-website-3c82</link>
      <guid>https://dev.to/angelocorleone/add-basic-search-to-your-hugo-website-3c82</guid>
      <description>&lt;h2&gt;
  
  
  Issue
&lt;/h2&gt;

&lt;p&gt;You want to add a basic search functionality to your Hugo website, however the suggestions from the &lt;a href="https://gohugo.io/tools/search/" rel="noopener noreferrer"&gt;official documentation&lt;/a&gt; feels a bit too much for your needs. Like if you want to search by one parameter only, or you don’t want to install additional packages, then this quick tutorial is a decent way to achieve this.&lt;/p&gt;

&lt;h2&gt;
  
  
  Solution
&lt;/h2&gt;

&lt;p&gt;Full Disclosure on Hugo: I perhaps have less than 24 actual hours of experience with Hugo, just used a theme and modified it a bit to get what I want. I apologize in advance as how I do things here aren’t best practice in Hugo.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In your theme’s &lt;code&gt;layouts &amp;gt; partials&lt;/code&gt; folder, create the html that houses the search input and the results (I’m not even sure if this folder structure is standard).
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!--The css styles can be ignored.--&amp;gt;
&amp;lt;div id="search-container" class="columns"&amp;gt;
    &amp;lt;div class="column is-three-quarters-desktop"&amp;gt;
        &amp;lt;input placeholder="Search all posts..." id="search-input" class="input"/&amp;gt;
        &amp;lt;div id="search-result"&amp;gt;
        &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Inside the &lt;code&gt;layouts &amp;gt; _default&lt;/code&gt; folder, create an index.json, this will have the data we will search client-side
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{{ $.Scratch.Add "index" slice }}
{{ range where .Site.RegularPages "Section" "blog" }}
{{ $.Scratch.Add "index" (dict "title" .Title "permalink" .Permalink "tags" .Params.tags ) }}
{{ end }}
{{ $.Scratch.Get "index" | jsonify }}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Note: We can get all the regular pages there on the 2nd line, or filter the range to something else. As for the index, add only the values you want to search &lt;code&gt;(title, link and tags for my case)&lt;/code&gt;, to keep it as small as possible&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In your &lt;code&gt;config.toml&lt;/code&gt;, tell your home page to output json as well
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; [outputs]
    home = ['html','json']

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;In your &lt;code&gt;assets&lt;/code&gt; folder, add the JavaScript file that will do the searching
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(() =&amp;gt; {
    let searchIndex = null;

    // populate the search index object
    const request = new XMLHttpRequest();
    request.onreadystatechange = () =&amp;gt; {
        // 4 - done request, 200 - OK
        if (request.readyState === 4 &amp;amp;&amp;amp; request.status === 200) {
            searchIndex = JSON.parse(request.responseText);
        }
    }
    request.open('GET', '/index.json');
    request.send();

    const search = async (query, index) =&amp;gt; {
        // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping
        const regex = new RegExp(query.toLowerCase().replace(/[.*+?^${}()|[\]\\]/g, '\\$&amp;amp;'));
        const keys = ['permalink','tags','title']; //can add other keys here if wanted

        // get only the stuff that matches the regex pattern
        const result = index.filter((value) =&amp;gt; {
            for (const key of keys) {
                if (!value[key]) continue;

                if (Array.isArray(value[key])){ 
                    // search each value in the nested array
                    if (value[key].some(v =&amp;gt; regex.test(v.toLowerCase()))) return true;
                }else if(regex.test(value[key].toLowerCase())) return true;
            }

            return false;
        });

        // put the results in a list that's rendered on the screen
        if (result.length &amp;gt; 0) {
            const ul = document.createElement('ul');
            ul.setAttribute('class','pt-1');

            result.forEach(item =&amp;gt; {
                const li = document.createElement('li');
                const aTag = document.createElement('a');

                li.setAttribute('class','px-1 py-1');
                aTag.setAttribute('href', item.permalink);
                aTag.setAttribute('class', 'is-block');
                aTag.innerHTML = item.title;

                li.appendChild(aTag);
                ul.appendChild(li);
            })

            return ul
        }

        return null;
    }

    const searchResult = document.getElementById('search-result');
    const searchInput = document.getElementById('search-input');

    // search on input
    searchInput?.addEventListener('input', async (e) =&amp;gt; {
        let result = null;
        if (e.currentTarget.value) result = await search(e.currentTarget.value, searchIndex);
        if (searchResult.lastChild) searchResult.removeChild(searchResult.lastChild);
        if (result) searchResult.appendChild(result);
    });

    // clear the search input - this is so when you press back in your browser, 
    // the search input is cleared nicely
    searchInput?.addEventListener('focusout', (e) =&amp;gt; {
        e.target.value = '';
        // timeout is there because removing the element instantly won't let you hit the anchor tag
        setTimeout(() =&amp;gt; {
            if (searchResult.lastChild) searchResult.removeChild(searchResult.lastChild);
        }, 150);
    });
})();

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Then call this script somewhere in your homepage, in my case it is on another partial page
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!-- At the home page --&amp;gt;
{{ partial "footer/scripts.html" . }}

&amp;lt;!-- At the partial page. layouts &amp;gt; partials &amp;gt; footer folder --&amp;gt;
{{ range $value := .Site.Params.customJS }}
    {{ $script := resources.Get . | js.Build (dict "minify" true) }}
    &amp;lt;script src="{{ $script.Permalink }}"&amp;gt;&amp;lt;/script&amp;gt;
{{ end }}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add your own styling to finish it off. Here is a working demo on my site’s &lt;a href="https://angeloryndon.com/blog/" rel="noopener noreferrer"&gt;blog area&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;One downside I can imagine is when there are lots of pages in your site, the index file will be big, which will slow down this solution. If you also search the contents of each page, the index file can get bigger even faster. For small sites however (I’m guessing less than 5,000 pages without searching the page’s content), this solution should work pretty well.&lt;/p&gt;

</description>
      <category>tutorial</category>
      <category>hugo</category>
      <category>staticsite</category>
    </item>
    <item>
      <title>Looking for advice about the first job after graduating</title>
      <dc:creator>Angelo Corleone</dc:creator>
      <pubDate>Fri, 10 Sep 2021 21:27:39 +0000</pubDate>
      <link>https://dev.to/angelocorleone/looking-for-advice-about-the-first-job-after-graduating-365n</link>
      <guid>https://dev.to/angelocorleone/looking-for-advice-about-the-first-job-after-graduating-365n</guid>
      <description>&lt;p&gt;Hello! I hope everybody is keeping safe and healthy. I'm graduating this December and accepted an offer from the company I was working part-time on while I was studying. For the country I am in, they gave a great offer to someone who is about to graduate. The company is a very small startup. The main reason I accepted it was I felt it was a good way to thank them, cuz they gave me an opportunity while I was still at Uni. Also, I read somewhere that it is a good way to grow on a codebase you worked on before. I wanna see the mistakes, the good decisions, essentially learn on the previous work I made while I was working part-time, well I hope I do see them :D&lt;/p&gt;

&lt;p&gt;My question is should I have applied at other companies as well before accepting, because I have a good feeling I could've nabbed some offers on the well-known ones in our country (had commercial dev experience while studying, also a bit before studying. Had decent grades, had a non-trivial project from school - well for me it was).&lt;/p&gt;

&lt;p&gt;There were a few companies that I felt were really good when they did that presentation thingys students attend.&lt;/p&gt;

&lt;p&gt;Am I slowing my career growth? Because at the moment, this small startup is using older tech by web standards. Or is my previous reasoning better (grow on a codebase, give it some time, yada yada). I have a feeling I might not be able to go full on studying new tech to keep up outside of work, or is that a must anyways - essentially at most work places you will be working with old tech?. Seeking advice from experienced people, and thanks in advance for your time :)&lt;/p&gt;

</description>
      <category>career</category>
    </item>
  </channel>
</rss>
