Fixed xml formatting
This commit is contained in:
parent
9c09d34a5f
commit
ce926cee4b
1 changed files with 30 additions and 63 deletions
|
|
@ -9,9 +9,9 @@ const SeoFile = ({ filePath }) => {
|
|||
const [content, setContent] = useState('');
|
||||
const [contentType, setContentType] = useState('');
|
||||
const [error, setError] = useState(null);
|
||||
console.log(filePath);
|
||||
|
||||
console.log(filePath)
|
||||
useEffect(() => {
|
||||
// Determine the content type based on the file extension
|
||||
const fileExtension = filePath.split('.').pop();
|
||||
const type = fileExtension === 'xml' ? 'application/xml' : 'text/plain';
|
||||
setContentType(type);
|
||||
|
|
@ -32,79 +32,46 @@ const SeoFile = ({ filePath }) => {
|
|||
});
|
||||
}, [filePath]);
|
||||
|
||||
// Set the content type and return the raw content
|
||||
useEffect(() => {
|
||||
if (content && contentType) {
|
||||
// Clear existing document
|
||||
document.open();
|
||||
|
||||
if (contentType.includes('xml')) {
|
||||
// XML styling
|
||||
document.write(`
|
||||
<!DOCTYPE html>
|
||||
<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>
|
||||
`);
|
||||
// For XML sitemaps, just write the raw content directly
|
||||
// This is critical - no HTML tags or wrappers for XML files
|
||||
document.write(content);
|
||||
} else {
|
||||
document.write(`
|
||||
<!DOCTYPE html>
|
||||
<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>
|
||||
`);
|
||||
// For robots.txt, use the pre tag to preserve formatting
|
||||
document.write(`<pre>${content}</pre>`);
|
||||
}
|
||||
|
||||
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]);
|
||||
|
||||
// Helper function to escape HTML entities
|
||||
function escapeHtml(text) {
|
||||
if (!text) return '';
|
||||
return text
|
||||
.replace(/&/g, '&')
|
||||
.replace(/</g, '<')
|
||||
.replace(/>/g, '>')
|
||||
.replace(/"/g, '"')
|
||||
.replace(/'/g, ''');
|
||||
}
|
||||
|
||||
// If there was an error, show a simple error message
|
||||
if (error) {
|
||||
return <div>{error}</div>;
|
||||
|
|
|
|||
Loading…
Reference in a new issue