From 65c45083cf01d3a189b28e964254e82680d90ba3 Mon Sep 17 00:00:00 2001 From: Daiki Ihara Date: Sat, 19 Dec 2020 22:12:51 +0900 Subject: Support intra_doc links --- crates/ide/src/doc_links.rs | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) (limited to 'crates/ide/src/doc_links.rs') diff --git a/crates/ide/src/doc_links.rs b/crates/ide/src/doc_links.rs index 1ff818de2..678d22d03 100644 --- a/crates/ide/src/doc_links.rs +++ b/crates/ide/src/doc_links.rs @@ -1,6 +1,6 @@ //! Resolves and rewrites links in markdown documentation. -use std::{convert::TryFrom, iter::once}; +use std::{convert::TryFrom, iter::once, ops::Range}; use itertools::Itertools; use pulldown_cmark::{BrokenLink, CowStr, Event, InlineStr, LinkType, Options, Parser, Tag}; @@ -61,6 +61,30 @@ pub(crate) fn rewrite_links(db: &RootDatabase, markdown: &str, definition: &Defi out } +pub(crate) fn extract_definitions_from_markdown( + markdown: &str, +) -> Vec<(String, Option, Range)> { + let mut res = vec![]; + let mut cb = |link: BrokenLink| { + Some(( + /*url*/ link.reference.to_owned().into(), + /*title*/ link.reference.to_owned().into(), + )) + }; + let doc = Parser::new_with_broken_link_callback(markdown, Options::empty(), Some(&mut cb)); + for (event, range) in doc.into_offset_iter() { + match event { + Event::Start(Tag::Link(_link_type, ref target, ref title)) => { + let link = if target.is_empty() { title } else { target }; + let (link, ns) = parse_link(link); + res.push((link.to_string(), ns, range)); + } + _ => {} + } + } + res +} + /// Remove all links in markdown documentation. pub(crate) fn remove_links(markdown: &str) -> String { let mut drop_link = false; -- cgit v1.2.3