1+ const express = require ( 'express' ) ;
2+ const fs = require ( "fs" ) ;
3+ const {
4+ render
5+ } = require ( "mustache" ) ;
6+ const sitedata = require ( "./config.json" ) ;
7+ const slugify = require ( "slugify" ) ;
8+
9+ const app = express ( ) ;
10+ const port = 3005 ;
11+
12+ app . set ( 'view engine' , 'hbs' ) ;
13+ app . use ( express . static ( 'public' ) ) ;
14+ app . set ( 'views' , './views' ) ;
15+
16+ app . use ( express . json ( ) ) ;
17+ app . use ( express . urlencoded ( {
18+ extended : true
19+ } ) ) ;
20+
21+ app . listen ( port , function ( ) {
22+ console . log ( 'listening on port ' + port ) ;
23+ } ) ;
24+
25+ function getCurrentDate ( n ) {
26+ return ( n < 10 ? "0" : "" ) + n
27+ }
28+
29+ const date = new Date ( )
30+ const month = getCurrentDate ( date . getMonth ( ) + 1 )
31+ const day = getCurrentDate ( date . getDate ( ) )
32+ const year = date . getFullYear ( )
33+ const formattedDate = year + "-" + month + "-" + day
34+
35+ app . get ( '/' , function ( req , res ) {
36+ res . render ( 'home' , {
37+ post : {
38+ title : 'Create New Markdown Post' ,
39+ description : 'Create New Markdown Blog Post.'
40+ }
41+ } ) ;
42+ } ) ;
43+
44+ app . post ( '/' , function ( req , res ) {
45+
46+ res . header ( 'X-Frame-Options' , 'DENY' ) ;
47+ res . header ( 'X-XSS-Protection' , '1; mode=block' ) ;
48+ res . header ( 'X-Content-Type-Options' , 'nosniff' ) ;
49+ res . header ( 'Strict-Transport-Security' , 'max-age=63072000' ) ;
50+
51+ const blog_title = req . body . title
52+ const random_id = Math . floor ( 1000 + Math . random ( ) * 9000 )
53+ const basename = sitedata . url_data + "-" + random_id
54+
55+ if ( blog_title == 0 || blog_title == "" ) {
56+
57+ res . status ( 200 ) . json ( {
58+ sucess : 0 ,
59+ message : 'Error Something is Missing'
60+ } ) ;
61+
62+ } else {
63+
64+ const seo_url = slugify ( blog_title , {
65+ replacement : '-' ,
66+ remove : / [ * + ~ . ( ) ' " ! : @ ] / g,
67+ lower : true ,
68+ strict : false
69+ } ) ;
70+
71+ var title = blog_title ;
72+ var description = req . body . description ;
73+ var date = formattedDate ;
74+ var tag = req . body . tag ;
75+ var postcontent = req . body . postcontent ;
76+ let content = [ {
77+ title : title || "Example Post title" ,
78+ description : description || "Example Post description" ,
79+ date : date ,
80+ tag : tag || "Hello World" ,
81+ postcontent : postcontent || "Example Post Content" ,
82+ slug : decodeURIComponent ( seo_url )
83+ } ] ;
84+ let template = fs . readFileSync ( "./template.md" ) . toString ( )
85+ content . forEach ( post_data => {
86+ let output = render ( template , post_data )
87+ const clean_url = basename ;
88+ fs . writeFileSync ( `${ sitedata . storage_path } /${ clean_url } .${ sitedata . format } ` , output )
89+ console . log ( post_data ) ;
90+ } )
91+ res . status ( 200 ) . json ( {
92+ sucess : 1 ,
93+ message : 'Post Created'
94+ } ) ;
95+ }
96+ } ) ;
97+
98+ app . use ( '/' , function ( req , res ) {
99+ res . status ( 404 ) . json ( {
100+ error : 1 ,
101+ message : 'Web App Error'
102+ } ) ;
103+ } ) ;
0 commit comments