Skip to content

Commit

Permalink
fix: remove trailing hyphen from generated id (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
ignatiusmb committed Mar 8, 2024
1 parent b9acd9d commit 60475df
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 21 deletions.
4 changes: 2 additions & 2 deletions workspace/aubade/src/artisan/marker.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import markdown from 'markdown-it';
import { scope } from 'mauss';
import { generate } from '../utils.js';
import { uhi } from '../utils.js';
import { transform } from './brush.js';

export const marker = markdown({
Expand Down Expand Up @@ -30,7 +30,7 @@ marker.renderer.rules.heading_open = scope(() => {
const level = +token.tag.slice(-1);
if (level > 4) return `<${token.tag}>`;
const [delimited] = text.match(/\$\(.*?\)/) || [''];
const id = generate.id(delimited.slice(2, -1) || text);
const id = uhi(delimited.slice(2, -1) || text);

if (level === 2) parents = [id];
if (level === 3) parents = [parents[0], id];
Expand Down
4 changes: 2 additions & 2 deletions workspace/aubade/src/core/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { generate } from '../utils.js';
import { uhi } from '../utils.js';

/** @param {string} source */
export function parse(source) {
Expand Down Expand Up @@ -37,7 +37,7 @@ export function parse(source) {
const [delimited] = title.match(/\$\(.*?\)/) || [''];

table.push({
id: generate.id(delimited.slice(2, -1) || title),
id: uhi(delimited.slice(2, -1) || title),
title: title.replace(delimited, delimited.slice(2, -1)),
level: hashes.length,
});
Expand Down
24 changes: 7 additions & 17 deletions workspace/aubade/src/utils.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,14 @@
const separators = /[\s\][!"#$%&'()*+,./:;<=>?@\\^_{|}~-]/g;

export const generate = {
/**
* @param {'clipboard' | 'list'} name
* @param {string} tooltip
*/
icon(name, tooltip) {
const span = `<span data-mrq="tooltip" class="mrq">${tooltip}</span>`;
return `<button data-mrq-toolbar="${name}" class="mrq">${span}</button>`;
},
/** @param {string} title */
id(title) {
title = title.toLowerCase().replace(separators, '-');
title = title.replace(/`/g, '').replace(/-+/g, '-');
return title.replace(/^-*(.+)-*$/, '$1');
},
};

/** @param {string} source */
export function escape(source) {
const symbols = { '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' };
return source.replace(/[&<>"']/g, (s) => symbols[/** @type {keyof typeof symbols} */ (s)]);
}

/** @param {string} title */
export function uhi(title) {
const cleaned = title.toLowerCase().replace(separators, '-');
const normalized = cleaned.replace(/`/g, '').replace(/-+/g, '-');
return normalized.replace(/^-*(.+?)-*$/, '$1'); // hyphen at the sides
}

0 comments on commit 60475df

Please sign in to comment.