Browse Source

finish asciidoc parser

master
Silberengel 2 weeks ago
parent
commit
48b25cb39d
  1. 4
      asciidoc_testdoc.adoc
  2. 12
      src/post-processor.ts

4
asciidoc_testdoc.adoc

@ -318,10 +318,6 @@ stop
`$[ x^n + y^n = z^n \]$` and `$[\sqrt{x^2+1}\]$` and `$\color{blue}{X \sim Normal \; (\mu,\sigma^2)}$` `$[ x^n + y^n = z^n \]$` and `$[\sqrt{x^2+1}\]$` and `$\color{blue}{X \sim Normal \; (\mu,\sigma^2)}$`
== LaTeX outside of code
This is a latex code block $$\mathbb{N} = \{ a \in \mathbb{Z} : a > 0 \}$$ and another that is an inline latex $\color{green}{X \sim Normal \; (\mu,\sigma^2)}$ and should be green
== Footnotes == Footnotes
Here's a simple footnote,footnote:[This is the first footnote.] and here's a longer one.footnote:[Here's one with multiple paragraphs and code.] Here's a simple footnote,footnote:[This is the first footnote.] and here's a longer one.footnote:[Here's one with multiple paragraphs and code.]

12
src/post-processor.ts

@ -208,7 +208,7 @@ export function postProcess(html: string, options: ParserOptions, skipWikilinksA
const youtubeVideoTagRegex = /<video[^>]+src="(https?:\/\/(?:www\.)?(?:youtube\.com\/(?:watch\?v=|shorts\/)|youtu\.be\/)([a-zA-Z0-9_-]+))"[^>]*>[\s\S]*?<\/video>/gi; const youtubeVideoTagRegex = /<video[^>]+src="(https?:\/\/(?:www\.)?(?:youtube\.com\/(?:watch\?v=|shorts\/)|youtu\.be\/)([a-zA-Z0-9_-]+))"[^>]*>[\s\S]*?<\/video>/gi;
processed = processed.replace(youtubeVideoTagRegex, (match, url, videoId) => { processed = processed.replace(youtubeVideoTagRegex, (match, url, videoId) => {
const embedUrl = `https://www.youtube.com/embed/${videoId}?enablejsapi=1`; const embedUrl = `https://www.youtube.com/embed/${videoId}?enablejsapi=1`;
return `<iframe class="youtube-embed" frameborder="0" allowfullscreen allow="accelerometer; autoplay; clipboard-write; encrypted-media; fullscreen; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" width="100%" height="360" src="${escapeHtml(embedUrl)}"></iframe>`; return `<iframe class="youtube-embed" frameborder="0" allow="encrypted-media; fullscreen; picture-in-picture; web-share" referrerpolicy='strict-origin-when-cross-origin' width="100%" height="360" src="${escapeHtml(embedUrl)}"></iframe>`;
}); });
// 2. SECOND: Process YouTube links in <a> tags // 2. SECOND: Process YouTube links in <a> tags
@ -217,7 +217,7 @@ export function postProcess(html: string, options: ParserOptions, skipWikilinksA
processed = processed.replace(youtubeLinkRegex, (match, url, videoId) => { processed = processed.replace(youtubeLinkRegex, (match, url, videoId) => {
if (isInCodeBlock(processed.indexOf(match))) return match; if (isInCodeBlock(processed.indexOf(match))) return match;
const embedUrl = `https://www.youtube.com/embed/${videoId}?enablejsapi=1`; const embedUrl = `https://www.youtube.com/embed/${videoId}?enablejsapi=1`;
return `<iframe class="youtube-embed" frameborder="0" allowfullscreen allow="accelerometer; autoplay; clipboard-write; encrypted-media; fullscreen; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" width="100%" height="360" src="${escapeHtml(embedUrl)}"></iframe>`; return `<iframe class="youtube-embed" frameborder="0" allow="encrypted-media; fullscreen; picture-in-picture; web-share" referrerpolicy='strict-origin-when-cross-origin' width="100%" height="360" src="${escapeHtml(embedUrl)}"></iframe>`;
}); });
// 3. THIRD: Fix malformed YouTube iframes from AsciiDoc video:: macro // 3. THIRD: Fix malformed YouTube iframes from AsciiDoc video:: macro
@ -226,7 +226,7 @@ export function postProcess(html: string, options: ParserOptions, skipWikilinksA
const malformedYoutubeIframeRegex = /<iframe[^>]+src="[^"]*youtube[^"]*(?:watch\?v=|shorts\/)([a-zA-Z0-9_-]+)[^"]*"[^>]*(?:\/>|>[\s\S]*?<\/iframe>)/gi; const malformedYoutubeIframeRegex = /<iframe[^>]+src="[^"]*youtube[^"]*(?:watch\?v=|shorts\/)([a-zA-Z0-9_-]+)[^"]*"[^>]*(?:\/>|>[\s\S]*?<\/iframe>)/gi;
processed = processed.replace(malformedYoutubeIframeRegex, (match, videoId) => { processed = processed.replace(malformedYoutubeIframeRegex, (match, videoId) => {
const embedUrl = `https://www.youtube.com/embed/${videoId}?enablejsapi=1`; const embedUrl = `https://www.youtube.com/embed/${videoId}?enablejsapi=1`;
return `<iframe class="youtube-embed" frameborder="0" allowfullscreen allow="accelerometer; autoplay; clipboard-write; encrypted-media; fullscreen; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" width="100%" height="360" src="${escapeHtml(embedUrl)}"></iframe>`; return `<iframe class="youtube-embed" frameborder="0" allow="encrypted-media; fullscreen; picture-in-picture; web-share" referrerpolicy='strict-origin-when-cross-origin' width="100%" height="360" src="${escapeHtml(embedUrl)}"></iframe>`;
}); });
// 3.5: Fix YouTube iframes with embed URLs but wrong parameters or missing required attributes // 3.5: Fix YouTube iframes with embed URLs but wrong parameters or missing required attributes
@ -243,7 +243,7 @@ export function postProcess(html: string, options: ParserOptions, skipWikilinksA
} }
// Fix the iframe with proper attributes // Fix the iframe with proper attributes
const embedUrl = `https://www.youtube.com/embed/${videoId}?enablejsapi=1`; const embedUrl = `https://www.youtube.com/embed/${videoId}?enablejsapi=1`;
return `<iframe class="youtube-embed" frameborder="0" allowfullscreen allow="accelerometer; autoplay; clipboard-write; encrypted-media; fullscreen; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" width="100%" height="360" src="${escapeHtml(embedUrl)}"></iframe>`; return `<iframe class="youtube-embed" frameborder="0" allow="encrypted-media; fullscreen; picture-in-picture; web-share" referrerpolicy='strict-origin-when-cross-origin' width="100%" height="360" src="${escapeHtml(embedUrl)}"></iframe>`;
}); });
// 4. FOURTH: Fix any existing YouTube iframes that have malformed embed URLs (AsciiDoc sometimes creates broken embed URLs) // 4. FOURTH: Fix any existing YouTube iframes that have malformed embed URLs (AsciiDoc sometimes creates broken embed URLs)
@ -251,7 +251,7 @@ export function postProcess(html: string, options: ParserOptions, skipWikilinksA
const brokenYoutubeIframeRegex = /<iframe[^>]+src="[^"]*youtube\.com\/embed\/[^"]*watch\?v=([a-zA-Z0-9_-]+)[^"]*"[^>]*(?:\/>|>[\s\S]*?<\/iframe>)/gi; const brokenYoutubeIframeRegex = /<iframe[^>]+src="[^"]*youtube\.com\/embed\/[^"]*watch\?v=([a-zA-Z0-9_-]+)[^"]*"[^>]*(?:\/>|>[\s\S]*?<\/iframe>)/gi;
processed = processed.replace(brokenYoutubeIframeRegex, (match, videoId) => { processed = processed.replace(brokenYoutubeIframeRegex, (match, videoId) => {
const embedUrl = `https://www.youtube.com/embed/${videoId}?enablejsapi=1`; const embedUrl = `https://www.youtube.com/embed/${videoId}?enablejsapi=1`;
return `<iframe class="youtube-embed" frameborder="0" allowfullscreen allow="accelerometer; autoplay; clipboard-write; encrypted-media; fullscreen; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" width="100%" height="360" src="${escapeHtml(embedUrl)}"></iframe>`; return `<iframe class="youtube-embed" frameborder="0" allow="encrypted-media; fullscreen; picture-in-picture; web-share" referrerpolicy='strict-origin-when-cross-origin' width="100%" height="360" src="${escapeHtml(embedUrl)}"></iframe>`;
}); });
// 5. LAST: Handle bare YouTube URLs (not in links, video tags, or iframes) // 5. LAST: Handle bare YouTube URLs (not in links, video tags, or iframes)
@ -345,7 +345,7 @@ export function postProcess(html: string, options: ParserOptions, skipWikilinksA
if (youtubeMatch) { if (youtubeMatch) {
const videoId = youtubeMatch[1]; const videoId = youtubeMatch[1];
const embedUrl = `https://www.youtube.com/embed/${videoId}?enablejsapi=1`; const embedUrl = `https://www.youtube.com/embed/${videoId}?enablejsapi=1`;
return `<iframe class="youtube-embed" frameborder="0" allowfullscreen allow="accelerometer; autoplay; clipboard-write; encrypted-media; fullscreen; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" width="100%" height="360" src="${escapeHtml(embedUrl)}"></iframe>`; return `<iframe class="youtube-embed" frameborder="0" allow="encrypted-media; fullscreen; picture-in-picture; web-share" referrerpolicy='strict-origin-when-cross-origin' width="100%" height="360" src="${escapeHtml(embedUrl)}"></iframe>`;
} }
// Check Spotify URLs (be very specific - must be open.spotify.com) // Check Spotify URLs (be very specific - must be open.spotify.com)

Loading…
Cancel
Save