Skip to content

Commit

Permalink
markdown: Support alignment for table cells (#7201)
Browse files Browse the repository at this point in the history
Just a small improvement as a follow up to @kierangilliam great work on
#6958

Rendering a table specified like this:
```markdown
| Left columns  | Center columns | Right columns |
| ------------- |:--------------:| -------------:|
| left foo      | center foo     | right foo     |
| left bar      | center bar     | right bar     |
| left baz      | center baz     | right baz     |
```
Does now look like this (notice the cell alignments):

![image](https://github.com/zed-industries/zed/assets/53836821/0f5b6a1e-a3c2-4fe9-bdcb-8654dbae7980)

Release Notes:
- N/A
  • Loading branch information
bennetbo authored Feb 1, 2024
1 parent 0102ffb commit 2d41a11
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions crates/markdown_preview/src/markdown_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use gpui::{
Styled, StyledText, WindowContext,
};
use language::LanguageRegistry;
use pulldown_cmark::{CodeBlockKind, Event, HeadingLevel, Options, Parser, Tag};
use pulldown_cmark::{Alignment, CodeBlockKind, Event, HeadingLevel, Options, Parser, Tag};
use rich_text::render_rich_text;
use theme::{ActiveTheme, Theme};
use ui::{h_flex, v_flex};
Expand All @@ -16,6 +16,7 @@ enum TableState {
}

struct MarkdownTable {
column_alignments: Vec<Alignment>,
header: Vec<Div>,
body: Vec<Vec<Div>>,
current_row: Vec<Div>,
Expand All @@ -24,8 +25,9 @@ struct MarkdownTable {
}

impl MarkdownTable {
fn new(border_color: Hsla) -> Self {
fn new(border_color: Hsla, column_alignments: Vec<Alignment>) -> Self {
Self {
column_alignments,
header: Vec::new(),
body: Vec::new(),
current_row: Vec::new(),
Expand All @@ -47,9 +49,15 @@ impl MarkdownTable {
}

fn add_cell(&mut self, contents: AnyElement) {
let cell = div()
.child(contents)
let container = match self.alignment_for_next_cell() {
Alignment::Left | Alignment::None => div(),
Alignment::Center => v_flex().items_center(),
Alignment::Right => v_flex().items_end(),
};

let cell = container
.w_full()
.child(contents)
.px_2()
.py_1()
.border_color(self.border_color);
Expand Down Expand Up @@ -79,6 +87,13 @@ impl MarkdownTable {
}
table
}

fn alignment_for_next_cell(&self) -> Alignment {
self.column_alignments
.get(self.current_row.len())
.copied()
.unwrap_or(Alignment::None)
}
}

struct Renderer<I> {
Expand Down Expand Up @@ -141,8 +156,11 @@ where
Tag::BlockQuote => {
self.block_quote_depth += 1;
}
Tag::Table(_text_alignments) => {
self.table = Some(MarkdownTable::new(self.theme.colors().border));
Tag::Table(column_alignments) => {
self.table = Some(MarkdownTable::new(
self.theme.colors().border,
column_alignments,
));
}
_ => {}
}
Expand Down

0 comments on commit 2d41a11

Please sign in to comment.