assistant: Add /rustdoc
slash command (#12453)
This PR adds a `/rustdoc` slash command for retrieving and inserting rustdoc docs into the Assistant. Right now the command accepts the crate name as an argument and will return the top-level docs from `docs.rs`. Release Notes: - N/A
This commit is contained in:
parent
dd328efaa7
commit
08881828ce
8 changed files with 152 additions and 21 deletions
|
@ -36,12 +36,6 @@ impl MarkdownWriter {
|
|||
.any(|parent_element| parent_element.tag == tag)
|
||||
}
|
||||
|
||||
fn is_inside_heading(&self) -> bool {
|
||||
["h1", "h2", "h3", "h4", "h5", "h6"]
|
||||
.into_iter()
|
||||
.any(|heading| self.is_inside(heading))
|
||||
}
|
||||
|
||||
/// Appends the given string slice onto the end of the Markdown output.
|
||||
fn push_str(&mut self, str: &str) {
|
||||
self.markdown.push_str(str);
|
||||
|
@ -135,16 +129,14 @@ impl MarkdownWriter {
|
|||
}
|
||||
}
|
||||
"div" | "span" => {
|
||||
if tag.attrs.borrow().iter().any(|attr| {
|
||||
attr.name.local.to_string() == "class"
|
||||
&& attr.value.to_string() == "sidebar-elems"
|
||||
}) {
|
||||
return StartTagOutcome::Skip;
|
||||
}
|
||||
let classes_to_skip = ["nav-container", "sidebar-elems", "out-of-band"];
|
||||
|
||||
if tag.attrs.borrow().iter().any(|attr| {
|
||||
attr.name.local.to_string() == "class"
|
||||
&& attr.value.to_string() == "out-of-band"
|
||||
&& attr
|
||||
.value
|
||||
.split(' ')
|
||||
.any(|class| classes_to_skip.contains(&class.trim()))
|
||||
}) {
|
||||
return StartTagOutcome::Skip;
|
||||
}
|
||||
|
@ -189,10 +181,6 @@ impl MarkdownWriter {
|
|||
return Ok(());
|
||||
}
|
||||
|
||||
if self.is_inside_heading() && self.is_inside("a") {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let trimmed_text = text.trim_matches(|char| char == '\n' || char == '\r' || char == '§');
|
||||
self.push_str(trimmed_text);
|
||||
|
||||
|
|
|
@ -4,6 +4,8 @@
|
|||
|
||||
mod markdown_writer;
|
||||
|
||||
use std::io::Read;
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
use html5ever::driver::ParseOpts;
|
||||
use html5ever::parse_document;
|
||||
|
@ -14,7 +16,7 @@ use markup5ever_rcdom::RcDom;
|
|||
use crate::markdown_writer::MarkdownWriter;
|
||||
|
||||
/// Converts the provided rustdoc HTML to Markdown.
|
||||
pub fn convert_rustdoc_to_markdown(html: &str) -> Result<String> {
|
||||
pub fn convert_rustdoc_to_markdown(mut html: impl Read) -> Result<String> {
|
||||
let parse_options = ParseOpts {
|
||||
tree_builder: TreeBuilderOpts {
|
||||
drop_doctype: true,
|
||||
|
@ -24,7 +26,7 @@ pub fn convert_rustdoc_to_markdown(html: &str) -> Result<String> {
|
|||
};
|
||||
let dom = parse_document(RcDom::default(), parse_options)
|
||||
.from_utf8()
|
||||
.read_from(&mut html.as_bytes())
|
||||
.read_from(&mut html)
|
||||
.context("failed to parse rustdoc HTML")?;
|
||||
|
||||
let markdown_writer = MarkdownWriter::new();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue