fencedCodeBlock and
removed h1 when matches link title
This commit is contained in:
parent
ba5427b049
commit
71baf499c7
@ -286,10 +286,10 @@ internal static partial class HelperMarkdown
|
||||
segments = afterTrim.Split(": ");
|
||||
if (segments.Length != 2)
|
||||
{
|
||||
if (results.Last()[^1] == '[')
|
||||
if (results[^1][^1] == '[')
|
||||
{
|
||||
_ = stringBuilder.Clear();
|
||||
_ = stringBuilder.Append(results.Last());
|
||||
_ = stringBuilder.Append(results[^1]);
|
||||
results.RemoveAt(results.Count - 1);
|
||||
for (int j = i; j < parsedLines.Length; j++)
|
||||
{
|
||||
@ -325,7 +325,7 @@ internal static partial class HelperMarkdown
|
||||
string? result;
|
||||
List<string> results;
|
||||
Dictionary<string, JsonElement>? keyValuePairs;
|
||||
string jsonLinesLast = jsonLines.Last();
|
||||
string jsonLinesLast = jsonLines[^1];
|
||||
jsonLines.RemoveAt(jsonLines.Count - 1);
|
||||
jsonLines.Add(jsonLinesLast[..^1]);
|
||||
jsonLines.Insert(0, "{");
|
||||
@ -365,23 +365,23 @@ internal static partial class HelperMarkdown
|
||||
jsonLines.Clear();
|
||||
break;
|
||||
}
|
||||
segmentsLast = segments.Last().Trim();
|
||||
segmentsLast = segments[^1].Trim();
|
||||
segmentsFirst = segments[0].Trim();
|
||||
if (string.IsNullOrEmpty(segmentsLast))
|
||||
continue;
|
||||
if (segmentsFirst[0] == '"' && segmentsFirst.Last() == '"')
|
||||
if (segmentsFirst[0] == '"' && segmentsFirst[^1] == '"')
|
||||
jsonLines.Add($"{segmentsFirst}: ");
|
||||
else if (segmentsFirst[0] == '\'' && segmentsFirst.Last() == '\'')
|
||||
else if (segmentsFirst[0] == '\'' && segmentsFirst[^1] == '\'')
|
||||
jsonLines.Add($"\"{segmentsFirst[1..^1]}\": ");
|
||||
else
|
||||
jsonLines.Add($"\"{segmentsFirst}\": ");
|
||||
if (segmentsLast == "[]")
|
||||
jsonLines.RemoveAt(jsonLines.Count - 1);
|
||||
else if (segmentsLast.Length > 4 && segmentsLast[0] == '[' && segmentsLast.Last() == ']' && segmentsLast[1] == '"' && segmentsLast[^2] == '"')
|
||||
else if (segmentsLast.Length > 4 && segmentsLast[0] == '[' && segmentsLast[^1] == ']' && segmentsLast[1] == '"' && segmentsLast[^2] == '"')
|
||||
jsonLines.Add($"{segmentsLast},");
|
||||
else if (segmentsLast[0] == '"' && segmentsLast.Last() == '"')
|
||||
else if (segmentsLast[0] == '"' && segmentsLast[^1] == '"')
|
||||
jsonLines.Add($"{segmentsLast},");
|
||||
else if (segmentsLast[0] == '"' && segmentsLast.Last() == '"')
|
||||
else if (segmentsLast[0] == '"' && segmentsLast[^1] == '"')
|
||||
jsonLines.Add($"\"{segmentsLast[1..^1]}\"");
|
||||
else if (!segmentsLast.Contains('"') && !segmentsLast.Contains('\''))
|
||||
{
|
||||
@ -394,7 +394,7 @@ internal static partial class HelperMarkdown
|
||||
else
|
||||
{
|
||||
segmentsB = segmentsLast.Split('.');
|
||||
if (segmentsB.Length == 2 && segmentsB[0].Length < 7 && segmentsB.Last().Length < 7 && segmentsB[0].All(l => char.IsNumber(l)) && segmentsB.Last().All(l => char.IsNumber(l)))
|
||||
if (segmentsB.Length == 2 && segmentsB[0].Length < 7 && segmentsB[^1].Length < 7 && segmentsB[0].All(l => char.IsNumber(l)) && segmentsB[^1].All(l => char.IsNumber(l)))
|
||||
jsonLines.Add($"{segmentsLast},");
|
||||
else if (!segmentsLast.Contains('[') && !segmentsLast.Contains('{'))
|
||||
jsonLines.Add($"\"{segmentsLast}\",");
|
||||
@ -771,14 +771,14 @@ internal static partial class HelperMarkdown
|
||||
segmentsB = segmentsA[0].Split("[[");
|
||||
if (segmentsB.Length is not 2 or 3)
|
||||
continue;
|
||||
segmentsC = segmentsB.Last().Split('|');
|
||||
segmentsC = segmentsB[^1].Split('|');
|
||||
markdownFileH1AndRelativePath = GetRelativePath(keyValuePairs, markdownFile, segmentsC[0]);
|
||||
if (markdownFileH1AndRelativePath.MarkdownFile is null || markdownFileH1AndRelativePath.H1 is null || markdownFileH1AndRelativePath.RelativePath is null)
|
||||
{
|
||||
logger.LogInformation("Didn't find {line} in <{file}>", lines[i], markdownFile.FileNameWithoutExtension);
|
||||
continue;
|
||||
}
|
||||
line = $"{segmentsB[0]}[{markdownFileH1AndRelativePath.H1}]({markdownFileH1AndRelativePath.RelativePath.Replace('\\', '/')}){segmentsA.Last()}";
|
||||
line = $"{segmentsB[0]}[{markdownFileH1AndRelativePath.H1}]({markdownFileH1AndRelativePath.RelativePath.Replace('\\', '/')}){segmentsA[^1]}";
|
||||
if (lines[i] == line)
|
||||
continue;
|
||||
lines[i] = line;
|
||||
@ -872,7 +872,7 @@ internal static partial class HelperMarkdown
|
||||
segmentsA = lines[i].Split("](");
|
||||
if (segmentsA.Length != 2)
|
||||
continue;
|
||||
segmentsALast = segmentsA.Last();
|
||||
segmentsALast = segmentsA[^1];
|
||||
if (appSettings.ExcludeSchemes.Any(l => segmentsALast.StartsWith(l)))
|
||||
continue;
|
||||
segmentsB = segmentsALast.Split(")");
|
||||
@ -893,7 +893,7 @@ internal static partial class HelperMarkdown
|
||||
logger.LogInformation("Didn't find {line} in <{file}>", lines[i], markdownFile.FileNameWithoutExtension);
|
||||
continue;
|
||||
}
|
||||
line = $"{string.Join('[', segmentsC, 0, segmentsC.Length - 1)}[{markdownFileH1AndRelativePath.H1}]({markdownFileH1AndRelativePath.RelativePath.Replace('\\', '/')}){segmentsB.Last()}";
|
||||
line = $"{string.Join('[', segmentsC, 0, segmentsC.Length - 1)}[{markdownFileH1AndRelativePath.H1}]({markdownFileH1AndRelativePath.RelativePath.Replace('\\', '/')}){segmentsB[^1]}";
|
||||
if (lines[i] == line)
|
||||
continue;
|
||||
if (fileName.Contains(' ') || fileName.Contains("%20"))
|
||||
@ -934,7 +934,7 @@ internal static partial class HelperMarkdown
|
||||
return result;
|
||||
}
|
||||
|
||||
private static void SetRecursiveLines(AppSettings appSettings, ILogger<Worker> logger, ReadOnlyDictionary<string, List<MarkdownFileAndLines>> keyValuePairs, MarkdownFile markdownFile, string[] lines, List<char> indentations, List<string> recursiveLines)
|
||||
private static void SetRecursiveLines(AppSettings appSettings, ILogger<Worker> logger, ReadOnlyDictionary<string, List<MarkdownFileAndLines>> keyValuePairs, string linkTitle, MarkdownFile markdownFile, string[] lines, List<char> indentations, List<string> recursiveLines)
|
||||
{
|
||||
if (recursiveLines is null)
|
||||
throw new Exception();
|
||||
@ -942,7 +942,7 @@ internal static partial class HelperMarkdown
|
||||
string[] segmentsA;
|
||||
string[] segmentsB;
|
||||
string segmentsALast;
|
||||
string segmentsBFirst;
|
||||
bool fencedCodeBlock = false;
|
||||
string indentation = new(indentations.ToArray());
|
||||
MarkdownFileH1AndRelativePath markdownFileH1AndRelativePath;
|
||||
for (int i = 0; i < lines.Length; i++)
|
||||
@ -954,22 +954,27 @@ internal static partial class HelperMarkdown
|
||||
}
|
||||
if (lines[i].Length < 1)
|
||||
continue;
|
||||
if (lines[i].Length > 4 && lines[i][..3] == "```")
|
||||
fencedCodeBlock = !fencedCodeBlock;
|
||||
if (fencedCodeBlock)
|
||||
continue;
|
||||
if (lines[i][0] == '#')
|
||||
{
|
||||
if (lines[i] == $"# {linkTitle}")
|
||||
continue;
|
||||
recursiveLines.Add($"{indentation}{lines[i]}");
|
||||
continue;
|
||||
}
|
||||
segmentsA = lines[i].Split("](");
|
||||
if (segmentsA.Length != 2)
|
||||
continue;
|
||||
segmentsALast = segmentsA.Last();
|
||||
segmentsALast = segmentsA[^1];
|
||||
if (appSettings.ExcludeSchemes.Any(l => segmentsALast.StartsWith(l)))
|
||||
continue;
|
||||
segmentsB = segmentsALast.Split(")");
|
||||
if (segmentsB.Length != 2)
|
||||
continue;
|
||||
segmentsBFirst = segmentsB[0];
|
||||
file = Path.GetFullPath(Path.Combine(markdownFile.Directory, segmentsBFirst));
|
||||
file = Path.GetFullPath(Path.Combine(markdownFile.Directory, segmentsB[0]));
|
||||
markdownFileH1AndRelativePath = GetRelativePath(keyValuePairs, markdownFile, file);
|
||||
if (markdownFileH1AndRelativePath.MarkdownFile is null || markdownFileH1AndRelativePath.H1 is null || markdownFileH1AndRelativePath.RelativePath is null)
|
||||
{
|
||||
@ -981,7 +986,7 @@ internal static partial class HelperMarkdown
|
||||
continue;
|
||||
indentations.Add('\t');
|
||||
recursiveLines.Add($"{indentation}{lines[i]}");
|
||||
SetRecursiveLines(appSettings, logger, keyValuePairs, markdownFileH1AndRelativePath.MarkdownFile, markdownFileH1AndRelativePath.Lines, indentations, recursiveLines);
|
||||
SetRecursiveLines(appSettings, logger, keyValuePairs, segmentsA[0].Split('[')[^1], markdownFileH1AndRelativePath.MarkdownFile, markdownFileH1AndRelativePath.Lines, indentations, recursiveLines);
|
||||
}
|
||||
if (indentations.Count > 0)
|
||||
indentations.RemoveAt(0);
|
||||
@ -1005,7 +1010,7 @@ internal static partial class HelperMarkdown
|
||||
recursiveLines = new();
|
||||
lines = relativeTo.Value.Lines;
|
||||
markdownFile = relativeTo.Value.MarkdownFile;
|
||||
SetRecursiveLines(appSettings, logger, keyValuePairs, markdownFile, lines, indentations, recursiveLines);
|
||||
SetRecursiveLines(appSettings, logger, keyValuePairs, markdownFile.FileNameWithoutExtension, markdownFile, lines, indentations, recursiveLines);
|
||||
results.Add(new(relativeTo.Value.MarkdownFile, recursiveLines.ToArray()));
|
||||
}
|
||||
return results;
|
||||
@ -1121,7 +1126,7 @@ internal static partial class HelperMarkdown
|
||||
segmentsA = lines[i].Split("](");
|
||||
if (segmentsA.Length != 2)
|
||||
continue;
|
||||
segmentsALast = segmentsA.Last();
|
||||
segmentsALast = segmentsA[^1];
|
||||
if (appSettings.ExcludeSchemes.Any(l => segmentsALast.StartsWith(l)))
|
||||
continue;
|
||||
segmentsB = segmentsALast.Split(")");
|
||||
@ -1133,7 +1138,7 @@ internal static partial class HelperMarkdown
|
||||
else
|
||||
file = Path.GetFullPath(Path.Combine(markdownFile.Directory, segmentsBFirst[..^3]));
|
||||
relativeFile = Path.GetRelativePath(record.Source, file).Replace('\\', '/');
|
||||
line = $"{segmentsA[0]}]({relativeFile}){segmentsB.Last()}";
|
||||
line = $"{segmentsA[0]}]({relativeFile}){segmentsB[^1]}";
|
||||
if (lines[i] == line)
|
||||
throw new NotSupportedException($"Line {i} shouldn't match with {line}");
|
||||
lines[i] = line;
|
||||
|
Loading…
x
Reference in New Issue
Block a user