Fixed xml formatting

This commit is contained in:
2ManyProjects 2025-05-07 21:40:59 -05:00
parent 9c09d34a5f
commit ce926cee4b

View file

@ -9,9 +9,9 @@ const SeoFile = ({ filePath }) => {
const [content, setContent] = useState(''); const [content, setContent] = useState('');
const [contentType, setContentType] = useState(''); const [contentType, setContentType] = useState('');
const [error, setError] = useState(null); const [error, setError] = useState(null);
console.log(filePath); console.log(filePath)
useEffect(() => { useEffect(() => {
// Determine the content type based on the file extension
const fileExtension = filePath.split('.').pop(); const fileExtension = filePath.split('.').pop();
const type = fileExtension === 'xml' ? 'application/xml' : 'text/plain'; const type = fileExtension === 'xml' ? 'application/xml' : 'text/plain';
setContentType(type); setContentType(type);
@ -32,79 +32,46 @@ const SeoFile = ({ filePath }) => {
}); });
}, [filePath]); }, [filePath]);
// Set the content type and return the raw content
useEffect(() => { useEffect(() => {
if (content && contentType) { if (content && contentType) {
// Clear existing document
document.open(); document.open();
if (contentType.includes('xml')) { if (contentType.includes('xml')) {
// XML styling // For XML sitemaps, just write the raw content directly
document.write(` // This is critical - no HTML tags or wrappers for XML files
<!DOCTYPE html> document.write(content);
<html>
<head>
<meta http-equiv="Content-Type" content="${contentType}; charset=utf-8">
<style>
body {
font-family: monospace;
background: #282c34;
color: #abb2bf;
padding: 20px;
}
pre {
white-space: pre-wrap;
word-wrap: break-word;
}
</style>
</head>
<body>
<pre>${escapeHtml(content)}</pre>
</body>
</html>
`);
} else { } else {
document.write(` // For robots.txt, use the pre tag to preserve formatting
<!DOCTYPE html> document.write(`<pre>${content}</pre>`);
<html>
<head>
<meta http-equiv="Content-Type" content="${contentType}; charset=utf-8">
<style>
body {
font-family: monospace;
background: #f5f5f5;
padding: 20px;
margin: 0;
}
pre {
white-space: pre;
font-size: 14px;
line-height: 1.5;
padding: 0;
margin: 0;
}
</style>
</head>
<body>
<pre>${escapeHtml(content)}</pre>
</body>
</html>
`);
} }
document.close(); document.close();
// Set the correct content type meta tag
const meta = document.createElement('meta');
meta.httpEquiv = 'Content-Type';
meta.content = `${contentType}; charset=utf-8`;
document.head.appendChild(meta);
// Add minimal styling for robots.txt only
if (!contentType.includes('xml')) {
const style = document.createElement('style');
style.textContent = `
pre {
font-family: monospace;
font-size: 14px;
line-height: 1.5;
margin: 0;
padding: 15px;
}
`;
document.head.appendChild(style);
}
} }
}, [content, contentType]); }, [content, contentType]);
// Helper function to escape HTML entities
function escapeHtml(text) {
if (!text) return '';
return text
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&#039;');
}
// If there was an error, show a simple error message // If there was an error, show a simple error message
if (error) { if (error) {
return <div>{error}</div>; return <div>{error}</div>;