Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HowardvanRooijen/4d816612fddaabd88e706d0f6642c919 to your computer and use it in GitHub Desktop.
Save HowardvanRooijen/4d816612fddaabd88e706d0f6642c919 to your computer and use it in GitHub Desktop.
A .NET Interactive Notebook that converts Netlify URL Redirects to Azure Static Web Apps staticwebapp.config.json format
#!csharp
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
#!csharp
var lines = await File.ReadAllLinesAsync("./_redirects");
var rules = new List<(string, string, string)>();
foreach (string line in lines)
{
if (line.StartsWith('#') || string.IsNullOrWhiteSpace(line)) { continue; }
var chunks = line.Split(' ').Where<string>(x => !string.IsNullOrWhiteSpace(x));
var origin = chunks.ElementAt(0);
var redirect = chunks.ElementAt(1);
if (chunks.Count() == 3)
{
var http = chunks.ElementAt(2);
}
(string origin, string redirect, string http) row = (chunks.ElementAt(0), chunks.ElementAt(1), chunks.Count() == 3 ? chunks.ElementAt(2): string.Empty);
rules.Add(row);
}
var sb = new StringBuilder();
sb.AppendLine("{");
sb.AppendLine(" \"routes\": [");
foreach(var rule in rules)
{
sb.AppendLine(" {");
sb.AppendLine($" \"route\": \"{rule.Item1}\",");
sb.Append($" \"redirect\": \"{rule.Item2}\"");
if (!string.IsNullOrEmpty(rule.Item3))
{
sb.Append(",");
sb.Append(Environment.NewLine);
sb.AppendLine($" \"statusCode\": {rule.Item3}");
}
if (rule != rules.Last())
{
sb.AppendLine(" },");
}
else{
sb.AppendLine(" }");
}
}
sb.AppendLine(" ]");
sb.AppendLine("}");
Console.Write(sb.ToString())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment