Beispiel #1
0
        protected override void Write(TsxRenderer renderer, CodeBlock obj)
        {
            renderer.EnsureLine();

            var fencedCodeBlock = obj as FencedCodeBlock;

            if (renderer.EnableHtmlForBlock)
            {
                renderer.Write("<mdx.CodeBlock");
                if (fencedCodeBlock?.Info != null)
                {
                    renderer.Write(" info=").WriteJsString(fencedCodeBlock.Info);
                }
                if (fencedCodeBlock?.Arguments != null)
                {
                    renderer.Write(" args=").WriteJsString(fencedCodeBlock.Arguments);
                }
                renderer.WriteProps(obj).Write('>');
            }

            renderer.WriteLeafRawLines(obj, false, true);

            if (renderer.EnableHtmlForBlock)
            {
                renderer.Write("</mdx.CodeBlock>").WriteLine();
            }

            renderer.EnsureLine();
        }
 protected override void Write(TsxRenderer renderer, FigureCaption obj)
 {
     renderer.EnsureLine();
     renderer.Write("<mdx.Figcaption").WriteProps(obj).Write('>');
     renderer.WriteLeafInline(obj);
     renderer.Write("</mdx.Figcaption>").WriteLine();
 }
Beispiel #3
0
        protected override void Write(TsxRenderer renderer, EmphasisInline obj)
        {
            string?tag = null;

            if (renderer.EnableHtmlForInline)
            {
                var c = obj.DelimiterChar;
                tag = c switch
                {
                    '~' => obj.DelimiterCount == 2 ? "Del" : "Sub",
                    '^' => "Sup",
                    '+' => "Ins",
                    '=' => "Mark",
                    '"' => "Cite",
                    '*' or '_' => obj.DelimiterCount == 2 ? "Strong" : "Em",
                    _ => throw new NotSupportedException($"Delimiter: {c}")
                };
                renderer.Write("<mdx.").Write(tag).WriteProps(obj).Write('>');
            }
            renderer.WriteChildren(obj);
            if (renderer.EnableHtmlForInline)
            {
                renderer.Write("</mdx.").Write(tag).Write('>');
            }
        }
    }
Beispiel #4
0
        protected override void Write(TsxRenderer renderer, FooterBlock footer)
        {
            renderer.EnsureLine();
            renderer.Write("<mdx.Footer").WriteProps(footer).Write(">");
            var implicitParagraph = renderer.ImplicitParagraph;

            renderer.ImplicitParagraph = true;
            renderer.WriteChildren(footer);
            renderer.ImplicitParagraph = implicitParagraph;
            renderer.Write("</mdx.Footer>").WriteLine();
        }
Beispiel #5
0
        protected override void Write(TsxRenderer renderer, ListBlock listBlock)
        {
            renderer.EnsureLine();
            if (renderer.EnableHtmlForBlock)
            {
                if (listBlock.IsOrdered)
                {
                    renderer.Write("<mdx.Ol");
                    if (listBlock.BulletType != '1')
                    {
                        renderer.Write(" type=\"").Write(listBlock.BulletType).Write('"');
                    }

                    if (listBlock.OrderedStart is { } and not "1")
                    {
                        renderer.Write(" start={").Write(listBlock.OrderedStart).Write('}');
                    }
                    renderer.WriteProps(listBlock);
                    renderer.Write('>').WriteLine().Indent();
                }
                else
                {
                    renderer.Write("<mdx.Ul");
                    renderer.WriteProps(listBlock);
                    renderer.Write('>').WriteLine().Indent();
                }
            }

            foreach (var item in listBlock)
            {
                var listItem         = (ListItemBlock)item;
                var previousImplicit = renderer.ImplicitParagraph;
                renderer.ImplicitParagraph = !listBlock.IsLoose;

                renderer.EnsureLine();
                if (renderer.EnableHtmlForBlock)
                {
                    renderer.Write("<mdx.Li").WriteProps(listItem).Write('>');
                }

                renderer.WriteChildren(listItem);

                if (renderer.EnableHtmlForBlock)
                {
                    renderer.Write("</mdx.Li>").WriteLine();
                }

                renderer.EnsureLine();
                renderer.ImplicitParagraph = previousImplicit;
            }

            if (renderer.EnableHtmlForBlock)
            {
                renderer.Dedent().Write(listBlock.IsOrdered ? "</mdx.Ol>" : "</mdx.Ul>").WriteLine();
            }

            renderer.EnsureLine();
        }
Beispiel #6
0
        protected override void Write(TsxRenderer renderer, AbbreviationInline obj)
        {
            // <abbr title="Hyper Text Markup Language">HTML</abbr>
            var abbr = obj.Abbreviation;

            if (renderer.EnableHtmlForInline)
            {
                renderer.Write("<mdx.Abbr").WriteProps(obj).Write(" title=").WriteEscape(ref abbr.Text);
                renderer.Write(">");
            }
            renderer.Write(abbr.Label);
            if (renderer.EnableHtmlForInline)
            {
                renderer.Write("</mdx.Abbr>");
            }
        }
Beispiel #7
0
 protected override void Write(TsxRenderer renderer, ThematicBreakBlock obj)
 {
     if (renderer.EnableHtmlForBlock)
     {
         renderer.Write("<mdx.Hr").WriteProps(obj).Write(" />").WriteLine();
     }
 }
Beispiel #8
0
        protected override void Write(TsxRenderer renderer, HeadingBlock obj)
        {
            if (renderer.EnableHtmlForBlock)
            {
                renderer.Write("<mdx.H level={" + obj.Level + "}").WriteProps(obj).Write('>');
            }

            renderer.WriteLeafInline(obj);

            if (renderer.EnableHtmlForBlock)
            {
                renderer.Write("</mdx.H>").WriteLine();
            }

            renderer.EnsureLine();
        }
Beispiel #9
0
 protected override void Write(TsxRenderer renderer, HtmlInline obj)
 {
     if (renderer.EnableHtmlForInline)
     {
         renderer.Write(obj.Tag);
     }
 }
Beispiel #10
0
        protected override void Write(TsxRenderer renderer, AutolinkInline obj)
        {
            if (renderer.EnableHtmlForInline)
            {
                renderer.Write("<mdx.A href=");
                renderer.WriteEscapeUrl(obj.IsEmail?  "mailto:" + obj.Url :obj.Url, false);
                renderer.WriteProps(obj);
                renderer.Write('>');
            }

            renderer.WriteEscape(obj.Url);

            if (renderer.EnableHtmlForInline)
            {
                renderer.Write("</mdx.A>");
            }
        }
Beispiel #11
0
        protected override void Write(TsxRenderer renderer, QuoteBlock obj)
        {
            renderer.EnsureLine();
            if (renderer.EnableHtmlForBlock)
            {
                renderer.Write("<mdx.BlockQuote").WriteProps(obj).Write(">").WriteLine();
            }
            var savedImplicitParagraph = renderer.ImplicitParagraph;

            renderer.ImplicitParagraph = false;
            renderer.WriteChildren(obj);
            renderer.ImplicitParagraph = savedImplicitParagraph;
            if (renderer.EnableHtmlForBlock)
            {
                renderer.Write("</mdx.BlockQuote>").WriteLine();
            }
            renderer.EnsureLine();
        }
Beispiel #12
0
 protected override void Write(TsxRenderer renderer, CodeInline obj)
 {
     if (renderer.EnableHtmlForInline)
     {
         renderer.Write("<mdx.Code").WriteProps(obj).Write('>');
     }
     if (renderer.EnableHtmlEscape)
     {
         renderer.WriteEscape(obj.Content);
     }
     else
     {
         renderer.Write(obj.Content);
     }
     if (renderer.EnableHtmlForInline)
     {
         renderer.Write("</mdx.Code>");
     }
 }
Beispiel #13
0
 protected override void Write(TsxRenderer renderer, LiteralInline obj)
 {
     if (renderer.EnableHtmlEscape)
     {
         renderer.WriteEscape(ref obj.Content);
     }
     else
     {
         renderer.Write(ref obj.Content);
     }
 }
Beispiel #14
0
        protected override void Write(TsxRenderer renderer, LinkInline link)
        {
            if (renderer.EnableHtmlForInline)
            {
                renderer.Write(link.IsImage ? "<mdx.Img src=" : "<mdx.A href=");
                renderer.WriteEscapeUrl(link.Url, link.IsImage);
                renderer.WriteProps(link);
            }
            if (link.IsImage)
            {
                if (renderer.EnableHtmlForInline)
                {
                    renderer.Write(" alt=\"");
                }
                var wasEnableHtmlForInline = renderer.EnableHtmlForInline;
                renderer.EnableHtmlForInline = false;
                renderer.WriteChildren(link);
                renderer.EnableHtmlForInline = wasEnableHtmlForInline;
                if (renderer.EnableHtmlForInline)
                {
                    renderer.Write('"');
                }
            }

            if (renderer.EnableHtmlForInline && !string.IsNullOrEmpty(link.Title))
            {
                renderer.Write(" title=\"");
                renderer.WriteEscape(link.Title);
                renderer.Write('"');
            }

            if (link.IsImage)
            {
                if (renderer.EnableHtmlForInline)
                {
                    renderer.Write(" />");
                }
            }
            else
            {
                if (renderer.EnableHtmlForInline)
                {
                    renderer.Write('>');
                }
                renderer.WriteChildren(link);
                if (renderer.EnableHtmlForInline)
                {
                    renderer.Write("</mdx.A>");
                }
            }
        }
 protected override void Write(TsxRenderer renderer, HtmlEntityInline obj)
 {
     if (renderer.EnableHtmlEscape)
     {
         var slice = obj.Transcoded;
         renderer.WriteEscape(ref slice);
     }
     else
     {
         renderer.Write(obj.Transcoded);
     }
 }
Beispiel #16
0
        protected override void Write(TsxRenderer renderer, LineBreakInline obj)
        {
            if (renderer.EnableHtmlForInline)
            {
                if (obj.IsHard)
                {
                    renderer.Write("{mdx.Br()}");
                }
            }

            renderer.EnsureLine();
        }
Beispiel #17
0
        public (string content, Dictionary <object, object> metadata) Render()
        {
            var renderer = new TsxRenderer();

            renderer.Write("import * as b from \"bobril\";").WriteLine();
            renderer.Write("import * as mdx from \"@bobril/mdx\";").WriteLine();

            foreach (var importBlock in _document !.Descendants <ImportBlock>())
            {
                renderer.Write(importBlock.Lines.ToSlice()).WriteLine();
            }

            var frontMatterBlock = _document !
                                   .Descendants <YamlFrontMatterBlock>()
                                   .FirstOrDefault();
            Dictionary <object, object>?metadata = null;

            if (frontMatterBlock != null)
            {
                var yaml         = frontMatterBlock.Lines.ToString();
                var deserializer = new DeserializerBuilder().Build();
                var yamlObject   = deserializer.Deserialize(new StringReader(yaml));
                metadata = yamlObject as Dictionary <object, object>;
                frontMatterBlock.Parent !.Remove(frontMatterBlock);
            }

            metadata ??= new();
            renderer.Write("export const metadata = ").Write(TypeConverter.ToAst(metadata)).Write(";").WriteLine();
            metadata.TryGetValue("DataType", out var dataType);
            var dataTypeStr = dataType as string;

            renderer.Write("export default b.component((" + (dataTypeStr != null?"data: " + dataTypeStr:"") + ") => { return (<>").WriteLine()
            .Indent();
            var output = (OutputContext)renderer.Render(_document !);

            renderer.Dedent().EnsureLine().Write("</>);});").WriteLine();
            return(output.ToString(), metadata);
        }
Beispiel #18
0
        protected override void Write(TsxRenderer renderer, ParagraphBlock obj)
        {
            if (!renderer.ImplicitParagraph && renderer.EnableHtmlForBlock)
            {
                if (!renderer.IsFirstInContainer)
                {
                    renderer.EnsureLine();
                }

                renderer.Write("<mdx.P").WriteProps(obj).Write(">").Indent();
            }
            renderer.WriteLeafInline(obj);
            if (!renderer.ImplicitParagraph)
            {
                if (renderer.EnableHtmlForBlock)
                {
                    renderer.Dedent();
                    renderer.Write("</mdx.P>");
                }

                renderer.EnsureLine();
            }
        }
Beispiel #19
0
 protected override void Write(TsxRenderer renderer, TaskList obj)
 {
     if (renderer.EnableHtmlForInline)
     {
         renderer.Write("<mdx.Task").WriteProps(obj).Write(" done={");
         renderer.Write(obj.Checked ? "true" : "false");
         renderer.Write("} />");
     }
     else
     {
         renderer.Write('[');
         renderer.Write(obj.Checked ? "x" : " ");
         renderer.Write(']');
     }
 }
Beispiel #20
0
        protected override void Write(TsxRenderer renderer, DefinitionList list)
        {
            renderer.EnsureLine();
            renderer.Write("<mdx.Dl").WriteProps(list).Write('>').WriteLine();
            foreach (var item in list)
            {
                var hasOpendd              = false;
                var definitionItem         = (DefinitionItem)item;
                var countdd                = 0;
                var lastWasSimpleParagraph = false;
                for (var i = 0; i < definitionItem.Count; i++)
                {
                    var definitionTermOrContent = definitionItem[i];
                    if (definitionTermOrContent is DefinitionTerm definitionTerm)
                    {
                        if (hasOpendd)
                        {
                            if (!lastWasSimpleParagraph)
                            {
                                renderer.EnsureLine();
                            }
                            renderer.Write("</mdx.Dd>").WriteLine();
                            lastWasSimpleParagraph = false;
                            hasOpendd = false;
                            countdd   = 0;
                        }
                        renderer.Write("<mdx.Dt").WriteProps(definitionTerm).Write('>');
                        renderer.WriteLeafInline(definitionTerm);
                        renderer.Write("</mdx.Dt>").WriteLine();
                    }
                    else
                    {
                        if (!hasOpendd)
                        {
                            renderer.Write("<mdx.Dd").WriteProps(definitionItem).Write('>');
                            countdd   = 0;
                            hasOpendd = true;
                        }

                        var nextTerm          = i + 1 < definitionItem.Count ? definitionItem[i + 1] : null;
                        var isSimpleParagraph = (nextTerm is null or DefinitionItem) && countdd == 0 &&
                                                definitionTermOrContent is ParagraphBlock;

                        var saveImplicitParagraph = renderer.ImplicitParagraph;
                        if (isSimpleParagraph)
                        {
                            renderer.ImplicitParagraph = true;
                            lastWasSimpleParagraph     = true;
                        }
                        renderer.Write(definitionTermOrContent);
                        renderer.ImplicitParagraph = saveImplicitParagraph;
                        countdd++;
                    }
                }
                if (hasOpendd)
                {
                    if (!lastWasSimpleParagraph)
                    {
                        renderer.EnsureLine();
                    }
                    renderer.Write("</mdx.Dd>").WriteLine();
                }
            }
            renderer.EnsureLine();
            renderer.Write("</mdx.Dl>").WriteLine();
        }
Beispiel #21
0
        protected override void Write(TsxRenderer renderer, Table table)
        {
            renderer.EnsureLine();
            renderer.Write("<mdx.Table").WriteProps(table).Write('>').WriteLine();

            var hasBody          = false;
            var hasAlreadyHeader = false;
            var isHeaderOpen     = false;


            var hasColumnWidth = false;

            foreach (var tableColumnDefinition in table.ColumnDefinitions)
            {
                if (tableColumnDefinition.Width != 0.0f && tableColumnDefinition.Width != 1.0f)
                {
                    hasColumnWidth = true;
                    break;
                }
            }

            if (hasColumnWidth)
            {
                foreach (var tableColumnDefinition in table.ColumnDefinitions)
                {
                    var width      = Math.Round(tableColumnDefinition.Width * 100) / 100;
                    var widthValue = string.Format(CultureInfo.InvariantCulture, "{0:0.##}", width);
                    renderer.Write($"<mdx.Col width=\"{widthValue}%\" />").WriteLine();
                }
            }

            foreach (var rowObj in table)
            {
                var row = (TableRow)rowObj;
                if (row.IsHeader)
                {
                    // Allow a single thead
                    if (!hasAlreadyHeader)
                    {
                        renderer.Write("<mdx.Thead>").WriteLine();
                        isHeaderOpen = true;
                    }
                    hasAlreadyHeader = true;
                }
                else if (!hasBody)
                {
                    if (isHeaderOpen)
                    {
                        renderer.Write("</mdx.Thead>").WriteLine();
                        isHeaderOpen = false;
                    }

                    renderer.Write("<mdx.Tbody>").WriteLine();
                    hasBody = true;
                }
                renderer.Write("<mdx.Tr").WriteProps(row).Write('>').WriteLine();
                for (var i = 0; i < row.Count; i++)
                {
                    var cellObj = row[i];
                    var cell    = (TableCell)cellObj;

                    renderer.EnsureLine();
                    renderer.Write(row.IsHeader ? "<mdx.Th" : "<mdx.Td");
                    if (cell.ColumnSpan != 1)
                    {
                        renderer.Write($" colspan={{{cell.ColumnSpan}}}");
                    }
                    if (cell.RowSpan != 1)
                    {
                        renderer.Write($" rowspan={{{cell.RowSpan}}}");
                    }
                    if (table.ColumnDefinitions.Count > 0)
                    {
                        var columnIndex = cell.ColumnIndex < 0 || cell.ColumnIndex >= table.ColumnDefinitions.Count
                            ? i
                            : cell.ColumnIndex;
                        columnIndex = columnIndex >= table.ColumnDefinitions.Count ? table.ColumnDefinitions.Count - 1 : columnIndex;
                        var alignment = table.ColumnDefinitions[columnIndex].Alignment;
                        if (alignment.HasValue)
                        {
                            switch (alignment)
                            {
                            case TableColumnAlign.Center:
                                renderer.Write(" align=\"center\"");
                                break;

                            case TableColumnAlign.Right:
                                renderer.Write(" align=\"right\"");
                                break;

                            case TableColumnAlign.Left:
                                renderer.Write(" align=\"left\"");
                                break;
                            }
                        }
                    }
                    renderer.WriteProps(cell);
                    renderer.Write('>');

                    var previousImplicitParagraph = renderer.ImplicitParagraph;
                    if (cell.Count == 1)
                    {
                        renderer.ImplicitParagraph = true;
                    }
                    renderer.Write(cell);
                    renderer.ImplicitParagraph = previousImplicitParagraph;

                    renderer.Write(row.IsHeader ? "</mdx.Th>" : "</mdx.Td>").WriteLine();
                }
                renderer.Write("</mdx.Tr>").WriteLine();
            }

            if (hasBody)
            {
                renderer.Write("</mdx.Tbody>").WriteLine();
            }
            else if (isHeaderOpen)
            {
                renderer.Write("</mdx.Thead>").WriteLine();
            }
            renderer.Write("</mdx.Table>").WriteLine();
        }