/// <summary> /// A helper function that checks the validity of a single local link. /// Side effects: Prints to the console. /// </summary> /// <param name="markdownFilePath">The fully qualified path of the Markdown file to check</param> /// <param name="markdownFileContents">An object representing the Markdown file to check.</param> /// <param name="localLink">The object representing the local link to check.</param> /// <param name="markdownFiles"> /// The corpus of Markdown files undergoing linting. Maps filename to Markdown file object /// representation. /// </param> /// <returns>A bool indicating the success of the check.</returns> internal static bool CheckLocalLink(string markdownFilePath, SimplifiedMarkdownDoc markdownFileContents, LocalLink localLink, Dictionary <string, SimplifiedMarkdownDoc> markdownFiles) { // This link is to something in the same file. if (localLink.FilePath == null) { // Must have a markdown heading! E.g. - "#documentation" if (!localLink.Heading.HasValue) { LogInvalidLink(markdownFilePath, localLink); return(false); } // Check that the heading is valid. if (!markdownFileContents.Headings.Contains(localLink.Heading.Value)) { LogInvalidLink(markdownFilePath, localLink); return(false); } } // This link is to another file. else { var fullyQualifiedFilePath = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(markdownFilePath), localLink.FilePath)); // The other file is a markdown file and has a heading. if (localLink.Heading.HasValue) { // Ensure that the other markdown files exists. SimplifiedMarkdownDoc otherMarkdownDoc; if (!markdownFiles.TryGetValue(fullyQualifiedFilePath, out otherMarkdownDoc)) { LogInvalidLink(markdownFilePath, localLink); return(false); } // Check that the heading is valid. if (!otherMarkdownDoc.Headings.Contains(localLink.Heading.Value)) { LogInvalidLink(markdownFilePath, localLink); return(false); } } else { // May be a markdown file (without a heading) or non-markdown file or a directory. E.g. - "../README.md" or "../../spatialos.json". if (!markdownFiles.ContainsKey(fullyQualifiedFilePath) && !File.Exists(fullyQualifiedFilePath) && !Directory.Exists(fullyQualifiedFilePath)) { LogInvalidLink(markdownFilePath, localLink); return(false); } } } return(true); }
/// <summary> /// A helper method that checks all the links in a markdown file and returns a success/fail. /// Side effects: Prints to the console. /// </summary> /// <param name="markdownFilePath">The fully qualified path of the Markdown file to check</param> /// <param name="markdownFileContents">An object representing the Markdown file to check.</param> /// <param name="markdownFiles"> /// The corpus of Markdown files undergoing linting. Maps filename to Markdown file object /// representation. /// </param> /// <param name="dotenv">A dictionary representing the dotenv contents.</param> /// <returns>A bool indicating the success of the check.</returns> internal static bool CheckMarkdownFile(string markdownFilePath, SimplifiedMarkdownDoc markdownFileContents, Dictionary <string, SimplifiedMarkdownDoc> markdownFiles, Dictionary <string, string> dotenv) { var allLinksValid = true; foreach (var localLink in markdownFileContents.Links.OfType <LocalLink>()) { allLinksValid &= CheckLocalLink(markdownFilePath, markdownFileContents, localLink, markdownFiles); } foreach (var remoteLink in markdownFileContents.Links.OfType <RemoteLink>()) { allLinksValid &= CheckRemoteLink(markdownFilePath, remoteLink, dotenv); } return(allLinksValid); }