Skip to content
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
12 changes: 12 additions & 0 deletions packages/zenn-markdown-html/__tests__/highlight.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,16 @@ describe('コードハイライトのテスト', () => {
'<span class="code-block-filename">index.html</span>'
);
});
test('":" が含まれるファイル名が表示される', () => {
const jsString = [
'```js:index:withcolons.js',
"console.log('foo')",
'```',
].join('\n');
const html = markdownToHtml(jsString);
expect(html).toContain(
'<span class="code-block-filename">index:withcolons.js</span>'
);
expect(html).toContain('language-js');
});
});
6 changes: 5 additions & 1 deletion packages/zenn-markdown-html/src/utils/md-renderer-fence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,11 @@ export function parseInfo(str: string): {

// e.g. foo:filename => ["foo", "filename"]
// e.g. foo diff:filename => ["foo diff", "filename"]
const [langInfo, fileName] = str.split(':');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

当初は str.split(':', 2); のような書き方で修正できるかと思ったのですが、そのような仕様ではなかったようなので左端のコロンを探して分割するように修正しました
https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/String/split#limit

// e.g. foo:filename:bar => ["foo", "filename:bar"]
const separatorIndex = str.indexOf(':');
const langInfo = separatorIndex > -1 ? str.substring(0, separatorIndex) : str;
const fileName =
separatorIndex > -1 ? str.substring(separatorIndex + 1) : undefined;

const langNames = langInfo.split(' ');
const hasDiff = langNames.some((name) => name === 'diff');
Expand Down
Loading