Embedding Discourse Community into the Original Unix & Linux Forums

Today, I added some code to embed topics (the last reply) into the original forums.

At first, I experimented with using a wide range of topic ids and used a random number generator across that range, but many of the posts here at Community do not format so nicely in old forums (not bad, but not perfect), so I decided to go the "selective route" and create a playlist.

If anyone has any topics they would like to see in the embedded playlist please send me a note or post back. The purpose of the embedded code is to continue to promote visitors to the original site to sign up and post here:

Example 1:

Screen Shot 2020-05-31 at 7.54.04 PM

Example 2:

Screen Shot 2020-05-31 at 7.53.38 PM

The embedding code from Discourse looks like this:

<div id="discourse-comments"></div>

<script type="text/javascript">
  let playlist = [378686, 378653, 378686];
  let max = playlist.length - 1;
  let index = Math.round(randomNumber(0, max));
  DiscourseEmbed = {
    discourseUrl: "https://community.unix.com/",
    topicId: playlist[index],
  };

  (function () {
    var d = document.createElement("script");
    d.type = "text/javascript";
    d.async = true;
    d.src = DiscourseEmbed.discourseUrl + "javascripts/embed.js";
    (
      document.getElementsByTagName("head")[0] ||
      document.getElementsByTagName("body")[0]
    ).appendChild(d);
  })();

  function randomNumber(min, max) {
    return Math.random() * (max - min) + min;
  }
</script>

In this embedding code, I give extra weight to one topic. We can easily add more topics to the playlist.

The custom CSS for this code (not yet optimized) to get it to match the original site is:

a.button{
    background-color:#0275d8;
    border-radius: 4px;
    font-size: 0.85em;
    font-family: 'Montserrat',sans-serif;
    margin-right:20px;
}
span.replies{
    color:#170072;
    font-family: 'Montserrat',sans-serif;
    opacity: 0.7  !important;
}
div.cooked > p > a, #post-1031906 > a{
    color:#170072 !important;
    font-family: 'Montserrat',sans-serif;
    opacity: 0.9  !important;
}
article.post{
     font-family: 'Montserrat',sans-serif;
}
span.title{
    display:none;
}
div.cooked > h3 > a{
    background-color: transparent !important;
    color:#170072 !important;
    font-family: 'Montserrat',sans-serif !important;
    opacity: 0.9  !important;
}

Free free to suggest any topic ids on this forum you would like to see in the embedded code playlist.

1 Like

Update:

Added the embedding code to mobile view; making some simple @media CSS changes:

@media only screen and (min-width: 600px) {
  a.button {
    background-color: #0275d8;
    border-radius: 4px;
    font-size: 0.85em;
    font-family: "Montserrat", sans-serif;
    margin-right: 20px;
  }
  span.replies {
    color: #170072;
    font-family: "Montserrat", sans-serif;
    opacity: 0.7 !important;
  }
  div.cooked > p > a,
  #post-1031906 > a {
    color: #170072 !important;
    font-family: "Montserrat", sans-serif;
    opacity: 0.9 !important;
  }
  article.post {
    font-family: "Montserrat", sans-serif;
  }
  span.title {
    display: none;
  }
  div.cooked > h3 > a {
    background-color: transparent !important;
    color: #170072 !important;
    font-family: "Montserrat", sans-serif !important;
    opacity: 0.9 !important;
  }
}
@media only screen and (max-width: 599px) {
  a.button {
    background-color: #000000;
    border-radius: 4px;
    font-size: 0.85em;
    margin-right: 20px;
  }
  span.replies {
    color: #000000;
    opacity: 0.7 !important;
  }
  div.cooked > p > a,
  #post-1031906 > a {
    color: #000000 !important;
    opacity: 0.9 !important;
  }
  span.title {
    display: none;
  }
  div.cooked > h3 > a {
    background-color: transparent !important;
    color: #000000 !important;
    opacity: 0.9 !important;
  }
  body > footer > a:nth-child(1) > img{
      display:none;
  }
}

Example 1:

Example 2:

OK, am now experimenting with a new approach where we use the actual communitiy topic id in the embedding code, when available and here it at least one reply:

Example:

<div id='discourse-comments' style="border:1px solid silver;padding:10px;"></div>

<script type="text/javascript">
  let playlist = [];
  if(discourse_topic_id > 0){
     playlist = [discourse_topic_id];
  }
  else{
     playlist = [378686];
  }
  let max = playlist.length - 1;
  let index = Math.round(randomNumber(0, max));
  DiscourseEmbed = { discourseUrl: 'https://community.unix.com/',
                     topicId: playlist[index]};

  (function() {
    var d = document.createElement('script'); d.type = 'text/javascript'; d.async = true;
    d.src = DiscourseEmbed.discourseUrl + 'javascripts/embed.js';
    (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(d);
  })();

  function randomNumber(min, max) {  
    return Math.random() * (max - min) + min; 
   }  
</script>

Where diiscourse_topic_id is derived from the original site thread:

<if condition="$discourse_topic_id > 0 && $totalposts > 1">
   <script>
     let discourse_topic_id = $discourse_topic_id;
   </script>
<else />
   <script>
     let discourse_topic_id = 0;
   </script>
</if>

Changed the embedded CSS a bit to clean up some issues:

@media only screen and (min-width: 600px) {
    div.cooked > a,body > header > a{
        display:none;
    }
  a.button {
    background-color: #0275d8;
    border-radius: 4px;
    font-size: 0.85em;
    font-family: "Montserrat", sans-serif;
    margin-right: 20px;
  }
  span.replies {
    color: #170072;
    font-family: "Montserrat", sans-serif;
    opacity: 0.7 !important;
  }
  div.cooked > p > a,
  #post-1031906 > a {
    color: #170072 !important;
    font-family: "Montserrat", sans-serif;
    opacity: 0.9 !important;
  }
  article.post {
    font-family: "Montserrat", sans-serif;
  }
  span.title {
    display: none;
  }
  div.cooked > h3 > a {
    background-color: transparent !important;
    color: #170072 !important;
    font-family: "Montserrat", sans-serif !important;
    opacity: 0.9 !important;
  }
}
@media only screen and (max-width: 599px) {
  div.cooked > a,body > header > a{
        display:none;
    }
  a.button {
    background-color: #000000;
    border-radius: 4px;
    font-size: 0.85em;
    margin-right: 20px;
  }
  span.replies {
    color: #000000;
    opacity: 0.7 !important;
  }
  div.cooked > p > a,
  #post-1031906 > a {
    color: #000000 !important;
    opacity: 0.9 !important;
  }
  span.title {
    display: none;
  }
  div.cooked > h3 > a {
    background-color: transparent !important;
    color: #000000 !important;
    opacity: 0.9 !important;
  }
  body > footer > a:nth-child(1) > img{
      display:none;
  }
}

Will optimize (shorten) CSS in the future, if we decide to keep this embedded code on the original site (now experimental).

Example Screenshot:

FYI, and for those interested, as expected, the embedding (embedded) code causes the traffic stats for the site to inflate (artificially) because this code queries the site just the same as any other query:

Today, I added a random number generator to show this embedded code on the old site less, just to compare.

This also illustrates how much traffic the original site sets compared to this one.

Discourse (indeed most javascript framework and infinite scroll sites) have very poor SEO performance compared to traditional paged sites.

Frankly speaking, the SEO performance of Discourse, is not impressive at all.

1 Like