How to create autocompletion for ReadMe documents.
iunderstand you want to implement auto-completion for markdown links based on the "slug" values from the YAML frontmatter of files in the readme/ folder. While I can't directly modify Cursor or store information for other users, I can suggest an approach to achieve this functionality: Create a script to scan all markdown files in the readme/ folder and extract their slug values. Store these slug values in a JSON file or a simple database. Implement a custom extension or plugin for your text editor (in this case, Cursor) that reads this JSON file or database. Use this data to provide auto-completion suggestions when typing markdown links. Here's a sample script to extract and store the slug values:
const fs = require('fs');
const path = require('path');
const yaml = require('js-yaml');
const readmeDir = path.join(__dirname, 'readme');
const slugs = [];
function extractSlugFromFile(filePath) {
const content = fs.readFileSync(filePath, 'utf8');
const match = content.match(/^---\n([\s\S]*?)\n---/);
if (match) {
const frontmatter = yaml.load(match[1]);
if (frontmatter && frontmatter.slug) {
slugs.push(frontmatter.slug);
}
}
}
function scanDirectory(dir) {
const files = fs.readdirSync(dir);
files.forEach(file => {
const filePath = path.join(dir, file);
const stat = fs.statSync(filePath);
if (stat.isDirectory()) {
scanDirectory(filePath);
} else if (path.extname(file) === '.md') {
extractSlugFromFile(filePath);
}
});
}
scanDirectory(readmeDir);
fs.writeFileSync('slugs.json', JSON.stringify(slugs, null, 2));
console.log('Slugs extracted and saved to slugs.json');
This script will create a slugs.json file containing all the slug values. You can then use this file as a source for auto-completion in your editor. For Cursor specifically, you might need to develop a custom extension that reads this JSON file and provides auto-completion suggestions. The exact implementation would depend on Cursor's extension API. As for sharing this functionality with other Cursor users, you could: Package this as a Cursor extension and publish it to the Cursor marketplace (if one exists). Share the script and instructions for setting up the auto-completion in your project's documentation. Remember to update the slugs.json file whenever you add or modify markdown files in your project. Can you give me the outline or a cursor extension?
Here's an outline for a Cursor extension that could provide auto-completion for markdown links based on slug values:
// cursor-extension.ts
import * as vscode from 'vscode';
import * as fs from 'fs';
import * as path from 'path';
// Load slugs from JSON file
function loadSlugs(): string[] {
const slugsPath = path.join(__dirname, 'slugs.json');
const slugsContent = fs.readFileSync(slugsPath, 'utf-8');
return JSON.parse(slugsContent);
}
// Provide completion items
function provideCompletionItems(document: vscode.TextDocument, position: vscode.Position) {
const linePrefix = document.lineAt(position).text.substr(0, position.character);
if (!linePrefix.endsWith('](doc:')) {
return undefined;
}
const slugs = loadSlugs();
return slugs.map(slug => new vscode.CompletionItem(slug, vscode.CompletionItemKind.Value));
}
// Activation function
export function activate(context: vscode.ExtensionContext) {
const provider = vscode.languages.registerCompletionItemProvider(
'markdown',
{
provideCompletionItems
},
':' // Trigger character
);
context.subscriptions.push(provider);
}
// Deactivation function
export function deactivate() {}
This outline includes: A function to load slugs from a JSON file. A function to provide completion items when the user types ](doc:. Activation and deactivation functions for the extension. To turn this into a full Cursor extension, you would need to:
- Set up a new extension project using Cursor's extension development tools (if available). Implement the extension manifest (usually package.json) to define the extension's metadata and activation events. Implement the main extension file (as outlined above). Package and publish the extension according to Cursor's guidelines. Note that this outline assumes Cursor uses a VS Code-like extension API. If Cursor has a different extension system, you'd need to adapt this code to fit their specific API and requirements. Also, you'd need to ensure that the slugs.json file is updated whenever the markdown files in your project change. You could do this by: Running the slug extraction script as part of your build process. Creating a file watcher in the extension to update the slugs in real-time. Providing a command in the extension to manually refresh the slugs. Remember to check Cursor's documentation for specific details on creating and publishing extensions, as the process may differ from other editors.