using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Semmle.Autobuild.Shared
{
public static class MarkdownUtil
{
///
/// Formats items as markdown inline code.
///
/// A function which formats items as markdown inline code.
public static readonly Func CodeFormatter = item => $"`{item}`";
///
/// Formats the string as a markdown link.
///
/// The URL for the link.
/// The text that is displayed.
/// A string containing a markdown-formatted link.
public static string ToMarkdownLink(this string link, string title) => $"[{title}]({link})";
///
/// Renders as a markdown list of the project paths.
///
///
/// The list of projects whose paths should be rendered as a markdown list.
///
/// The maximum number of items to include in the list.
/// Returns the markdown list as a string.
public static string ToMarkdownList(this IEnumerable projects, int? limit = null)
{
return projects.ToMarkdownList(p => $"`{p.FullPath}`", limit);
}
///
/// Renders as a markdown list.
///
/// The item type.
/// The list that should be formatted as a markdown list.
/// A function which converts individual items into a string representation.
/// The maximum number of items to include in the list.
/// Returns the markdown list as a string.
public static string ToMarkdownList(this IEnumerable items, Func formatter, int? limit = null)
{
var sb = new StringBuilder();
// if there is a limit, take at most that many items from the start of the list
var list = limit is not null ? items.Take(limit.Value) : items;
sb.Append(string.Join('\n', list.Select(item => $"- {formatter(item)}")));
// if there were more items than allowed in the list, add an extra item indicating
// how many more items there were
var length = items.Count();
if (limit is not null && length > limit)
{
sb.Append($"\n- and {length - limit} more. View the CodeQL logs for a full list.");
}
return sb.ToString();
}
}
}