feat(clean-references): add reference cleanup service with colon formatting

- Implement cleanReferencesSectionService with addColonSyntaxWhereNone function
- Add command to format footnote references with consistent colon syntax
- Fix regex to handle various footnote reference formats
- Preserve leading whitespace and ensure single space after colon
- Add TypeScript type safety with proper null checks
- Integrate with main plugin command registry

The new command formats footnote references from:
  [^hexId] Text...
to:
  [^hexId]: Text...

This improves consistency in citation formatting throughout documents.
 On branch development
 Changes to be committed:
	modified:   main.ts
	new file:   src/services/cleanReferencesSectionService.ts
This commit is contained in:
mpstaton 2025-07-08 19:24:11 +03:00
parent 39945546a1
commit 3d0eb2ac06
2 changed files with 86 additions and 0 deletions

21
main.ts
View file

@ -1,6 +1,7 @@
import { Notice, Plugin, Editor } from 'obsidian';
import { citationService } from './src/services/citationService';
import { CitationModal } from './src/modals/CitationModal';
import { cleanReferencesSectionService } from './src/services/cleanReferencesSectionService';
export default class CiteWidePlugin extends Plugin {
async onload(): Promise<void> {
@ -9,6 +10,7 @@ export default class CiteWidePlugin extends Plugin {
// Register commands
this.registerCitationCommands();
this.registerReferenceCleanupCommands();
}
private async loadStyles() {
@ -106,4 +108,23 @@ export default class CiteWidePlugin extends Plugin {
}
});
}
private registerReferenceCleanupCommands(): void {
// Command to clean up references section
this.addCommand({
id: 'clean-references-section',
name: 'Add Colon to Footnote References in Selection',
editorCallback: (editor: Editor) => {
const selection = editor.getSelection();
if (!selection) {
new Notice('Please select some text first');
return;
}
const processed = cleanReferencesSectionService.addColonSyntaxWhereNone(selection);
editor.replaceSelection(processed);
new Notice('References cleaned up successfully');
}
});
}
}

View file

@ -0,0 +1,65 @@
/**
* Service for cleaning up and normalizing references sections in markdown content
*/
/**
* Adds colon syntax to footnote references that are missing it
* @param text The text to process
* @returns The processed text with colons added to footnote references
*/
export function addColonSyntaxWhereNone(text: string): string {
if (!text) return text;
// Split the text into lines and process each line
return text.split('\n').map(line => {
// Match footnote references that don't have a colon after the ID
// Format: [^hexId] Text...
const footnoteRegex = /^(\s*\[\^([a-z0-9]+)\])(?::?\s*)(.*)$/i;
const match = line.match(footnoteRegex);
if (match && match[1] && match[2] && match[3] !== undefined) {
const leadingSpaceMatch = match[1].match(/^\s*/);
const leadingSpace = leadingSpaceMatch ? leadingSpaceMatch[0] : '';
const id = match[2];
const content = match[3].trimStart();
// Preserve leading space and ensure exactly one space after the colon
return `${leadingSpace}[^${id}]: ${content}`;
}
return line;
}).join('\n');
}
/**
* Processes the current selection in the active editor to add colon syntax to footnotes
* @returns A promise that resolves when the operation is complete
*/
export async function processSelection(): Promise<void> {
// This function would be called from a command or UI component
// that has access to the Obsidian editor instance
const editor = (window as any).activeEditor?.editor;
if (!editor) {
console.warn('No active editor found');
return;
}
const selection = editor.getSelection();
if (!selection) {
console.warn('No text selected');
return;
}
// Process the selected text
const processedText = addColonSyntaxWhereNone(selection);
// Replace the selection with the processed text
editor.replaceSelection(processedText);
}
export const cleanReferencesSectionService = {
addColonSyntaxWhereNone,
processSelection
};
export default cleanReferencesSectionService;