Skip to content

Commit

Permalink
Memoize madskin init
Browse files Browse the repository at this point in the history
  • Loading branch information
1st1 committed Aug 11, 2021
1 parent f85ca8f commit 4ccea4d
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/markdown.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use once_cell::sync::Lazy;

fn prepare_markdown(text: &str) -> String {
let mut min_indent = text.len();
for line in text.lines() {
Expand All @@ -23,12 +25,13 @@ fn prepare_markdown(text: &str) -> String {
return buf;
}

fn make_skin() -> termimad::MadSkin {
static MADSKIN: Lazy<termimad::MadSkin> = Lazy::new(|| {
use crossterm::style::{Color, Attribute};

if !atty::is(atty::Stream::Stdout) {
return termimad::MadSkin::no_style();
}

use crossterm::style::{Color, Attribute};
let mut skin = termimad::MadSkin::default();
skin.bold.set_fg(Color::Reset);
skin.inline_code.set_fg(Color::Reset);
Expand All @@ -38,7 +41,7 @@ fn make_skin() -> termimad::MadSkin {
skin.code_block.set_bg(Color::Reset);
skin.code_block.add_attr(Attribute::Bold);
skin
}
});

fn parse_markdown(text: &str) -> minimad::Text {
use minimad::{Text, Composite};
Expand Down Expand Up @@ -87,9 +90,8 @@ fn parse_markdown(text: &str) -> minimad::Text {
pub fn format_markdown(text: &str) -> String {
let text = prepare_markdown(&text);
let text = parse_markdown(&text);
let skin = make_skin();
let fmt = termimad::FmtText::from_text(
&skin,
&MADSKIN,
text,
None,
);
Expand All @@ -102,9 +104,8 @@ pub fn format_title(text: &str) -> String {
if !text.lines.is_empty() {
text.lines.drain(1..);
}
let skin = make_skin();
let fmt = termimad::FmtText::from_text(
&skin,
&MADSKIN,
text,
None,
);
Expand Down

0 comments on commit 4ccea4d

Please sign in to comment.