-
Notifications
You must be signed in to change notification settings - Fork 6
Add Support for Subcategories in Guide #84
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ElijahAhianyo
wants to merge
11
commits into
cot-rs:master
Choose a base branch
from
ElijahAhianyo:elijah/sub-categories
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
807446c
Support route with section
ElijahAhianyo e13abfe
Add support for Sub categories in Guide
ElijahAhianyo 6de3cd9
Revert "Add support for Sub categories in Guide"
ElijahAhianyo 87c2b0a
Revert "Support route with section"
ElijahAhianyo fe74c78
clean up commits
ElijahAhianyo 5a1bb6a
add docs and fix struct scope
ElijahAhianyo f416ac9
more docs
ElijahAhianyo eafaa3f
Support route with section
ElijahAhianyo dbfcae9
fix ci failures
ElijahAhianyo 7680e71
Merge branch 'master' into elijah/sub-categories
ElijahAhianyo 411c6a5
move css for guide chapters to custom.scss
ElijahAhianyo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -64,6 +64,77 @@ async fn index(base_context: BaseContext) -> cot::Result<Html> { | |
| Ok(Html::new(rendered)) | ||
| } | ||
|
|
||
| #[derive(Debug, Clone)] | ||
| struct GuideLinkCategory { | ||
| title: &'static str, | ||
| guides: Vec<GuideCategoryItem>, | ||
| } | ||
|
|
||
| #[derive(Debug, Clone)] | ||
| enum GuideCategoryItem { | ||
| Page(MdPageLink), | ||
| SubCategory { | ||
| title: &'static str, | ||
| pages: Vec<MdPageLink>, | ||
| }, | ||
| } | ||
|
|
||
| impl GuideCategoryItem { | ||
| /// Takes a link and checks whether any of the pages in that subcategory | ||
| /// matches it. We call this inside the templates to decide whether a | ||
| /// subcategory accordion should start open or closed. | ||
| fn contains_active_page(&self, current_link: &str) -> bool { | ||
| match self { | ||
| GuideCategoryItem::SubCategory { pages, .. } => { | ||
| pages.iter().any(|p| p.link == current_link) | ||
| } | ||
| GuideCategoryItem::Page(_) => false, | ||
| } | ||
| } | ||
| /// Returns a unique ID for the category which bootstrap uses to | ||
| /// control the open/close behavior of the accordion | ||
| fn collapse_id(&self) -> String { | ||
| match self { | ||
| GuideCategoryItem::SubCategory { title, .. } => title.to_lowercase().replace(' ', "-"), | ||
| GuideCategoryItem::Page(_) => String::new(), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Represents an item in a documentation guide. Each item can either be a | ||
| /// single markdown page or a subcategory containing a collection of related | ||
| /// pages. | ||
| pub enum GuideItem { | ||
| /// A single markdown page to be rendered as part of the guide. | ||
| /// | ||
| /// # Examples | ||
| /// ```ignore | ||
| /// use cot_site::{GuideItem, md_page}; | ||
| /// | ||
| /// let page = GuideItem::Page(md_page!("guide", "introduction")); | ||
| /// ``` | ||
| Page(MdPage), | ||
| /// A subcategory containing a collection of related pages. | ||
| /// | ||
| /// # Examples | ||
| /// ```ignore | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ignore here because the |
||
| /// use cot_site::{GuideItem, md_page}; | ||
| /// | ||
| /// let subcategory = GuideItem::SubCategory{ | ||
| /// title: "Database", | ||
| /// pages: vec![ | ||
| /// md_page!("guide/databases/overview"), | ||
| /// md_page!("guide/databases/queries"), | ||
| /// md_page!("guide/databases/migrations"), | ||
| /// ] | ||
| /// }; | ||
| /// ``` | ||
| SubCategory { | ||
| title: &'static str, | ||
| pages: Vec<MdPage>, | ||
| }, | ||
| } | ||
|
|
||
| #[derive(Debug, Template)] | ||
| #[template(path = "guide.html")] | ||
| struct GuideTemplate<'a> { | ||
|
|
@@ -79,12 +150,6 @@ struct GuideTemplate<'a> { | |
| next: Option<&'a MdPageLink>, | ||
| } | ||
|
|
||
| #[derive(Debug, Clone)] | ||
| struct GuideLinkCategory { | ||
| title: &'static str, | ||
| guides: Vec<MdPageLink>, | ||
| } | ||
|
|
||
| fn render_section(section: &Section) -> Safe<String> { | ||
| #[derive(Debug, Clone, Template)] | ||
| #[template(path = "_md_page_toc_item.html")] | ||
|
|
@@ -257,7 +322,7 @@ impl CotSiteApp { | |
| /// The `master_pages` parameter should contain a list of sections, where | ||
| /// each section is a tuple containing the name of the section and list | ||
| /// of pages inside it. | ||
| pub fn new(master_pages: Vec<(&'static str, Vec<MdPage>)>) -> Self { | ||
| pub fn new(master_pages: Vec<(&'static str, Vec<GuideItem>)>) -> Self { | ||
| let pages = get_categories(master_pages); | ||
|
|
||
| Self { | ||
|
|
@@ -335,6 +400,7 @@ impl App for CotSiteApp { | |
| fn static_files(&self) -> Vec<StaticFile> { | ||
| static_files!( | ||
| "favicon.ico", | ||
| "static/css/guide_chapters.css", | ||
|
ElijahAhianyo marked this conversation as resolved.
Outdated
|
||
| "static/css/main.css", | ||
| "static/js/color-modes.js", | ||
| "static/js/search.js", | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if
GuideCategoryItemcould be merged withGuideItemas they seem almost the same. Why did you decide to split them? 🤔Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think they serve the same purpose, per se. The previous version of
CotApp::newacceptedMdPage, which was a public-facing representation of a page. When parsing the guide(cot_site::guides::parse_guides), we convert that toMdPageLink(almost similar signature asMdPage), which is an internal representation of the page that gets rendered in the template.This change also follows the same concept/design;
GuideItemis public-facing, whereasGuideCategoryItemis an internal representation ofMdPageLink(s) that get rendered in the template