Example #1
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();
        }
Example #2
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();
        }
Example #3
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>");
                }
            }
        }
Example #4
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>");
            }
        }
Example #5
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();
        }