MCPcopy Create free account
hub / github.com/GCWing/BitFun / render

Method render

src/apps/cli/src/ui/markdown.rs:55–342  ·  view source on GitHub ↗
(&self, markdown: &str, width: usize)

Source from the content-addressed store, hash-verified

53 }
54
55 pub fn render(&self, markdown: &str, width: usize) -> Vec<Line<'static>> {
56 let mut lines = Vec::new();
57 let mut current_line_spans: Vec<Span<'static>> = Vec::new();
58
59 // Style stack
60 let mut style_stack: Vec<StyleModifier> = Vec::new();
61 let mut list_level: usize = 0;
62 let mut in_code_block = false;
63 let mut code_block_lang = String::new();
64 let wrap_width = if width > 0 { width } else { 80 };
65
66 // Table state
67 let mut table_state = TableState::new();
68
69 // Flush current_line_spans into `lines`, wrapping at `wrap_width`.
70 // Headings skip wrapping.
71 let flush_with_wrap = |spans: &mut Vec<Span<'static>>,
72 lines: &mut Vec<Line<'static>>,
73 max_w: usize,
74 do_wrap: bool| {
75 if spans.is_empty() {
76 return;
77 }
78 let collected = std::mem::take(spans);
79 if !do_wrap {
80 lines.push(Line::from(collected));
81 return;
82 }
83 wrap_spans_to_lines(collected, max_w, lines);
84 };
85
86 let options = Options::all();
87 let parser = Parser::new_ext(markdown, options);
88
89 for event in parser {
90 match event {
91 Event::Start(tag) => match tag {
92 Tag::Heading { level, .. } => {
93 flush_with_wrap(&mut current_line_spans, &mut lines, wrap_width, true);
94 lines.push(Line::from(""));
95
96 let prefix = match level {
97 HeadingLevel::H1 => "# ",
98 HeadingLevel::H2 => "## ",
99 HeadingLevel::H3 => "### ",
100 HeadingLevel::H4 => "#### ",
101 HeadingLevel::H5 => "##### ",
102 HeadingLevel::H6 => "###### ",
103 };
104 current_line_spans.push(Span::styled(
105 prefix.to_string(),
106 self.theme
107 .style(StyleKind::Primary)
108 .add_modifier(Modifier::BOLD),
109 ));
110
111 style_stack.push(StyleModifier::Heading);
112 }

Callers

nothing calls this directly

Calls 15

takeFunction · 0.85
wrap_spans_to_linesFunction · 0.85
styleMethod · 0.80
start_tableMethod · 0.80
collectMethod · 0.80
start_rowMethod · 0.80
start_cellMethod · 0.80
end_tableMethod · 0.80
end_rowMethod · 0.80
end_cellMethod · 0.80
compute_styleMethod · 0.80
push_spanMethod · 0.80

Tested by

no test coverage detected