Code gets formatted as one line when using invalid language code


#1

I was trying to post code samples on another discourse forum and couldn’t get it to format it on multiple lines.

Here’s the sample

    if ((this.LocalResident) && (this.ResortAmbassador) && (SpecialOfferDiscount > 0) && UseSpecialOffer)
        //blah
    else if ((this.LocalResident) && (this.ResortAmbassador))
        //blah
    else if ((this.ResortAmbassador) && (SpecialOfferDiscount > 0) && UseSpecialOffer)
        //blah
    else if ((this.LocalResident) && (SpecialOfferDiscount > 0) && UseSpecialOffer)
        //blah
    else if (this.LocalResident)
        //blah
    else if (this.ResortAmbassador)
        //blah
    else if (this.SpecialOfferDiscount > 0 && UseSpecialOffer)
        //blah
    else
    {
        _CurrentDiscount = Discounts.None;
    }

It appears fine in preview, but doesn’t appear correctly once posted.

It also broke subsequent code blocks.

    //get the values of all the currently applied discounts
    var discounts = new[]
    {
        new 
        { 
            DiscountType = Discounts.LocalResident,
            Discount = LocalResident ? LocalResidentDiscount : 0m,
        },
        new 
        { 
            DiscountType = Discounts.Amabassador,
            Discount = ResortAmbassador ? ResortAmbassadorDiscount : 0m,
        },
        new 
        {
            DiscountType = Discounts.SpecialOffer,
            Discount = UseSpecialOffer ? SpecialOfferDiscount : 0m,
        },
        new 
        {
            DiscountType = Discounts.DiscountCode,
            Discount = DiscountCodeDiscount,
        },
    };

#2

The second code block renders fine when posted on its own

    //get the values of all the currently applied discounts
    var discounts = new[]
    {
        new 
        { 
            DiscountType = Discounts.LocalResident,
            Discount = LocalResident ? LocalResidentDiscount : 0m,
        },
        new 
        { 
            DiscountType = Discounts.Amabassador,
            Discount = ResortAmbassador ? ResortAmbassadorDiscount : 0m,
        },
        new 
        {
            DiscountType = Discounts.SpecialOffer,
            Discount = UseSpecialOffer ? SpecialOfferDiscount : 0m,
        },
        new 
        {
            DiscountType = Discounts.DiscountCode,
            Discount = DiscountCodeDiscount,
        },
    };

OK it doesn’t.

I suspected the if blocks without the braces were breaking it, but now I have no idea.

It’s difficult to test because you can’t see the result until you post, because it looks different in the preview.


(Jeff Atwood) #3

Is c# the correct tag here? I thought it was csharp or similar. Let’s try it as a simple set of 3 backticks, no language specified:

    if ((this.LocalResident) && (this.ResortAmbassador) && (SpecialOfferDiscount > 0) && UseSpecialOffer)
        //blah
    else if ((this.LocalResident) && (this.ResortAmbassador))
        //blah
    else if ((this.ResortAmbassador) && (SpecialOfferDiscount > 0) && UseSpecialOffer)
        //blah
    else if ((this.LocalResident) && (SpecialOfferDiscount > 0) && UseSpecialOffer)
        //blah
    else if (this.LocalResident)
        //blah
    else if (this.ResortAmbassador)
        //blah
    else if (this.SpecialOfferDiscount > 0 && UseSpecialOffer)
        //blah
    else
    {
        _CurrentDiscount = Discounts.None;
    }

(Jeff Atwood) #4

Per the docs

  return {
    aliases: ['csharp'],

(Sam Saffron) #5

confirmed this is only a bug for invalid language codes.

not planning on fixing this prior to the move to GitHub - markdown-it/markdown-it: Markdown parser, done right. 100% CommonMark support, extensions, syntax plugins & high speed


#6

Ah OK, that makes a lot of sense!

Is there an online reference that has a list of all of the supported language aliases?

I tried searching, but discourse search and my google-fu let me down.


(Régis Hanol) #7

It’s a site setting. Here’s what it’s set at here


#8

OK Sam, interestingly it doesn’t break for all samples where an invalid language is specified. Take the following example:

  decimal discount = 0;
  int maxIndex = -1;

  decimal[] vals = new decimal[4];

  //are all offers "on"?
  if ((this.LocalResident) && (this.ResortAmbassador) && (SpecialOfferDiscount > 0) && UseSpecialOffer)
  {
      vals[0] = this.SpecialOfferDiscount;
      vals[1] = this.ResortAmbassadorDiscount;
      vals[2] = this.LocalResidentDiscount;
  }
  //how about just the resident and ambassador offers?
  else if ((this.LocalResident) && (this.ResortAmbassador))
  {
      //return the biggest discount...
      vals[0] = 0;
      vals[1] = this.ResortAmbassadorDiscount;
      vals[2] = this.LocalResidentDiscount;
  }
  //or if just the ambassador and special offer?
  else if ((this.ResortAmbassador) && (SpecialOfferDiscount > 0) && UseSpecialOffer)
  {
      //return the biggest discount...  
      vals[0] = this.SpecialOfferDiscount;
      vals[1] = this.ResortAmbassadorDiscount;
      vals[2] = 0;
  }
  //or the ambassador and special offers
  else if ((this.LocalResident) && (SpecialOfferDiscount > 0) && UseSpecialOffer)
  {
      //return the biggest discount...
      vals[0] = this.SpecialOfferDiscount;
      vals[1] = 0;
      vals[2] = this.LocalResidentDiscount;
  }
  //else just an individual offer
  else if (this.LocalResident)
  {
      vals[0] = 0;
      vals[1] = 0;
      vals[2] = this.LocalResidentDiscount;
  }
  else if (this.ResortAmbassador)
  {
      vals[0] = 0;
      vals[1] = this.ResortAmbassadorDiscount;
      vals[2] = 0;
  }
  else if (this.SpecialOfferDiscount > 0 && UseSpecialOffer)
  {
      vals[0] = this.SpecialOfferDiscount;
      vals[1] = 0;
      vals[2] = 0;
  }
  else
  {
      _CurrentDiscount = Discounts.None;
  }

  vals[3] = this.DiscountCodeDiscount;

  //find the biggest discount...
  for (int i = 0; i != vals.Length; ++i)
  {
      if (vals[i] > discount)
      {
          discount = vals[i];
          maxIndex = i;
      }
  }
                  
  //switch statement to determine which discount has been applied and save as enum value.
  //this can be used externally to flag up any info required by the particular discount applied.
  switch (maxIndex)
  {
      case 0: this._CurrentDiscount = Discounts.SpecialOffer; break;
      case 1: this._CurrentDiscount = Discounts.Amabassador; break;
      case 2: this._CurrentDiscount = Discounts.LocalResident; break;
      case 3: this._CurrentDiscount = Discounts.DiscountCode; break;
  }

EDIT
OK, that’s clearly broken here, but wasn’t broken on our discourse install. That’s why it confused me so much, I had the samples working in the original post, including invalid language specified, but broken in subsequent posts.

Never mind, thanks for your help.


(Sam Saffron) #9

I think the full list of syntax highlighted languages that you can use in fenced code blocks is located in GitHub

However admins have control over which are supported via the “highlighted languages” sites setting.

We default to:

apache
bash
cs
cpp
css
coffeescript
diff
xml
http
ini
json
java
javascript
makefile
markdown
nginx
objectivec
ruby
perl
php
python
sql
handlebars
text

We do not include everything cause the js payload gets big really fast.


#10

Thanks @Sam and @Zogstrip! Is there a plan to expose the values to end users without having to ask admins? It’d be useful to have it in the user guide (if there is one).


(Jeff Atwood) #11

Why? As illustrated above it “just works” if you leave the language code blank. It is able to automatically infer the language based on the code posted.

(This works in most cases, it’s actually how Stack Overflow worked for a long time, and most of the time, still does…)


#12

Why not? At least as part of the FAQ. I’m pretty sure most tech sites would benefit from this.


(Sam Saffron) #13

we have some open discussions on lounge on how to deal with this stuff, still deciding on a strategy.