private static void AppendHugoShortcodeParameterValue( StringBuilder sb, string parameterName, string parameterValue, bool omitIfNullOrWhitespace = true, bool appendNewLine = true) { if (omitIfNullOrWhitespace == true && string.IsNullOrWhiteSpace(parameterValue) == true) { return; } // If the parameter value contains a backslash and a double quote, // then simply escaping the double quote(s) causes the backslashes // to be omitted from the HTML generated by Hugo. To avoid this, // use a string literal by using back quotes instead of double // quotes (https://golang.org/ref/spec#String_literals) bool valueContainsBackslashesAndQuotes = parameterValue.Contains(@"\") && parameterValue.Contains("\""); var encodedParameterValue = HtmlDocumentHelper.NormalizeWhitespace(parameterValue) .Replace("\"", """) .Replace(""", "\\"") .Trim(); var quote = "\""; if (valueContainsBackslashesAndQuotes == true) { if (parameterValue.Contains("`") == true) { throw new ArgumentException( "The parameter value cannot contain a backslash, a" + " double quote, and a back quote.", nameof(parameterValue)); } quote = "`"; // Do not escape double quotes when using string literals encodedParameterValue = encodedParameterValue.Replace( "\\"", "\""); } if (string.IsNullOrWhiteSpace(parameterName) == false) { sb.Append($" {parameterName}={quote}{encodedParameterValue}{quote}"); } else { sb.Append($" {quote}{encodedParameterValue}{quote}"); } if (appendNewLine == true) { sb.AppendLine(); } }