refs: converge Markdown link resolution on one scoped catalog
Add the single pure document-link resolver: it resolves exact anchors first, collects all canonical-name fallback candidates, decodes percent escapes once, and distinguishes resolved/not_loaded/missing/ambiguous/external/outside_scope/ invalid without network access or caller-entry fallback. The loaded catalog never opens a link target; the explicit --refs catalog may verify directly referenced files once, inside the package root, with symlink refusal and read limits. Add unit-range ownership helpers to the heading index and focused resolver tests.
This commit is contained in:
1 parent
d8212aaa01
commit
1b214d48da
3 files changed
+1070
No files matched your search
@@ -105,6 +105,38 @@ impl UnitIndex {
|
||||
.iter()
|
||||
.find(|f| block_index >= f.body_start && block_index < f.body_end)
|
||||
}
|
||||
|
||||
/// Complete public range of one unit: the whole source for the root, the
|
||||
/// heading-through-next-same-or-higher-heading range otherwise.
|
||||
pub fn unit_range(&self, id: &UnitId) -> Option<Span> {
|
||||
if id.path.is_empty() {
|
||||
return self
|
||||
.units
|
||||
.iter()
|
||||
.find(|unit| unit.id.path.is_empty())
|
||||
.map(|unit| unit.span);
|
||||
}
|
||||
self.headings
|
||||
.iter()
|
||||
.find(|heading| heading.path == id.path)
|
||||
.map(|heading| heading.full_span)
|
||||
}
|
||||
|
||||
/// Innermost unit whose complete range contains `span`.
|
||||
///
|
||||
/// Used for document links: the root covers the whole source, so a linked
|
||||
/// span always has an owner. Ties prefer the deeper path, then the later
|
||||
/// start (a child cannot be larger than its parent).
|
||||
pub fn unit_containing_span(&self, span: Span) -> Option<UnitId> {
|
||||
self.units
|
||||
.iter()
|
||||
.filter_map(|unit| {
|
||||
let range = self.unit_range(&unit.id)?;
|
||||
(range.start <= span.start && span.end <= range.end).then_some((unit, range))
|
||||
})
|
||||
.max_by_key(|(unit, range)| (unit.id.path.len(), range.start))
|
||||
.map(|(unit, _)| unit.id.clone())
|
||||
}
|
||||
}
|
||||
|
||||
fn has_payload(node: &MdNode) -> bool {
|
||||
@@ -791,6 +823,19 @@ mod tests {
|
||||
.any(|diagnostic| diagnostic.code == "sem.ambiguous_identity"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn innermost_unit_owns_a_linked_span() {
|
||||
let source = "# @knowledge 数学\n\n## @exec 计算\n\n见[链接](#细节)。\n\n### 细节\n\n`return 1`\n";
|
||||
let index = index_of(source);
|
||||
let link_start = source.find("[链接]").unwrap();
|
||||
let owner = index
|
||||
.unit_containing_span(Span::new(link_start, link_start + 2))
|
||||
.expect("owning unit");
|
||||
assert_eq!(owner.path, vec!["数学".to_string(), "计算".to_string()]);
|
||||
let root = index.unit_containing_span(Span::new(0, 1)).expect("root");
|
||||
assert!(root.path.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn knowledge_scope_payloads_and_sections_stay_inert() {
|
||||
let index = index_of(
|
||||
|
||||
Reference in new issue
Block a user