-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
113 lines (94 loc) · 3.84 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
const express = require('express')
const fs = require('fs')
const path = require('path')
const MarkdownIt = require('markdown-it')
const markdownItCheckbox = require('markdown-it-checkbox');
const app = express()
const md = new MarkdownIt()
// Adiciona o plugin de checkboxes ao MarkdownIt
md.use(markdownItCheckbox);
// Definir variável para verificar o ambiente
const isProduction = process.env.NODE_ENV === 'production'
const PORT = process.env.PORT || 3000
const baseUrl = isProduction ? 'https://juninhopo.com' : `http://localhost:${PORT}`
// Defina o diretório de arquivos estáticos
app.use(express.static(path.join(__dirname, 'public')))
// Função para ajustar links no conteúdo do Markdown
const adjustLinks = (content) => {
return content.replace(/https:\/\/juninhopo\.com/g, baseUrl)
}
const generateBreadcrumbs = (path) => {
// Remove segmentos vazios e o 'index' final do caminho
const segments = path.split('/').filter(segment => segment && segment !== 'index');
// Gera os breadcrumbs com links, adicionando '/index' ao final do href
const breadcrumbs = segments.map((segment, index) => {
const href = '/' + segments.slice(0, index + 1).join('/') + '/index'; // Adiciona '/index'
return `<a href="${href}">${decodeURIComponent(segment)}</a>`;
});
return `
<nav class="breadcrumbs">
<a href="/">Home</a>
${breadcrumbs.length > 0 ? breadcrumbs.map(crumb => `<span class="separator">→</span>${crumb}`).join('') : ''}
</nav>
`;
};
app.get('*', (req, res) => {
// Captura o caminho da URL e remove a barra inicial
const relativePath = req.path.slice(1)
const fileName = `${relativePath || 'index'}.md`
const filePath = path.join(__dirname, 'views', fileName)
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
res.status(404).send('Arquivo Markdown não encontrado.')
return
}
// Renderiza o conteúdo Markdown e ajusta os links
const htmlContent = adjustLinks(md.render(data))
// Gera os breadcrumbs apenas se não for a página inicial
const breadcrumbs = (req.path === '/' || req.path === '/index' || req.path === '/index.md')
? ''
: generateBreadcrumbs(req.path);
// Lógica para decidir se o footer será exibido
const footerContent = isProduction
? ''
: `<footer style="position: fixed; bottom: 0; left: 0; right: 0; background: #333; color: #fff; text-align: center; padding: 10px;">
${isProduction ? 'Running in Production' : 'Running in Development'}
</footer>`
// Condição para não exibir os botões na home
const isHomePage = req.path === '/' || req.path === '/index' || req.path === '/index.md';
// Botão de Home
const homeButton = !isHomePage ? `
<div style="position: fixed; top: 20px; right: 20px; background: #007bff; color: white; padding: 10px 20px; border-radius: 5px; cursor: pointer;">
<a href="/" style="color: white; text-decoration: none;">Home</a>
</div>
` : '';
// Botão de Voltar
const backButton = !isHomePage ? `
<div style="position: fixed; bottom: 60px; right: 20px; background: #28a745; color: white; padding: 10px 20px; border-radius: 5px; cursor: pointer;" onclick="window.history.back()">
Voltar
</div>
` : '';
// Envia o conteúdo renderizado como HTML
res.send(`
<!DOCTYPE html>
<html>
<head>
<title>${relativePath || 'index'}</title>
<link rel="stylesheet" href="/styles.css">
<link rel="icon" type="image/x-icon" href="/favicon.ico" />
</head>
<body>
${breadcrumbs}
<!-- ${homeButton} --!>
${backButton}
${htmlContent}
${footerContent}
</body>
</html>
`)
})
})
// Iniciar o servidor
app.listen(PORT, () => {
console.log(`🚀 Server is Running in port: http://localhost:${PORT}/`)
})