Skip to content

feat: support config helper multi keys if supported #2571

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 22, 2025
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: 4 additions & 0 deletions docs/helpers.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ You will get `<a href="/demo/">link</a>`html. Do not worry, you can still set th

```md
![logo](https://docsify.js.org/_media/icon.svg ':class=someCssClass')

<!-- Multiple class names -->

![logo](https://docsify.js.org/_media/icon.svg ':class=someCssClass :class=anotherCssClass')
```

### IDs
Expand Down
6 changes: 5 additions & 1 deletion src/core/render/compiler/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ export const imageCompiler = ({ renderer, contentBase, router }) =>
}

if (config.class) {
attrs.push(`class="${config.class}"`);
let classes = config.class;
if (Array.isArray(config.class)) {
classes = config.class.join(' ');
}
attrs.push(`class="${classes}"`);
}

if (config.id) {
Expand Down
6 changes: 5 additions & 1 deletion src/core/render/compiler/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ export const linkCompiler = ({
}

if (config.class) {
attrs.push(`class="${config.class}"`);
let classes = config.class;
if (Array.isArray(config.class)) {
classes = config.class.join(' ');
}
attrs.push(`class="${classes}"`);
}

if (config.id) {
Expand Down
17 changes: 13 additions & 4 deletions src/core/render/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,21 @@ export function getAndRemoveConfig(str = '') {
.replace(/^('|")/, '')
.replace(/('|")$/, '')
.replace(/(?:^|\s):([\w-]+:?)=?([\w-%]+)?/g, (m, key, value) => {
if (key.indexOf(':') === -1) {
config[key] = (value && value.replace(/&quot;/g, '')) || true;
return '';
if (key.indexOf(':') !== -1) {
return m;
}

return m;
value = (value && value.replace(/&quot;/g, '')) || true;

if (value !== true && config[key] !== undefined) {
if (!Array.isArray(config[key]) && value !== config[key]) {
config[key] = [config[key]];
}
config[key].includes(value) || config[key].push(value);
} else {
config[key] = value;
}
return '';
})
.trim();
}
Expand Down
10 changes: 10 additions & 0 deletions test/integration/render.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,16 @@ describe('render', function () {
);
});

test('multi class config', async function () {
const output = window.marked(
"[alt text](http://url ':class=someCssClass :class=anotherCssClass')",
);

expect(output).toMatchInlineSnapshot(
`"<p><a href="http://url" target="_blank" rel="noopener" class="someCssClass anotherCssClass">alt text</a></p>"`,
);
});

test('id', async function () {
const output = window.marked("[alt text](http://url ':id=someCssID')");

Expand Down
39 changes: 39 additions & 0 deletions test/unit/render-util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,45 @@ describe('core/render/utils', () => {
});
});

test('parse config with key arguments img', () => {
const result = getAndRemoveConfig(
"![logo](https://docsify.js.org/_media/icon.svg ' :size=50x100 ')",
);

expect(result).toMatchObject({
config: {
size: '50x100',
},
str: "![logo](https://docsify.js.org/_media/icon.svg ' ')",
});
});

test('parse config with key arguments', () => {
const result = getAndRemoveConfig(
"[filename](_media/example.md ' :class=foo ')",
);

expect(result).toMatchObject({
config: {
class: 'foo',
},
str: "[filename](_media/example.md ' ')",
});
});

test('parse config with same key arguments', () => {
const result = getAndRemoveConfig(
"[filename](_media/example.md ' :class=foo :class=bar :bb=aa ')",
);

expect(result).toMatchObject({
config: {
class: ['foo', 'bar'],
},
str: "[filename](_media/example.md ' ')",
});
});

test('parse config with double quotes', () => {
const result = getAndRemoveConfig(
'[filename](_media/example.md ":include")',
Expand Down
Loading