-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
172 additions
and
6 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { inBrowser } from 'vitepress' | ||
|
||
export function useCodeModal() { | ||
if (inBrowser) { | ||
window.addEventListener('click', (e) => { | ||
const el = e.target as HTMLElement | ||
|
||
if (el.matches('div[class*="language-"] > button.modal')) { | ||
const parent = el.parentElement | ||
const sibling = el.nextElementSibling | ||
if (!parent || !sibling) { | ||
return | ||
} | ||
|
||
sibling.classList.add('open') | ||
} | ||
|
||
if (el.matches('div[class*="language-"] > div.modal-container')) { | ||
el.classList.remove('open') | ||
} | ||
}) | ||
|
||
window.addEventListener('keydown', (ev) => { | ||
if (ev.key == 'Escape') { | ||
let modal = window.document.querySelector( | ||
'div[class*="language-"] > div.modal-container.open' | ||
) | ||
|
||
if (!modal) { | ||
return | ||
} | ||
|
||
modal.classList.remove('open') | ||
} | ||
}) | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// markdown-it plugin for generating line numbers. | ||
// It depends on preWrapper plugin. | ||
|
||
import type MarkdownIt from 'markdown-it' | ||
import type { MarkdownOptions } from '../markdown' | ||
|
||
export const codeModalPlugin = ( | ||
md: MarkdownIt, | ||
enable = false, | ||
options: MarkdownOptions = {} | ||
) => { | ||
const fence = md.renderer.rules.fence! | ||
md.renderer.rules.fence = (...args) => { | ||
const rawCode = fence(...args) | ||
|
||
const [tokens, idx] = args | ||
const info = tokens[idx].info | ||
|
||
if ( | ||
(!enable && !/:modal($| |=)/.test(info)) || | ||
(enable && /:no-modal($| )/.test(info)) | ||
) { | ||
return rawCode | ||
} | ||
|
||
const code = | ||
`<button title="${options.codeModalButtonTitle}" class="modal"></button>` + | ||
'<div class="modal-container">' + | ||
fence(...args) + | ||
'</div>' | ||
|
||
let end = rawCode.lastIndexOf('</div>') | ||
|
||
return rawCode.substring(0, end) + code + '</div>' | ||
} | ||
} |
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