Terminal Markdown previewer — GUI-like experience.
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

chore: split markdown toc

RivoLink 0e64c463 5db82c76

+46 -45
+3 -39
src/app.rs
··· 1 1 use crate::{ 2 2 markdown::{ 3 - build_plain_lines, hash_file_contents, hash_str, parse_markdown_with_width, read_file_state, 3 + build_plain_lines, hash_file_contents, hash_str, parse_markdown_with_width, 4 + read_file_state, 5 + toc::{should_hide_single_h1, should_promote_h2_when_no_h1, toc_display_level, TocEntry}, 4 6 }, 5 7 render::{build_status_bar, build_toc_line_with_index, toc_header_line}, 6 8 theme::{ ··· 34 36 ".next", 35 37 ".cache", 36 38 ]; 37 - 38 - #[derive(Clone)] 39 - pub(crate) struct TocEntry { 40 - pub(crate) level: u8, 41 - pub(crate) title: String, 42 - pub(crate) line: usize, 43 - } 44 39 45 40 #[derive(Clone, Copy, Debug, PartialEq, Eq)] 46 41 pub(crate) struct FileState { ··· 1674 1669 true 1675 1670 } 1676 1671 } 1677 - 1678 - pub(crate) fn should_hide_single_h1(toc: &[TocEntry]) -> bool { 1679 - let h1_count = toc.iter().filter(|entry| entry.level == 1).count(); 1680 - let has_h2 = toc.iter().any(|entry| entry.level == 2); 1681 - h1_count == 1 && has_h2 1682 - } 1683 - 1684 - pub(crate) fn should_promote_h2_when_no_h1(toc: &[TocEntry]) -> bool { 1685 - !toc.iter().any(|entry| entry.level == 1) && toc.iter().any(|entry| entry.level == 2) 1686 - } 1687 - 1688 - pub(crate) fn toc_display_level(level: u8, hide_single_h1: bool, promote_h2_root: bool) -> u8 { 1689 - if hide_single_h1 || promote_h2_root { 1690 - match level { 1691 - 2 => 1, 1692 - 3 => 2, 1693 - _ => level, 1694 - } 1695 - } else { 1696 - level 1697 - } 1698 - } 1699 - 1700 - pub(crate) fn normalize_toc(mut toc: Vec<TocEntry>) -> Vec<TocEntry> { 1701 - if should_hide_single_h1(&toc) || should_promote_h2_when_no_h1(&toc) { 1702 - toc.retain(|entry| matches!(entry.level, 1..=3)); 1703 - } else { 1704 - toc.retain(|entry| matches!(entry.level, 1..=2)); 1705 - } 1706 - toc 1707 - }
+1 -1
src/main.rs
··· 25 25 const MAX_STDIN_BYTES: usize = 8 * 1024 * 1024; 26 26 27 27 #[cfg(test)] 28 - pub(crate) use app::{ 28 + pub(crate) use markdown::toc::{ 29 29 normalize_toc, should_hide_single_h1, should_promote_h2_when_no_h1, toc_display_level, TocEntry, 30 30 }; 31 31 #[cfg(test)]
+4 -4
src/markdown.rs src/markdown/mod.rs
··· 1 - use crate::{ 2 - app::{normalize_toc, TocEntry}, 3 - theme::{app_theme, MarkdownTheme}, 4 - }; 1 + pub(crate) mod toc; 2 + 3 + use crate::theme::{app_theme, MarkdownTheme}; 5 4 use pulldown_cmark::{ 6 5 Alignment, CodeBlockKind, Event as MdEvent, HeadingLevel, Options, Parser, Tag, TagEnd, 7 6 }; ··· 17 16 use syntect::{ 18 17 easy::HighlightLines, highlighting::Theme, parsing::SyntaxSet, util::LinesWithEndings, 19 18 }; 19 + use toc::{normalize_toc, TocEntry}; 20 20 use unicode_width::{UnicodeWidthChar, UnicodeWidthStr}; 21 21 22 22 const TAB_STOP: usize = 4;
+37
src/markdown/toc.rs
··· 1 + #[derive(Clone)] 2 + pub(crate) struct TocEntry { 3 + pub(crate) level: u8, 4 + pub(crate) title: String, 5 + pub(crate) line: usize, 6 + } 7 + 8 + pub(crate) fn should_hide_single_h1(toc: &[TocEntry]) -> bool { 9 + let h1_count = toc.iter().filter(|entry| entry.level == 1).count(); 10 + let has_h2 = toc.iter().any(|entry| entry.level == 2); 11 + h1_count == 1 && has_h2 12 + } 13 + 14 + pub(crate) fn should_promote_h2_when_no_h1(toc: &[TocEntry]) -> bool { 15 + !toc.iter().any(|entry| entry.level == 1) && toc.iter().any(|entry| entry.level == 2) 16 + } 17 + 18 + pub(crate) fn toc_display_level(level: u8, hide_single_h1: bool, promote_h2_root: bool) -> u8 { 19 + if hide_single_h1 || promote_h2_root { 20 + match level { 21 + 2 => 1, 22 + 3 => 2, 23 + _ => level, 24 + } 25 + } else { 26 + level 27 + } 28 + } 29 + 30 + pub(crate) fn normalize_toc(mut toc: Vec<TocEntry>) -> Vec<TocEntry> { 31 + if should_hide_single_h1(&toc) || should_promote_h2_when_no_h1(&toc) { 32 + toc.retain(|entry| matches!(entry.level, 1..=3)); 33 + } else { 34 + toc.retain(|entry| matches!(entry.level, 1..=2)); 35 + } 36 + toc 37 + }
+1 -1
src/render.rs
··· 866 866 } 867 867 868 868 pub(crate) fn build_toc_line_with_index( 869 - entry: &crate::app::TocEntry, 869 + entry: &crate::markdown::toc::TocEntry, 870 870 display_level: u8, 871 871 top_level_index: Option<usize>, 872 872 active: bool,