Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert markdown to normal xml #158

Open
carlossanlop opened this issue Feb 3, 2023 · 1 comment
Open

Convert markdown to normal xml #158

carlossanlop opened this issue Feb 3, 2023 · 1 comment

Comments

@carlossanlop
Copy link
Member

We don't want to backport markdown to triple slash. Convert it back to normal xml.

@carlossanlop
Copy link
Member Author

We have functions that do this task:

private static XmlNodeSyntax GetTextAsFormatCData(string text, SyntaxTriviaList leadingWhitespace)
{
XmlTextSyntax remarks = GetTextAsCommentedTokens(text, leadingWhitespace, wrapWithNewLines: true);
XmlNameSyntax formatName = SyntaxFactory.XmlName("format");
XmlAttributeSyntax formatAttribute = SyntaxFactory.XmlTextAttribute("type", "text/markdown");
var formatAttributes = new SyntaxList<XmlAttributeSyntax>(formatAttribute);
var formatStart = SyntaxFactory.XmlElementStartTag(formatName, formatAttributes);
var formatEnd = SyntaxFactory.XmlElementEndTag(formatName);
XmlCDataSectionSyntax cdata = SyntaxFactory.XmlCDataSection(remarks.TextTokens);
var cdataList = new SyntaxList<XmlNodeSyntax>(cdata);
XmlElementSyntax contents = SyntaxFactory.XmlElement(formatStart, cdataList, formatEnd);
return contents;
}
private static string RemoveUnnecessaryMarkdown(string text)
{
text = Regex.Replace(text, @"<!\[CDATA\[(\r?\n)*[\t ]*", "");
text = Regex.Replace(text, @"\]\]>", "");
text = Regex.Replace(text, @"##[ ]?Remarks(\r?\n)*[\t ]*", "");
return text;
}
private static string ReplaceMarkdownWithXmlElements(string text, List<DocsParam> docsParams, List<DocsTypeParam> docsTypeParams)
{
text = CleanXrefs(text);
// commonly used url entities
text = Regex.Replace(text, @"%23", "#");
text = Regex.Replace(text, @"%28", "(");
text = Regex.Replace(text, @"%29", ")");
text = Regex.Replace(text, @"%2C", ",");
// hyperlinks
text = Regex.Replace(text, RegexMarkdownLinkPattern, RegexHtmlLinkReplacement);
// bold
text = Regex.Replace(text, RegexMarkdownBoldPattern, RegexXmlBoldReplacement);
// code snippet
text = Regex.Replace(text, RegexMarkdownCodeStartPattern, RegexXmlCodeStartReplacement);
text = Regex.Replace(text, RegexMarkdownCodeEndPattern, RegexXmlCodeEndReplacement);
// langwords|parameters|typeparams
MatchCollection collection = Regex.Matches(text, @"(?<backtickedParam>`(?<paramName>[a-zA-Z0-9_]+)`)");
foreach (Match match in collection)
{
string backtickedParam = match.Groups["backtickedParam"].Value;
string paramName = match.Groups["paramName"].Value;
if (ReservedKeywords.Any(x => x == paramName))
{
text = Regex.Replace(text, $"{backtickedParam}", $"<see langword=\"{paramName}\" />");
}
else if (docsParams.Any(x => x.Name == paramName))
{
text = Regex.Replace(text, $"{backtickedParam}", $"<paramref name=\"{paramName}\" />");
}
else if (docsTypeParams.Any(x => x.Name == paramName))
{
text = Regex.Replace(text, $"{backtickedParam}", $"<typeparamref name=\"{paramName}\" />");
}
}
return text;
}

but still need to confirm with detailed unit tests that it works as expected, especially for items that are not markdown standard like [!NOTE], [!IMPORTANT] and similar.

cc @smasher164

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant