-
Notifications
You must be signed in to change notification settings - Fork 2
feat: copy feature v2 #278
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
Open
epsjunior
wants to merge
9
commits into
main
Choose a base branch
from
dxp-575-improve-mdx-to-markdown-processing-for-better-ai-readability
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8ba3cc0
feat: copy feature v2
epsjunior 8f248be
chore: gitignore
epsjunior ff642f8
fix: prevent security issue on safari
epsjunior 558ca4a
chore: comments
epsjunior 2551fd8
chore: comments
epsjunior 6e71beb
fix: custom card better refex
epsjunior 09aabd8
fix: callout regex
epsjunior 0acde6d
feat: including new components and fixing image src with space
epsjunior 507956f
fix: not removing imports from code blocks
epsjunior 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,263 @@ | ||
| const fs = require('fs'); | ||
| const path = require('path'); | ||
|
|
||
| function stripTopLevelImports(input) { | ||
| const lines = input.split('\n'); | ||
| let inFence = false; | ||
| return lines | ||
| .map((line) => { | ||
| const trimmed = line.trim(); | ||
| if (trimmed.startsWith('```')) { | ||
| inFence = !inFence; | ||
| return line; | ||
| } | ||
| if (!inFence && /^import\s+.*$/.test(line)) { | ||
| return ''; | ||
| } | ||
| return line; | ||
| }) | ||
| .join('\n'); | ||
| } | ||
|
|
||
| function processMdxToMarkdown(content) { | ||
| const baseUrl = 'https://docs.genlayer.com'; | ||
|
|
||
| // Helper function to convert relative URLs to absolute | ||
| function makeAbsoluteUrl(url) { | ||
| // Keep anchors as-is | ||
| if (url.startsWith('#')) return url; | ||
|
|
||
| // Absolute URLs | ||
| if (url.startsWith('http://') || url.startsWith('https://')) { | ||
| return encodeURI(url); | ||
| } | ||
|
|
||
| // Relative to root | ||
| if (url.startsWith('/')) { | ||
| return encodeURI(baseUrl + url); | ||
| } | ||
|
|
||
| // Other relative paths | ||
| return encodeURI(url); | ||
| } | ||
|
|
||
| let processed = content; | ||
|
|
||
| // Remove top-level MDX import statements (skip fenced code blocks) | ||
| processed = stripTopLevelImports(processed); | ||
|
|
||
| // Convert CustomCard components to markdown links | ||
| processed = processed.replace( | ||
| /<CustomCard([^>]*)\/>/g, | ||
| (match, attrs) => { | ||
| // Extract attributes regardless of order | ||
| const titleMatch = attrs.match(/title="([^"]*)"/); | ||
| const descMatch = attrs.match(/description="([^"]*)"/); | ||
| const hrefMatch = attrs.match(/href="([^"]*)"/); | ||
|
|
||
| if (titleMatch && hrefMatch) { | ||
| const title = titleMatch[1]; | ||
| const description = descMatch ? descMatch[1] : ''; | ||
| const absoluteUrl = makeAbsoluteUrl(hrefMatch[1]); | ||
| return description | ||
| ? `- **[${title}](${absoluteUrl})**: ${description}` | ||
| : `- **[${title}](${absoluteUrl})**`; | ||
| } | ||
| return match; // Return unchanged if required attributes are missing | ||
| } | ||
| ); | ||
|
|
||
| // Convert Card components to markdown links | ||
| processed = processed.replace( | ||
| /<Card([^>]*)\/>/g, | ||
| (match, attrs) => { | ||
| // Extract attributes regardless of order | ||
| const titleMatch = attrs.match(/title="([^"]*)"/); | ||
| const hrefMatch = attrs.match(/href="([^"]*)"/); | ||
|
|
||
| if (titleMatch && hrefMatch) { | ||
| const title = titleMatch[1]; | ||
| const absoluteUrl = makeAbsoluteUrl(hrefMatch[1]); | ||
| return `- **[${title}](${absoluteUrl})**`; | ||
| } | ||
| return match; // Return unchanged if required attributes are missing | ||
| } | ||
| ); | ||
|
|
||
| // Convert simple JSX links to markdown | ||
| processed = processed.replace( | ||
| /<a\s+href="([^"]*)"[^>]*>([^<]*)<\/a>/g, | ||
| (match, href, text) => { | ||
| const absoluteUrl = makeAbsoluteUrl(href); | ||
| return `[${text}](${absoluteUrl})`; | ||
| } | ||
| ); | ||
|
|
||
| // Convert Callout components to markdown blockquotes | ||
| processed = processed.replace( | ||
| /<Callout([^>]*)>([\s\S]*?)<\/Callout>/g, | ||
| (match, attrs, content) => { | ||
| // Extract attributes regardless of order | ||
| const typeMatch = attrs.match(/type="([^"]*)"/); | ||
| const type = typeMatch ? typeMatch[1] : ''; | ||
| const cleanContent = content.trim(); | ||
| const prefix = type === 'warning' ? 'β οΈ ' : type === 'info' ? 'βΉοΈ ' : ''; | ||
| return `> ${prefix}${cleanContent}`; | ||
| } | ||
| ); | ||
|
|
||
| // Convert Tabs with labeled items into headings with their respective content | ||
| processed = processed.replace( | ||
| /<Tabs[^>]*items=\{\[([\s\S]*?)\]\}[^>]*>([\s\S]*?)<\/Tabs>/g, | ||
| (match, itemsRaw, inner) => { | ||
| const tabTitles = itemsRaw | ||
| .split(',') | ||
| .map(s => s.trim()) | ||
| .map(s => s.replace(/^["']|["']$/g, '')) | ||
| .filter(Boolean); | ||
|
|
||
| const tabContents = []; | ||
| const tabRegex = /<Tabs\.Tab[^>]*>([\s\S]*?)<\/Tabs\.Tab>/g; | ||
| let m; | ||
| while ((m = tabRegex.exec(inner)) !== null) { | ||
| tabContents.push(m[1].trim()); | ||
| } | ||
|
|
||
| // Map titles to contents; if counts mismatch, best-effort pairing | ||
| const sections = []; | ||
| const count = Math.max(tabTitles.length, tabContents.length); | ||
| for (let i = 0; i < count; i++) { | ||
| const title = tabTitles[i] || `Tab ${i + 1}`; | ||
| const content = (tabContents[i] || '').trim(); | ||
| if (content) { | ||
| sections.push(`### ${title}\n\n${content}`); | ||
| } else { | ||
| sections.push(`### ${title}`); | ||
| } | ||
| } | ||
| return sections.join('\n\n'); | ||
| } | ||
| ); | ||
|
|
||
| // Fallback: strip any remaining Tabs wrappers (keep inner content) | ||
| processed = processed.replace(/<Tabs[^>]*>/g, ''); | ||
| processed = processed.replace(/<\/Tabs>/g, ''); | ||
| processed = processed.replace(/<Tabs\.Tab[^>]*>/g, ''); | ||
| processed = processed.replace(/<\/Tabs\.Tab>/g, ''); | ||
|
|
||
| // Strip Cards container (individual <Card/> handled above) | ||
| processed = processed.replace(/<Cards[^>]*>/g, ''); | ||
| processed = processed.replace(/<\/Cards>/g, ''); | ||
|
|
||
| // Strip Bleed wrapper | ||
| processed = processed.replace(/<Bleed[^>]*>/g, ''); | ||
| processed = processed.replace(/<\/Bleed>/g, ''); | ||
|
|
||
| // Strip Fragment wrapper | ||
| processed = processed.replace(/<Fragment[^>]*>/g, ''); | ||
| processed = processed.replace(/<\/Fragment>/g, ''); | ||
|
|
||
| // Convert simple HTML divs to text (remove div tags but keep content) | ||
| processed = processed.replace(/<div[^>]*>([\s\S]*?)<\/div>/g, '$1'); | ||
|
|
||
| // Convert <br/> tags to line breaks | ||
| processed = processed.replace(/<br\s*\/?>(?!\s*<\/)/g, '\n'); | ||
|
|
||
| // Convert Image components to markdown images (with alt) - leave src as-is to avoid double-encoding | ||
| processed = processed.replace( | ||
| /<Image[^>]*\s+src="([^"]*)"[^>]*\s+alt="([^"]*)"[^>]*\/?>(?!\s*<\/Image>)/g, | ||
| (match, src, alt) => { | ||
| return ``; | ||
| } | ||
| ); | ||
|
|
||
| // Convert Image components to markdown images (without alt) - leave src as-is | ||
| processed = processed.replace( | ||
| /<Image[^>]*\s+src="([^"]*)"[^>]*\/?>(?!\s*<\/Image>)/g, | ||
| (match, src) => { | ||
| return ``; | ||
| } | ||
| ); | ||
|
|
||
| // Convert regular <img> tags to markdown images - leave src as-is | ||
| processed = processed.replace( | ||
| /<img[^>]*\s+src="([^"]*)"[^>]*\s+alt="([^"]*)"[^>]*\/?>(?!\s*<\/img>)/g, | ||
| (match, src, alt) => { | ||
| return ``; | ||
| } | ||
| ); | ||
|
|
||
| // Convert regular markdown images to absolute URLs | ||
| processed = processed.replace( | ||
| /!\[([^\]]*)\]\(([^)]*)\)/g, | ||
| (match, alt, src) => { | ||
| const absoluteUrl = makeAbsoluteUrl(src); | ||
| return ``; | ||
| } | ||
| ); | ||
|
|
||
| // Convert regular markdown links to absolute URLs (skip images) | ||
| processed = processed.replace( | ||
| /(^|[^!])\[([^\]]*)\]\(([^)]*)\)/gm, | ||
| (match, prefix, text, href) => { | ||
| const absoluteUrl = makeAbsoluteUrl(href); | ||
| return `${prefix}[${text}](${absoluteUrl})`; | ||
| } | ||
| ); | ||
|
|
||
| // Remove empty lines created by import removal and clean up | ||
| processed = processed | ||
| .split('\n') | ||
| .filter((line, index, array) => { | ||
| // Remove empty lines at the start | ||
| if (index === 0 && line.trim() === '') return false; | ||
| // Remove multiple consecutive empty lines | ||
| if (line.trim() === '' && array[index - 1] && array[index - 1].trim() === '') return false; | ||
| return true; | ||
| }) | ||
| .join('\n') | ||
| .trim(); | ||
|
|
||
| // Normalize list indentation (remove unintended leading spaces before list markers) | ||
| processed = processed.replace(/^[ \t]+- /gm, '- '); | ||
|
|
||
| return processed; | ||
| } | ||
|
|
||
| function processAllMdxFiles() { | ||
| const pagesDir = path.join(process.cwd(), 'pages'); | ||
| const publicPagesDir = path.join(process.cwd(), 'public', 'pages'); | ||
|
|
||
| // Create public/pages directory if it doesn't exist | ||
| if (!fs.existsSync(publicPagesDir)) { | ||
| fs.mkdirSync(publicPagesDir, { recursive: true }); | ||
| } | ||
|
|
||
| function processDirectory(sourceDir, targetDir) { | ||
| const items = fs.readdirSync(sourceDir); | ||
|
|
||
| items.forEach(item => { | ||
| const sourcePath = path.join(sourceDir, item); | ||
| const stat = fs.statSync(sourcePath); | ||
|
|
||
| if (stat.isDirectory()) { | ||
| const newTargetDir = path.join(targetDir, item); | ||
| if (!fs.existsSync(newTargetDir)) { | ||
| fs.mkdirSync(newTargetDir, { recursive: true }); | ||
| } | ||
| processDirectory(sourcePath, newTargetDir); | ||
| } else if (item.endsWith('.mdx')) { | ||
| const content = fs.readFileSync(sourcePath, 'utf8'); | ||
| const processedContent = processMdxToMarkdown(content); | ||
|
|
||
| const targetPath = path.join(targetDir, item.replace('.mdx', '.md')); | ||
| fs.writeFileSync(targetPath, processedContent); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| processDirectory(pagesDir, publicPagesDir); | ||
| console.log('β Processed all MDX files to clean Markdown'); | ||
| } | ||
epsjunior marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| processAllMdxFiles(); | ||
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.
Uh oh!
There was an error while loading. Please reload this page.