NOTE

This feature was discontinued because it was used to quickly navigate to the (edit) page when the website was still hosted and updated on WordPress. Now that everything is migrated to Google App Engine, there is no use for this anymore in its current form.

Feature description

Each article has metadata at the top of the file which contains the URL and the Edit URL of the article. Using a regex, it is possible to extract that URL and open it in a browser window.

export function openUrl() {
    let metadata = lib.getMetadataFromCurrentFile()
 
    vscode.env.openExternal(vscode.Uri.parse(metadata.url))
}

Originally this metadata was in a custom format which looked like

 <!--Metadata
 URL: <url>
 Edit URL: <edit-url>
 -->

but it turns out Markdown already has supported methods of adding metadata to a file, which has taken inspiration from YAML and can contain any information you want

---
url: <url>
edit-url: <edit-url>
---

There are two reasons for using this format:

  • The metadata can be used in 🧰 Pandoc templates
  • Once the metadata is located within the file, no more custom processing is required

Removed code

extension.ts
vscode.commands.registerCommand('blogging.openUrl', functions.openUrl),
vscode.commands.registerCommand('blogging.openEditUrl', functions.openEditUrl),
functions.ts
export function openUrl() {
    let metadata = getMetadataFromCurrentFile();
 
    vscode.env.openExternal(vscode.Uri.parse(metadata.url));
}
 
export function openEditUrl() {
    let metadata = getMetadataFromCurrentFile();
 
    vscode.env.openExternal(vscode.Uri.parse(metadata.editUrl));
}
 
export function openUrl() {
    let metadata = getMetadataFromCurrentFile();
 
    vscode.env.openExternal(vscode.Uri.parse(metadata.url));
}
 
export function openEditUrl() {
    let metadata = getMetadataFromCurrentFile();
 
    vscode.env.openExternal(vscode.Uri.parse(metadata.editUrl));
}
lib.ts
/**
 * Parse the metadata from a string in the form of
 * <!--Metadata
 * URL: (...)
 * Edit URL: (...)
 * ...
 * -->
 * @returns an object containing the parsed metadata
 */
export function parseMetadata(text: String) {
    const metadataRegex = /^---\n(.*?)\n---$/ms;
    const result = text.match(metadataRegex)!![1];
 
    return parseMetadataLines(result);
}
 
/**
 * Extract each line from a metadata string. For example:
 * 'Edit URL: test-url'
 * 
 * will be returned as
 * {
 *   'editUrl': 'test-url'
 * }
 * 
 * @returns an object containing the parsed metadata
 */
function parseMetadataLines(metadata: String) {
    let urlRegex = /^URL: (.*?)$/gms;
    let url = regexMatch(urlRegex, metadata)[0];
 
    let editUrlRegex = /^Edit URL: (.*?)$/gms;
    let editUrl = regexMatch(editUrlRegex, metadata)[0];
    return {
        'url': url,
        'editUrl': editUrl
    };
}
lib.test.ts
test('parse article metadata', () => {
    let content = lib.dedent`---
                                URL: test-url
                                Edit URL: edit-url
                                ---
 
                                URL: some-random-unimportant-url`;
    
    let metadata = lib.parseMetadata(content);
    assert.equal(metadata.url, 'test-url');
    assert.equal(metadata.editUrl, 'edit-url');
});