Skip to content

Commit

Permalink
Implement tables, math
Browse files Browse the repository at this point in the history
  • Loading branch information
asinghvi17 committed Feb 17, 2024
1 parent 2b2fa4d commit 6aaec88
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 81 deletions.
8 changes: 6 additions & 2 deletions docs/make.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
using Documenter
using DocumenterVitepress

makedocs(; sitename="DocumenterVitepress", authors="LuxDL et al.",
makedocs(;
sitename="DocumenterVitepress",
authors="LuxDL et al.",
modules=[DocumenterVitepress],
warnonly = true,
checkdocs=:all,
format=DocumenterVitepress.MarkdownVitepress(),
draft=false,
source="src", build=joinpath(@__DIR__, "docs_site")
)
)

# To edit the sidebar, you must edit `docs/src/.vitepress/config.mts`.
1 change: 0 additions & 1 deletion docs/src/.vitepress/config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ export default defineConfig({
items: [
{ text: 'Code Example', link: '/code_example' },
{ text: 'Markdown Examples', link: '/markdown-examples' },
{ text: 'Tables', link: '/tables' },
{ text: 'Runtime API Examples', link: '/api-examples' }
]
}
Expand Down
71 changes: 71 additions & 0 deletions docs/src/markdown-examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,52 @@ export default {
}
```

### Code groups

::: code-group

```js [config.js]
/**
* @type {import('vitepress').UserConfig}
*/
const config = {
// ...
}

export default config
```

```ts [config.ts]
import type { UserConfig } from 'vitepress'

const config: UserConfig = {
// ...
}

export default config
```

:::

### Code focus

```js
export default {
data () {
return {
msg: 'Focused!' // [!code focus]
}
}
}
```
### Lists

1. a
1. b
1. c
1. d
1. e

## Custom Containers

**Input**
Expand Down Expand Up @@ -160,6 +206,31 @@ d content 2

::::


## GitHub-flavored Alerts
See: https://vitepress.dev/guide/markdown#github-flavored-alerts

> [!WARNING]
> Critical content.
## Tables
See: https://vitepress.dev/guide/markdown#github-style-tables

| Tables | Are | Cool |
| ------------- | :-----------: | ----: |
| col 3 is | right-aligned | \$1600 |
| col 2 is | centered | \$12 |
| zebra stripes | are neat | \$1 |

## Equations

When $a \ne 0$, there are two solutions to $(ax^2 + bx + c = 0)$ and they are

$$x = {-b \pm \sqrt{b^2-4ac} \over 2a}$$

Don't type anything after the last double dollar sign, and make sure there are no spaces after the opening double dollar sign in the display math!


## More

Check out the documentation for the [full list of markdown extensions](https://vitepress.dev/guide/markdown).
65 changes: 0 additions & 65 deletions docs/src/tables.md

This file was deleted.

84 changes: 71 additions & 13 deletions src/writer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ function render(io::IO, mime::MIME"text/plain", node::Documenter.MarkdownAST.Nod
print(io, text.text)
end
# Bold text (strong)
# These are wrapper elements - so the wrapper doesn't actually contain any text, the current node's children do.
function render(io::IO, mime::MIME"text/plain", node::Documenter.MarkdownAST.Node, strong::MarkdownAST.Strong, page, doc)
# @infiltrate
print(io, "**")
Expand Down Expand Up @@ -381,29 +382,86 @@ function render(io::IO, mime::MIME"text/plain", node::Documenter.MarkdownAST.Nod
end
# Admonitions
function render(io::IO, mime::MIME"text/plain", node::Documenter.MarkdownAST.Node, admonition::MarkdownAST.Admonition, page, doc)
# @infiltrate
# Main.@infiltrate
println(io, "\n::: $(admonition.category) $(admonition.title)")
render(io, mime, node, node.children, page, doc)
println(io, "\n:::")
end
# Block quotes
function render(io::IO, mime::MIME"text/plain", node::Documenter.MarkdownAST.Node, q::MarkdownAST.BlockQuote, page, doc)
# Main.@infiltrate
iob = IOBuffer()
render(iob, mime, node, node.children, page, doc)
output = String(take!(iob))
eachline = split(output, '\n')
println.((io,), "> " .* eachline)
end
# Inline math
function render(io::IO, mime::MIME"text/plain", node::Documenter.MarkdownAST.Node, math::MarkdownAST.InlineMath, page, doc)
# Main.@infiltrate
print(io, "\$", math.math, "\$")
end
# Display math
function render(io::IO, mime::MIME"text/plain", node::Documenter.MarkdownAST.Node, math::MarkdownAST.DisplayMath, page, doc)
# Main.@infiltrate
println(io)
println(io, "\$\$", math.math, "\$\$")
end
# Lists
# TODO: list ordering is broken!
function render(io::IO, mime::MIME"text/plain", node::Documenter.MarkdownAST.Node, list::MarkdownAST.List, page, doc)
# @infiltrate
if list.type === :ordered
println(io)
for (i, item) in enumerate(node.children)
print(io, "$(i). ")
render(io, mime, item, item.children, page, doc)
print(io, "\n")
end
else
for item in node.children
print(io, "- ")
render(io, mime, item, item.children, page, doc)
print(io, "\n")
bullet = list.type === :ordered ? "1. " : "- "
iob = IOBuffer()
for item in node.children
render(iob, mime, item, item.children, page, doc)
eachline = split(String(take!(iob)), '\n')
print(io, bullet)
println.((io,), " " .* eachline)
end
end
# Tables
function render(io::IO, mime::MIME"text/plain", node::Documenter.MarkdownAST.Node, table::MarkdownAST.TableCell, page, doc)
println("Encountered table cell!")
end

function render(io::IO, mime::MIME"text/plain", node::Documenter.MarkdownAST.Node, table::MarkdownAST.Table, page, doc)
th_row, tbody_rows = Iterators.peel(MarkdownAST.tablerows(node))
# function mdconvert(t::Markdown.Table, parent; kwargs...)
alignment_style = map(table.spec) do align
if align == :right
"text-align: right"
elseif align == :center
"text-align: center"
else
"text-align: left"
end
end
# We now emit a HTML table, which is of course styled by CSS - so that images etc. can be included more easily,
# without worrying about Markdown spacing issues.
println(io, "<table>") # begin table
println(io, "<thead>")
println(io, "<tr>") # begin header row
# Main.@infiltrate
for (cell, align) in zip(th_row.children, alignment_style)
print(io, "<th style=\"$align\">")
render(io, mime, cell, cell.children, page, doc)
println(io, "</th>")
end
println(io, "</tr>") # end header row
println(io, "</thead>")
println(io, "<tbody>")
for row in tbody_rows
println(io, "<tr>") # begin row
for (cell, align) in zip(row.children, alignment_style)
print(io, "<td style=\"$align\">")
render(io, mime, cell, cell.children, page, doc)
println(io, "</td>") end
println(io, "</tr>") # end row
end
println(io, "</tbody>")
println(io, "</table>") # end table

end
# Images
function render(io::IO, mime::MIME"text/plain", node::Documenter.MarkdownAST.Node, image::MarkdownAST.Image, page, doc)
Expand Down

0 comments on commit 6aaec88

Please sign in to comment.