-
Notifications
You must be signed in to change notification settings - Fork 79
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
feat: [zenn-markdown-html] VS Code 拡張機能でのスクロール同期のためのソースマップ追加 #504
Merged
cm-igarashi-ryosuke
merged 3 commits into
zenn-dev:canary
from
senkenn:feat/add-source-map
Aug 15, 2024
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,73 +51,63 @@ describe('Linkifyのテスト', () => { | |
|
||
test('URLの前にテキストが存在する場合はリンクをリンクカードに変換しない', () => { | ||
const html = renderLink('foo https://example.com'); | ||
expect(html).toEqual( | ||
'<p>foo <a href="https://example.com" target="_blank" rel="nofollow noopener noreferrer">https://example.com</a></p>\n' | ||
expect(html).toContain( | ||
'foo <a href="https://example.com" target="_blank" rel="nofollow noopener noreferrer">https://example.com</a>' | ||
); | ||
}); | ||
|
||
test('意図的にリンクしているURLはリンクカードに変換しない', () => { | ||
const html = renderLink('[https://example.com](https://example.com)'); | ||
expect(html).toEqual( | ||
'<p><a href="https://example.com" target="_blank" rel="nofollow noopener noreferrer">https://example.com</a></p>\n' | ||
); | ||
}); | ||
|
||
test('リンク内のリンクを変換しない', () => { | ||
const html = renderLink('- https://example.com\n- second'); | ||
expect(html).toEqual( | ||
'<ul>\n<li><a href="https://example.com" target="_blank" rel="nofollow noopener noreferrer">https://example.com</a></li>\n<li>second</li>\n</ul>\n' | ||
expect(html).toContain( | ||
'<a href="https://example.com" target="_blank" rel="nofollow noopener noreferrer">https://example.com</a>' | ||
); | ||
}); | ||
|
||
test('<details />内のリンクはリンクカードに変換しない', () => { | ||
const html = renderLink(':::message alert\nhttps://example.com\n:::'); | ||
expect(html).toEqual( | ||
'<aside class="msg alert"><span class="msg-symbol">!</span><div class="msg-content"><p><a href="https://example.com" target="_blank" rel="nofollow noopener noreferrer">https://example.com</a></p>\n</div></aside>\n' | ||
); | ||
const iframe = parse(html).querySelector('span.zenn-embedded iframe'); | ||
expect(iframe).toBeNull(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. iframeが存在しないことを確認するのはいい作戦と思いましたが、ここのテストケースの意図としては「details内のリンクが、リンクカードには変換されずリンクになる」ということを確認したいのだと思いました。 なので期待値としては以下を確認するようにしていただけますでしょうか?
|
||
}); | ||
|
||
test('<details />内の2段落空いたリンクをリンクカードに変換しない', () => { | ||
const html = renderLink( | ||
':::message alert\nhello\n\nhttps://example.com\n:::' | ||
); | ||
expect(html).toContain( | ||
'<aside class="msg alert"><span class="msg-symbol">!</span><div class="msg-content"><p>hello</p>\n<p><a href="https://example.com" target="_blank" rel="nofollow noopener noreferrer">https://example.com</a></p>\n</div></aside>' | ||
); | ||
const iframes = parse(html).querySelectorAll('span.zenn-embedded iframe'); | ||
expect(iframes.length).toBe(0); | ||
}); | ||
|
||
test('リスト内のリンクをリンクカードに変換しない', () => { | ||
const html = renderLink('- https://example.com\n- second'); | ||
expect(html).toEqual( | ||
'<ul>\n<li><a href="https://example.com" target="_blank" rel="nofollow noopener noreferrer">https://example.com</a></li>\n<li>second</li>\n</ul>\n' | ||
); | ||
const iframe = parse(html).querySelector('span.zenn-embedded iframe'); | ||
expect(iframe).toBeNull(); | ||
}); | ||
|
||
test('URLにテキストが続く場合はリンクカードに変換しない', () => { | ||
const html = renderLink('https://example.com foo'); | ||
expect(html).toEqual( | ||
'<p><a href="https://example.com" target="_blank" rel="nofollow noopener noreferrer">https://example.com</a> foo</p>\n' | ||
expect(html).toContain( | ||
'<a href="https://example.com" target="_blank" rel="nofollow noopener noreferrer">https://example.com</a> foo' | ||
); | ||
}); | ||
|
||
test('同じ段落内のテキストを含むリンクをリンクカードに変換しない', () => { | ||
const html = renderLink(`a: https://example.com\nb: https://example.com`); | ||
expect(html).toEqual( | ||
'<p>a: <a href="https://example.com" target="_blank" rel="nofollow noopener noreferrer">https://example.com</a><br />\nb: <a href="https://example.com" target="_blank" rel="nofollow noopener noreferrer">https://example.com</a></p>\n' | ||
expect(html).toContain( | ||
'a: <a href="https://example.com" target="_blank" rel="nofollow noopener noreferrer">https://example.com</a><br />\nb: <a href="https://example.com" target="_blank" rel="nofollow noopener noreferrer">https://example.com</a>' | ||
); | ||
}); | ||
|
||
test('URLにテキストが続くならリンクが先頭であってもリンクカードに変換しない', () => { | ||
const html = renderLink('\n\nhttps://example.com text'); | ||
expect(html).toEqual( | ||
'<p><a href="https://example.com" target="_blank" rel="nofollow noopener noreferrer">https://example.com</a> text</p>\n' | ||
expect(html).toContain( | ||
'<a href="https://example.com" target="_blank" rel="nofollow noopener noreferrer">https://example.com</a> text' | ||
); | ||
}); | ||
|
||
test('URLの前にテキストがあるならリンクが行末でもリンクカードに変換しない', () => { | ||
const html = renderLink('text https://example.com\n\n'); | ||
expect(html).toEqual( | ||
'<p>text <a href="https://example.com" target="_blank" rel="nofollow noopener noreferrer">https://example.com</a></p>\n' | ||
expect(html).toContain( | ||
'text <a href="https://example.com" target="_blank" rel="nofollow noopener noreferrer">https://example.com</a>' | ||
); | ||
}); | ||
}); | ||
|
127 changes: 127 additions & 0 deletions
127
packages/zenn-markdown-html/__tests__/source-map.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import { describe, test, expect } from "vitest"; | ||
import markdownToHtml from "../src/index"; | ||
import parse from "node-html-parser"; | ||
|
||
describe("ソースマップ(data-line属性)のテスト", () => { | ||
test("Header", () => { | ||
const html = markdownToHtml(`# Header1 | ||
## Header2 | ||
### Header3 | ||
#### Header4 | ||
##### Header5 | ||
###### Header6`); | ||
const h1 = parse(html).querySelector("h1"); | ||
const h2 = parse(html).querySelector("h2"); | ||
const h3 = parse(html).querySelector("h3"); | ||
const h4 = parse(html).querySelector("h4"); | ||
const h5 = parse(html).querySelector("h5"); | ||
const h6 = parse(html).querySelector("h6"); | ||
expect(h1?.getAttribute("data-line")).toEqual("0"); | ||
expect(h1?.classList.contains("code-line")).toBe(true); | ||
expect(h2?.getAttribute("data-line")).toEqual("1"); | ||
expect(h2?.classList.contains("code-line")).toBe(true); | ||
expect(h3?.getAttribute("data-line")).toEqual("2"); | ||
expect(h3?.classList.contains("code-line")).toBe(true); | ||
expect(h4?.getAttribute("data-line")).toEqual("3"); | ||
expect(h4?.classList.contains("code-line")).toBe(true); | ||
expect(h5?.getAttribute("data-line")).toEqual("4"); | ||
expect(h5?.classList.contains("code-line")).toBe(true); | ||
expect(h6?.getAttribute("data-line")).toEqual("5"); | ||
expect(h6?.classList.contains("code-line")).toBe(true); | ||
}); | ||
|
||
test("Paragraph", () => { | ||
const html = markdownToHtml(`Paragraph1\n\nhttps://example.com`); | ||
const p = parse(html).querySelectorAll("p"); | ||
expect(p?.[0].getAttribute("data-line")).toEqual("0"); | ||
expect(p?.[0].classList.contains("code-line")).toBe(true); | ||
expect(p?.[1].getAttribute("data-line")).toEqual("2"); | ||
expect(p?.[1].classList.contains("code-line")).toBe(true); | ||
}); | ||
|
||
test("List(unordered)", () => { | ||
const html = markdownToHtml(`- item1\n- item2`); | ||
const ul = parse(html).querySelector("ul"); | ||
const li = parse(html).querySelectorAll("li"); | ||
expect(ul?.getAttribute("data-line")).toEqual("0"); | ||
expect(ul?.classList.contains("code-line")).toBe(true); | ||
expect(li?.[0].getAttribute("data-line")).toEqual("0"); | ||
expect(li?.[0].classList.contains("code-line")).toBe(true); | ||
expect(li?.[1].getAttribute("data-line")).toEqual("1"); | ||
expect(li?.[1].classList.contains("code-line")).toBe(true); | ||
}); | ||
|
||
test("List(ordered)", () => { | ||
const html = markdownToHtml(`1. item1\n2. item2`); | ||
const ol = parse(html).querySelector("ol"); | ||
const li = parse(html).querySelectorAll("li"); | ||
expect(ol?.getAttribute("data-line")).toEqual("0"); | ||
expect(ol?.classList.contains("code-line")).toBe(true); | ||
expect(li?.[0].getAttribute("data-line")).toEqual("0"); | ||
expect(li?.[0].classList.contains("code-line")).toBe(true); | ||
expect(li?.[1].getAttribute("data-line")).toEqual("1"); | ||
expect(li?.[1].classList.contains("code-line")).toBe(true); | ||
}); | ||
|
||
test("Table", () => { | ||
const html = markdownToHtml(`| a | b |\n| --- | --- |\n| c | d |`); | ||
const table = parse(html).querySelector("table"); | ||
const thead = parse(html).querySelector("thead"); | ||
const tbody = parse(html).querySelector("tbody"); | ||
const tr = parse(html).querySelectorAll("tr"); | ||
expect(table?.getAttribute("data-line")).toEqual("0"); | ||
expect(table?.classList.contains("code-line")).toBe(true); | ||
expect(thead?.getAttribute("data-line")).toEqual("0"); | ||
expect(thead?.classList.contains("code-line")).toBe(true); | ||
expect(tbody?.getAttribute("data-line")).toEqual("2"); | ||
expect(tbody?.classList.contains("code-line")).toBe(true); | ||
expect(tr?.[0].getAttribute("data-line")).toEqual("0"); | ||
expect(tr?.[0].classList.contains("code-line")).toBe(true); | ||
expect(tr?.[1].getAttribute("data-line")).toEqual("2"); | ||
expect(tr?.[1].classList.contains("code-line")).toBe(true); | ||
}); | ||
|
||
test("Code Block", () => { | ||
const html = markdownToHtml("```\ncode\n```"); | ||
// <code />が取得できないので<pre />で取得する | ||
const innerHTML: any = parse(html).querySelector("pre")?.innerHTML; | ||
const code = parse(innerHTML).querySelector("code"); | ||
expect(code?.getAttribute("data-line")).toEqual("0"); // フェンス開始時の行番号 | ||
expect(code?.classList.contains("code-line")).toBe(true); | ||
}); | ||
|
||
test("Katex", () => { | ||
const html = markdownToHtml(`$$\na\n$$`); | ||
const katex = parse(html).querySelector("section"); | ||
expect(katex?.getAttribute("data-line")).toEqual("0"); | ||
expect(katex?.classList.contains("code-line")).toBe(true); | ||
}); | ||
|
||
test("Blockquote", () => { | ||
const html = markdownToHtml("> quote"); | ||
const blockquote = parse(html).querySelector("blockquote"); | ||
expect(blockquote?.getAttribute("data-line")).toEqual("0"); | ||
expect(blockquote?.classList.contains("code-line")).toBe(true); | ||
}); | ||
|
||
test("Horizontal Rule", () => { | ||
const html = markdownToHtml(`---`); | ||
const hr = parse(html).querySelector("hr"); | ||
expect(hr?.getAttribute("data-line")).toEqual("0"); | ||
expect(hr?.classList.contains("code-line")).toBe(true); | ||
}); | ||
|
||
test("Alert", () => { | ||
const html = markdownToHtml(":::message\nhello\n:::"); | ||
const p = parse(html).querySelector("p"); | ||
expect(p?.getAttribute("data-line")).toEqual("1"); | ||
expect(p?.classList.contains("code-line")).toBe(true); | ||
}); | ||
|
||
test("Details/Summary", () => { | ||
const html = markdownToHtml(`:::details タイトル\nhello\n:::`); | ||
const p = parse(html).querySelector("p"); | ||
expect(p?.getAttribute("data-line")).toEqual("1"); | ||
expect(p?.classList.contains("code-line")).toBe(true); | ||
}); | ||
}); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[memo] テストケースがL89と重複しているため削除。