Skip to content

Instantly share code, notes, and snippets.

View RickStrahl's full-sized avatar

Rick Strahl RickStrahl

View GitHub Profile
@RickStrahl
RickStrahl / MarkdownMonster-Mathjax.md
Created January 29, 2019 04:12
Markdown Monster Math Expressions with MathJax
useMath
true

Math Expressions using MathJax

Markdown Monster has built in support for rendering Math expressions using Latex, MathML and AsciiMath syntax. In order to use Math expressions on your page you have to explicit ask for it via the useMath YAML header:

---
useMath: true
@RickStrahl
RickStrahl / CompilingCSharpCode.cs
Last active April 3, 2024 14:11
A few different approaches to dynamically execute C# code dynamically at runtime from a string of code.
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Scripting;
using Microsoft.CodeAnalysis.Scripting;
using Mono.CSharp;
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Diagnostics;
using System.Dynamic;
using System.Linq;
@RickStrahl
RickStrahl / GitHubRespositoryGraphQl.cs
Last active February 23, 2019 02:41
Recursively parse a Github Repository with GraphQL
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Octokit.GraphQL;
using Octokit.GraphQL.Model;
namespace GithubGraphQl
{

You can do simple date formatting with a library by using the Intl.DateTimeFormat JavaScript class.

MDN for DateTimeFormat

alert( formatDate(new Date()));
// Apr 11, 2019, 1:10:40 PM

function formatDate(date, locale) {
@RickStrahl
RickStrahl / AbsoluteUriUrlEncoding.cs
Last active November 27, 2023 13:02
UrlEncoding on local Urls producing double encoded Uri.AbsoluteUri values
void Main()
{
var part1 = new Uri("C:\\temp\\"); // double encodes when combining parts
var part2 = "assets/Image%20File.jpg";
var uri = new Uri(part1, part2);
uri.Dump();
uri.ToString().Dump(); // still encoded and shouldn't be
@RickStrahl
RickStrahl / LegacyHostEnvironment.md
Last active March 24, 2025 10:32
Masking IWebHostEnvironment in .NET Core 2.x for operation like 3.x

IWebHostingEnvironment is not available in .NET Core 2.x, but in 3.x it's the recommended way to access the host environment with threat of IHostingEnvironment being removed in the future. This can be a problem in libraries that need to run both on 2.x and 3.x.

The following tries to mask the differences in a multi-targeted .NET Core project by creating a custom IWebHostEnvironment implementations that picks up values from IHostingEnvironment in 2.x.

#if NETCORE2
using Microsoft.Extensions.FileProviders;

namespace Microsoft.AspNetCore.Hosting
{
@RickStrahl
RickStrahl / StatusDisplay.js
Last active November 27, 2023 13:18
Drop in JavaScript `status()` function that can be used from code to display debug status message on the bottom of the viewport for scenarios where console.log() is not available.
var statusTimeout = null;
var statusDefaultTimeout = 10000;
/*
* Generic Status Message for the bottom of the screen. Can be used to render debug output
* into the editor. Double click status bar to clear for appended output.
* status('Started');
* status('updated',true, 5000);
*/
function status(msg, append, timeout) {
@RickStrahl
RickStrahl / SimpleWebSocketServer-Usage.cs
Last active November 27, 2023 13:18
Simple Notification Only C# WebSocket Server
if (WebSocketServer == null)
{
WebSocketServer = new MarkdownMonster.WebSockets.WebSocketServer();
WebSocketServer.StartServer();
WebSocketServer.OnBinaryMessage = (bytes) =>
{
App.CommandArgs = new[] {"untitled.base64," + Convert.ToBase64String(bytes)};
mmApp.Model.Window.Dispatcher.InvokeAsync(() => mmApp.Model.Window.OpenFilesFromCommandLine());
};
}
@RickStrahl
RickStrahl / Bash-ShellExecute.cs
Last active May 19, 2020 21:48
Trying to run `bash -c ""` command with Process.Start/ShellExecute
// works in LinqPad
void Main()
{
ExecuteCommandLine("wt"); // works - this is resolved
ExecuteCommandLine("bash"); // The system cannot find the file specified
// actual command line I'm trying to fire
ExecuteCommandLine("bash -c \"cd /mnt/c/projects/Test/jekyll/help; bundle exec jekyll serve\"");
}
@RickStrahl
RickStrahl / ColorConsole.cs
Last active September 2, 2024 14:49
Color Console - a simple class to add color to .NET Console commands more easily.
using System;
using System.Text;
using System.Text.RegularExpressions;
namespace MainColorConsole
{
class Program
{
static void Main(string[] args)
{