Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ jobs:
const text = [
`# Citation Preview ${tag}`,
'',
'Hover over numeric citations such as [1-10] or [3,1-10,12] to view the corresponding references together in Obsidian Page preview. Citation groups are assembled in memory from the numbered bibliography in the same note.',
'Display numeric citations such as [1-10] or [3,1-10,12] as blue links and hover to view their references together in Obsidian Page preview. Citation groups are assembled in memory from references in the same note.',
'',
'Reference blocks are identified by unique ^ref-N and ^sref-N block IDs in the same note. No particular bibliography heading is required. Use explicit links to distinguish main and supplementary references with the same number. See README.md for the exact format and linked-citation examples.',
'Explicit links resolve by their unique ^ref-N and ^sref-N block IDs without requiring a bibliography heading. With plain numeric recognition enabled, citations can also use detected numbered reference lists, preferring the same structural section and then the nearest list, including entries without anchors. The optional Add missing reference anchors command writes block IDs only when manually invoked. See README.md for supported formats, list detection, and navigation behavior.',
'',
'## Changes',
'',
Expand Down
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Changelog

## 1.3.0

- Display resolved citations as blue links, including their outer brackets and separators, with an option to use the current theme's link color. Keep existing links and native single-reference previews; add navigation for resolved plain-text citation numbers without rewriting the note.
- Keep editor syntax and link destinations in their normal colors. Ordinary editor clicks still place the cursor; modified clicks navigate to the corresponding reference.
- Show an explicitly linked citation group such as `[1-5](#^ref-1)` in one preview with the specified reference first, a horizontal rule, and the full expanded group below. Preserve the existing link's click target.
- Recognize nearby numbered reference lists for plain numeric citations, including lists without block anchors. Prefer the same structural section, then the nearest detected list; report missing entries there instead of borrowing from another list. Exact `ref` and `sref` links continue to use their actual anchors independently of bibliography headings.
- Add the manually invoked **Add missing reference anchors** command. It adds missing block IDs to recognized reference entries in the current note, keeps existing IDs, avoids collisions, and applies edits as one undoable editor transaction. Normal previewing and navigation do not write to the note.
- Add regression coverage for list selection, anchor insertion, visible citation ranges, link preservation, grouped previews, and editor interactions. Keep the plugin ID `citation-range-preview` and minimum Obsidian version `1.15.1`.

## 1.2.0

- Identify references directly from their `^ref-N` and `^sref-N` block IDs, without requiring a bibliography heading. References work under arbitrary headings or with no heading, including notes whose headings contain section numbers or entry counts.
Expand Down
77 changes: 64 additions & 13 deletions README.md

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"id": "citation-range-preview",
"name": "Citation Preview",
"version": "1.2.0",
"version": "1.3.0",
"minAppVersion": "1.15.1",
"description": "Preview numeric citation groups in Page preview using reference block anchors in the same note.",
"description": "Preview and link numeric citation groups using reference anchors or nearby bibliographies in the same note.",
"author": "Chuangwu Deng",
"authorUrl": "https://github.com/lyingTree",
"isDesktopOnly": false
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "citation-preview",
"version": "1.2.0",
"description": "Preview numeric citation groups in Page preview using reference block anchors in the same note.",
"version": "1.3.0",
"description": "Preview and link numeric citation groups using reference anchors or nearby bibliographies in the same note.",
"main": "main.js",
"private": true,
"scripts": {
Expand Down
85 changes: 85 additions & 0 deletions src/anchors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { ignoredMarkdownRanges, parseReferences, referenceAnchor, sourceLines } from './bibliography';
import { blockAnchorAtEnd, detectReferenceLists } from './reference-lists';

export interface AnchorInsertion {
from: number;
to: number;
insert: string;
}

export interface AnchorPlan {
/** Sorted insertions, suitable for one undoable editor transaction. */
changes: AnchorInsertion[];
added: number;
skipped: number;
lists: number;
}

/**
* Plan only. No source or files are changed until a user invokes the registered command.
* Existing IDs, including non-reference IDs, are never replaced or duplicated on a block.
*/
export function planReferenceAnchors(source: string): AnchorPlan {
const lines = sourceLines(source);
const ignored = ignoredMarkdownRanges(source, lines, false);
const index = parseReferences(source, lines);
const lists = detectReferenceLists(source, index, lines);
const ids = new Set<string>();
const namespaces = new Set<string>();
const usedNumbers = new Map(index.sections.map(section => [section.id, new Set([...section.entries.keys(), ...section.duplicates])]));
let ignoredIndex = 0;
for (const line of lines) {
const anchor = blockAnchorAtEnd(line.text);
if (!anchor) continue;
const offset = line.from + anchor.from;
while (ignoredIndex < ignored.length && ignored[ignoredIndex].to <= offset) ignoredIndex += 1;
if (ignored[ignoredIndex]?.from <= offset) continue;
ids.add(anchor.id);
const identity = referenceAnchor(anchor.id);
if (identity) {
namespaces.add(identity.namespace);
const numbers = usedNumbers.get(identity.namespace) ?? new Set<number>();
numbers.add(identity.number);
usedNumbers.set(identity.namespace, numbers);
}
}

const plan: AnchorPlan = { changes: [], added: 0, skipped: 0, lists: lists.length };
const newNamespace = (supplemental: boolean): string => {
const base = supplemental ? 'sref' : 'ref';
if (!namespaces.has(base)) { namespaces.add(base); return base; }
for (let suffix = 2; ; suffix += 1) {
const candidate = `${base}-list${suffix}`;
if (namespaces.has(candidate)) continue;
namespaces.add(candidate);
return candidate;
}
};

for (const list of lists) {
const existingNamespaces = new Set(list.items.flatMap(entry => {
const identity = entry.anchor ? referenceAnchor(entry.anchor.slice(1)) : undefined;
return identity ? [identity.namespace] : [];
}));
if (existingNamespaces.size > 1) {
plan.skipped += list.items.length;
continue;
}
const namespace = existingNamespaces.values().next().value
?? newNamespace(list.supplemental);
for (const entry of list.items) {
if (entry.anchor || list.duplicates.has(entry.number)) { plan.skipped += 1; continue; }
const id = `${namespace}-${entry.number}`;
if (ids.has(id) || usedNumbers.get(namespace)?.has(entry.number)) { plan.skipped += 1; continue; }
// Detection ends at the final content character, before CRLF or trailing spaces.
plan.changes.push({ from: entry.to, to: entry.to, insert: ` ^${id}` });
ids.add(id);
const numbers = usedNumbers.get(namespace) ?? new Set<number>();
numbers.add(entry.number);
usedNumbers.set(namespace, numbers);
plan.added += 1;
}
}
plan.changes.sort((left, right) => left.from - right.from);
return plan;
}
2 changes: 1 addition & 1 deletion src/bibliography.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export function ignoredMarkdownRanges(source: string, lines = sourceLines(source
}

/** Only explicit reference block IDs establish a bibliography namespace. */
function referenceAnchor(id: string): { number: number; namespace: string } | undefined {
export function referenceAnchor(id: string): { number: number; namespace: string } | undefined {
const match = /^(s?ref)-?([1-9]\d{0,8})$/i.exec(id)
?? /^((?:s?ref)(?:-[A-Za-z][A-Za-z0-9]*)+)-([1-9]\d{0,8})$/i.exec(id);
if (!match) return undefined;
Expand Down
8 changes: 8 additions & 0 deletions src/citation-state.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { CitationGroup } from './types';

/** Expanded preview numbers are deduplicated; native hover applies to one displayed linked token only. */
export function usesNativeSinglePreview(group: CitationGroup): boolean {
if (!group.linked) return false;
if (group.tokens) return group.tokens.length === 1 && group.tokens[0].linked;
return group.numbers.length === 1;
}
Loading