< Summary

Information
Class: RoleRadar.Domain.Notes.ParsedImportSection
Assembly: RoleRadar.Domain
File(s): /home/runner/work/roleradar/roleradar/src/RoleRadar.Domain/Notes/MarkdownImportParser.cs
Line coverage
90%
Covered lines: 9
Uncovered lines: 1
Coverable lines: 10
Total lines: 435
Line coverage: 90%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
get_SourceFileId()100%210%
get_HeadingPath()100%11100%
get_OccurrenceIndex()100%11100%
get_SectionOrderIndex()100%11100%
get_Title()100%11100%
get_Body()100%11100%
get_Provenance()100%11100%
get_BodyHash()100%11100%
get_ImportKey()100%11100%

File(s)

/home/runner/work/roleradar/roleradar/src/RoleRadar.Domain/Notes/MarkdownImportParser.cs

#LineLine coverage
 1using System.Text;
 2using System.Text.RegularExpressions;
 3using RoleRadar.Domain.Common;
 4
 5namespace RoleRadar.Domain.Notes;
 6
 16387public sealed record ParsedImportSection(
 08    string SourceFileId,
 8289    string HeadingPath,
 81010    int OccurrenceIndex,
 122611    int SectionOrderIndex,
 80612    string Title,
 126013    string Body,
 82014    ProvenanceMarker Provenance,
 20415    string BodyHash,
 405216    string ImportKey);
 17
 18public static partial class MarkdownImportParser
 19{
 20    private static readonly UTF8Encoding StrictUtf8 = new(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: t
 21
 22    [GeneratedRegex(@"\[(stated|researched|inferred)(?:\s+(\d{4}-\d{2}-\d{2})(?:\s+([^\]]+?))?)?\]", RegexOptions.Compil
 23    private static partial Regex ProvenanceTagRegex();
 24
 25    public static bool ValidateUtf8(ReadOnlySpan<byte> bytes)
 26    {
 27        try
 28        {
 29            _ = StrictUtf8.GetCharCount(bytes);
 30            return true;
 31        }
 32        catch (DecoderFallbackException)
 33        {
 34            return false;
 35        }
 36    }
 37
 38    public static IReadOnlyList<ParsedImportSection> ParseBytes(
 39        string sourceFileId,
 40        byte[] bytes,
 41        string defaultDate)
 42    {
 43        ArgumentNullException.ThrowIfNull(bytes);
 44
 45        if (!ValidateUtf8(bytes))
 46        {
 47            throw new DecoderFallbackException($"File '{sourceFileId}' contains invalid UTF-8 byte sequences.");
 48        }
 49
 50        var markdown = StrictUtf8.GetString(bytes);
 51        return Parse(sourceFileId, markdown, defaultDate);
 52    }
 53
 54    public static IReadOnlyList<ParsedImportSection> Parse(
 55        string sourceFileId,
 56        string markdown,
 57        string defaultDate)
 58    {
 59        ArgumentNullException.ThrowIfNull(sourceFileId);
 60        ArgumentNullException.ThrowIfNull(markdown);
 61
 62        if (string.IsNullOrWhiteSpace(defaultDate) || !ProvenanceMarker.IsValidIsoDate(defaultDate))
 63        {
 64            throw new ArgumentException($"Default date '{defaultDate}' must be a valid ISO-8601 date in 'YYYY-MM-DD' for
 65        }
 66
 67        var normalizedSourceId = sourceFileId.Trim();
 68        var lines = SplitLines(markdown);
 69
 70        var headingLocations = FindSplitHeadings(markdown, lines);
 71
 72        var sections = new List<ParsedImportSection>();
 73        var headingPathOccurrences = new Dictionary<string, int>(StringComparer.Ordinal);
 74        var bareHeadingCounter = 0;
 75        var sectionOrderIndex = 0;
 76
 77        string? currentH1Path = null;
 78
 79        if (headingLocations.Count == 0)
 80        {
 81            if (markdown.Length > 0)
 82            {
 83                var prov = ExtractSectionProvenance(markdown, lines, 0, lines.Count, defaultDate);
 84                var hash = Sha256Hasher.ComputeLowerHex(markdown);
 85                const string path = "$preamble";
 86                var importKey = $"{normalizedSourceId}#{path}#0";
 87
 88                sections.Add(new ParsedImportSection(
 89                    SourceFileId: normalizedSourceId,
 90                    HeadingPath: path,
 91                    OccurrenceIndex: 0,
 92                    SectionOrderIndex: 0,
 93                    Title: "Preamble",
 94                    Body: markdown,
 95                    Provenance: prov,
 96                    BodyHash: hash,
 97                    ImportKey: importKey));
 98            }
 99
 100            return sections;
 101        }
 102
 103        // Check if there is preamble text before the first # or ## heading
 104        var firstHeadingStart = headingLocations[0].StartCharIndex;
 105        if (firstHeadingStart > 0)
 106        {
 107            var preambleBody = markdown[..firstHeadingStart];
 108            var firstHeadingLineIndex = headingLocations[0].LineIndex;
 109            var prov = ExtractSectionProvenance(markdown, lines, 0, firstHeadingLineIndex, defaultDate);
 110            var hash = Sha256Hasher.ComputeLowerHex(preambleBody);
 111            const string path = "$preamble";
 112            var importKey = $"{normalizedSourceId}#{path}#0";
 113
 114            sections.Add(new ParsedImportSection(
 115                SourceFileId: normalizedSourceId,
 116                HeadingPath: path,
 117                OccurrenceIndex: 0,
 118                SectionOrderIndex: sectionOrderIndex++,
 119                Title: "Preamble",
 120                Body: preambleBody,
 121                Provenance: prov,
 122                BodyHash: hash,
 123                ImportKey: importKey));
 124        }
 125
 126        for (var i = 0; i < headingLocations.Count; i++)
 127        {
 128            var current = headingLocations[i];
 129            var nextStart = (i + 1 < headingLocations.Count)
 130                ? headingLocations[i + 1].StartCharIndex
 131                : markdown.Length;
 132
 133            var sectionBody = markdown[current.StartCharIndex..nextStart];
 134            var nextLineIndex = (i + 1 < headingLocations.Count)
 135                ? headingLocations[i + 1].LineIndex
 136                : lines.Count;
 137
 138            var prov = ExtractSectionProvenance(markdown, lines, current.LineIndex, nextLineIndex, defaultDate);
 139
 140            string title;
 141            if (string.IsNullOrWhiteSpace(current.RawTitle))
 142            {
 143                bareHeadingCounter++;
 144                title = $"(Untitled Heading #{bareHeadingCounter})";
 145            }
 146            else
 147            {
 148                title = current.RawTitle;
 149            }
 150
 151            var markerTitle = string.IsNullOrWhiteSpace(current.RawTitle)
 152                ? new string('#', current.Level)
 153                : $"{new string('#', current.Level)} {current.RawTitle}";
 154
 155            string headingPath;
 156            if (current.Level == 1)
 157            {
 158                headingPath = markerTitle;
 159                currentH1Path = headingPath;
 160            }
 161            else // Level == 2
 162            {
 163                headingPath = currentH1Path is not null
 164                    ? $"{currentH1Path} / {markerTitle}"
 165                    : markerTitle;
 166            }
 167
 168            if (!headingPathOccurrences.TryGetValue(headingPath, out var count))
 169            {
 170                count = 0;
 171            }
 172
 173            headingPathOccurrences[headingPath] = count + 1;
 174            var occurrenceIndex = count;
 175
 176            var hash = Sha256Hasher.ComputeLowerHex(sectionBody);
 177            var importKey = $"{normalizedSourceId}#{headingPath}#{occurrenceIndex}";
 178
 179            sections.Add(new ParsedImportSection(
 180                SourceFileId: normalizedSourceId,
 181                HeadingPath: headingPath,
 182                OccurrenceIndex: occurrenceIndex,
 183                SectionOrderIndex: sectionOrderIndex++,
 184                Title: title,
 185                Body: sectionBody,
 186                Provenance: prov,
 187                BodyHash: hash,
 188                ImportKey: importKey));
 189        }
 190
 191        return sections;
 192    }
 193
 194    private sealed record LineInfo(int LineIndex, int StartCharIndex, int EndCharIndex, string Text, bool InFrontMatter)
 195
 196    private sealed record HeadingLocation(int LineIndex, int Level, string RawTitle, int StartCharIndex);
 197
 198    private static List<LineInfo> SplitLines(string markdown)
 199    {
 200        var lines = new List<LineInfo>();
 201        var pos = 0;
 202        var len = markdown.Length;
 203        var lineIndex = 0;
 204        var inFrontMatter = false;
 205
 206        while (pos < len)
 207        {
 208            var lineStart = pos;
 209            while (pos < len && markdown[pos] != '\r' && markdown[pos] != '\n')
 210            {
 211                pos++;
 212            }
 213
 214            var contentEnd = pos;
 215            if (pos < len)
 216            {
 217                if (markdown[pos] == '\r' && pos + 1 < len && markdown[pos + 1] == '\n')
 218                {
 219                    pos += 2;
 220                }
 221                else
 222                {
 223                    pos += 1;
 224                }
 225            }
 226
 227            var lineText = markdown[lineStart..contentEnd];
 228
 229            if (lineIndex == 0 && lineText.Trim() == "---")
 230            {
 231                inFrontMatter = true;
 232                lines.Add(new LineInfo(lineIndex++, lineStart, pos, lineText, InFrontMatter: true));
 233                continue;
 234            }
 235
 236            if (inFrontMatter)
 237            {
 238                lines.Add(new LineInfo(lineIndex++, lineStart, pos, lineText, InFrontMatter: true));
 239                if (lineText.Trim() == "---")
 240                {
 241                    inFrontMatter = false;
 242                }
 243                continue;
 244            }
 245
 246            lines.Add(new LineInfo(lineIndex++, lineStart, pos, lineText, InFrontMatter: false));
 247        }
 248
 249        return lines;
 250    }
 251
 252    private static List<HeadingLocation> FindSplitHeadings(string markdown, List<LineInfo> lines)
 253    {
 254        var headings = new List<HeadingLocation>();
 255        var inFence = false;
 256        var fenceChar = '\0';
 257        var fenceLen = 0;
 258
 259        foreach (var line in lines)
 260        {
 261            if (line.InFrontMatter)
 262            {
 263                continue;
 264            }
 265
 266            var span = line.Text.AsSpan();
 267            if (MarkdownSectionParser.TryUpdateFenceState(span, ref inFence, ref fenceChar, ref fenceLen))
 268            {
 269                continue;
 270            }
 271
 272            if (inFence)
 273            {
 274                continue;
 275            }
 276
 277            // Indented code block: 4 or more leading spaces or tab
 278            if (IsIndentedCodeLine(span))
 279            {
 280                continue;
 281            }
 282
 283            if (TryParseAtxHeading(span, out var level, out var title))
 284            {
 285                // We split ONLY on level 1 (#) and level 2 (##)
 286                if (level is 1 or 2)
 287                {
 288                    headings.Add(new HeadingLocation(line.LineIndex, level, title, line.StartCharIndex));
 289                }
 290            }
 291        }
 292
 293        return headings;
 294    }
 295
 296    private static bool IsIndentedCodeLine(ReadOnlySpan<char> span)
 297    {
 298        if (span.IsEmpty)
 299        {
 300            return false;
 301        }
 302
 303        if (span[0] == '\t')
 304        {
 305            return true;
 306        }
 307
 308        var spaces = 0;
 309        while (spaces < span.Length && span[spaces] == ' ')
 310        {
 311            spaces++;
 312        }
 313
 314        return spaces >= 4;
 315    }
 316
 317    private static bool TryParseAtxHeading(ReadOnlySpan<char> span, out int level, out string title)
 318    {
 319        level = 0;
 320        title = string.Empty;
 321
 322        var spaces = 0;
 323        while (spaces < span.Length && spaces < 3 && span[spaces] == ' ')
 324        {
 325            spaces++;
 326        }
 327
 328        var trimmedLeading = span[spaces..];
 329        if (trimmedLeading.IsEmpty || trimmedLeading[0] != '#')
 330        {
 331            return false;
 332        }
 333
 334        var hashes = 0;
 335        while (hashes < trimmedLeading.Length && trimmedLeading[hashes] == '#')
 336        {
 337            hashes++;
 338        }
 339
 340        if (hashes is < 1 or > 6)
 341        {
 342            return false;
 343        }
 344
 345        var rest = trimmedLeading[hashes..];
 346        if (!rest.IsEmpty && rest[0] != ' ' && rest[0] != '\t')
 347        {
 348            return false;
 349        }
 350
 351        level = hashes;
 352
 353        var rawTitle = rest.Trim();
 354        // Check for optional closing sequence of # preceded by space/tab
 355        var titleStr = StripTrailingClosingHashes(rawTitle);
 356        title = titleStr;
 357        return true;
 358    }
 359
 360    private static string StripTrailingClosingHashes(ReadOnlySpan<char> span)
 361    {
 362        if (span.IsEmpty)
 363        {
 364            return string.Empty;
 365        }
 366
 367        var end = span.Length;
 368        while (end > 0 && (span[end - 1] == ' ' || span[end - 1] == '\t'))
 369        {
 370            end--;
 371        }
 372
 373        var trailingHashes = 0;
 374        while (end - trailingHashes > 0 && span[end - trailingHashes - 1] == '#')
 375        {
 376            trailingHashes++;
 377        }
 378
 379        if (trailingHashes > 0 && end - trailingHashes > 0 && (span[end - trailingHashes - 1] == ' ' || span[end - trail
 380        {
 381            var beforeHashes = span[..(end - trailingHashes)].Trim();
 382            return beforeHashes.ToString();
 383        }
 384
 385        return span.Trim().ToString();
 386    }
 387
 388    private static ProvenanceMarker ExtractSectionProvenance(
 389        string markdown,
 390        List<LineInfo> lines,
 391        int startLineIndex,
 392        int endLineIndex,
 393        string defaultDate)
 394    {
 395        var inFence = false;
 396        var fenceChar = '\0';
 397        var fenceLen = 0;
 398
 399        for (var i = startLineIndex; i < endLineIndex && i < lines.Count; i++)
 400        {
 401            var line = lines[i];
 402            if (line.InFrontMatter)
 403            {
 404                continue;
 405            }
 406
 407            var span = line.Text.AsSpan();
 408            if (MarkdownSectionParser.TryUpdateFenceState(span, ref inFence, ref fenceChar, ref fenceLen))
 409            {
 410                continue;
 411            }
 412
 413            if (inFence)
 414            {
 415                continue;
 416            }
 417
 418            var match = ProvenanceTagRegex().Match(line.Text);
 419            if (match.Success)
 420            {
 421                var kind = ProvenanceKindExtensions.FromWireString(match.Groups[1].Value);
 422                var date = match.Groups[2].Success && !string.IsNullOrWhiteSpace(match.Groups[2].Value)
 423                    ? match.Groups[2].Value.Trim()
 424                    : defaultDate;
 425                var qualifier = match.Groups[3].Success && !string.IsNullOrWhiteSpace(match.Groups[3].Value)
 426                    ? match.Groups[3].Value.Trim()
 427                    : null;
 428
 429                return new ProvenanceMarker(kind, date, qualifier);
 430            }
 431        }
 432
 433        return new ProvenanceMarker(ProvenanceKind.Inferred, defaultDate);
 434    }
 435}