Skip to content

Commit b5fae6f

Browse files
committed
✨ First Commit
0 parents  commit b5fae6f

File tree

13 files changed

+1748
-0
lines changed

13 files changed

+1748
-0
lines changed

.gitignore

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# Created by .ignore support plugin (hsz.mobi)
2+
### Node template
3+
# Logs
4+
/logs
5+
*.log
6+
npm-debug.log*
7+
yarn-debug.log*
8+
yarn-error.log*
9+
.vercel
10+
11+
# Runtime data
12+
pids
13+
*.pid
14+
*.seed
15+
*.pid.lock
16+
17+
# Directory for instrumented libs generated by jscoverage/JSCover
18+
lib-cov
19+
20+
# Coverage directory used by tools like istanbul
21+
coverage
22+
23+
# nyc test coverage
24+
.nyc_output
25+
26+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
27+
.grunt
28+
29+
# Bower dependency directory (https://bower.io/)
30+
bower_components
31+
32+
# node-waf configuration
33+
.lock-wscript
34+
35+
# Compiled binary addons (https://nodejs.org/api/addons.html)
36+
build/Release
37+
38+
# Dependency directories
39+
node_modules/
40+
jspm_packages/
41+
42+
# TypeScript v1 declaration files
43+
typings/
44+
45+
# Optional npm cache directory
46+
.npm
47+
48+
# Optional eslint cache
49+
.eslintcache
50+
51+
# Optional REPL history
52+
.node_repl_history
53+
54+
# Output of 'npm pack'
55+
*.tgz
56+
57+
# Yarn Integrity file
58+
.yarn-integrity
59+
60+
# dotenv environment variables file
61+
.env
62+
63+
# parcel-bundler cache (https://parceljs.org/)
64+
.cache
65+
66+
# next.js build output
67+
.next
68+
69+
# nuxt.js build output
70+
.nuxt
71+
72+
# Nuxt generate
73+
dist
74+
75+
# vuepress build output
76+
.vuepress/dist
77+
78+
# Serverless directories
79+
.serverless
80+
81+
# IDE / Editor
82+
.idea
83+
84+
# Service worker
85+
sw.*
86+
87+
# macOS
88+
.DS_Store
89+
90+
# Vim swap files
91+
*.swp

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# MIT License
2+
3+
Copyright (c) 2021 Santhosh Veer
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# Create Markdown Post
2+
3+
Create Markdown Post for blog and website using Mustache Template System.
4+
5+
> Under Development and Testing Stage
6+
7+
## Usage
8+
9+
- Express.js for Server and API
10+
- Mustache.JS for Template
11+
- Slugify for Generate SEO Friedly Filename and Slug
12+
- Auto Date Generation
13+
- Handlebar for HTML Template page
14+
15+
## Development
16+
17+
- Clone or Download the repo
18+
19+
```sh
20+
git clone https://github.com/mskian/create-markdown-post.git
21+
cd create-markdown-post
22+
yarn
23+
```
24+
25+
- Start the Dev server
26+
27+
```sh
28+
yarn dev
29+
```
30+
31+
- Start the Production Server
32+
33+
```sh
34+
yarn start
35+
```
36+
37+
- Post New Content
38+
39+
```sh
40+
http://localhost:3005/
41+
```
42+
43+
- Post API Example
44+
45+
```sh
46+
curl --request POST \
47+
--url http://localhost:3005/ \
48+
--header 'Content-Type: application/x-www-form-urlencoded' \
49+
--data 'title=Example Post' \
50+
--data 'postcontent=Example Post - Example Blog Post via cURL.' \
51+
--data 'tag=Hello World' \
52+
--data 'description=Post Content - Hello World Blog.'
53+
```
54+
55+
```php
56+
<?php
57+
58+
$curl = curl_init();
59+
60+
curl_setopt_array($curl, [
61+
CURLOPT_PORT => "3005",
62+
CURLOPT_URL => "http://localhost:3005/",
63+
CURLOPT_RETURNTRANSFER => true,
64+
CURLOPT_ENCODING => "",
65+
CURLOPT_MAXREDIRS => 10,
66+
CURLOPT_TIMEOUT => 30,
67+
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
68+
CURLOPT_CUSTOMREQUEST => "POST",
69+
CURLOPT_POSTFIELDS => "title=Example%20Post&postcontent=Example%20Post%20-%20Example%20Blog%20Post%20via%20cURL.&tag=Hello%20World&description=Post%20Content%20-%20Hello%20World%20Blog.",
70+
CURLOPT_HTTPHEADER => [
71+
"Content-Type: application/x-www-form-urlencoded"
72+
],
73+
]);
74+
75+
$response = curl_exec($curl);
76+
$err = curl_error($curl);
77+
78+
curl_close($curl);
79+
80+
if ($err) {
81+
echo "cURL Error #:" . $err;
82+
} else {
83+
echo $response;
84+
}
85+
```
86+
87+
## Modification
88+
89+
- Modify the Post template Content data on `index.js`
90+
- Add storage path, Markdown Extension and Manual URL Slug Generation Name in `config.json`
91+
- if you want add custom slug in Markdown File - `template.md`
92+
93+
```md
94+
---
95+
title: "{{title}}"
96+
date: {{date}}
97+
description: "{{description}}"
98+
tags:
99+
- "{{tag}}"
100+
slug: "{{seo_url}}"
101+
---
102+
103+
{{postcontent}}
104+
105+
```
106+
107+
## LICENSE
108+
109+
MIT

config.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"url_data": "example-blog",
3+
"storage_path": "./posts",
4+
"format": "md"
5+
}

index.js

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
});

package.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "create-markdown-post",
3+
"version": "0.0.1",
4+
"description": "Create Markdown Post for blog and website using Mustache Template System.",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1",
8+
"start": "node index.js",
9+
"dev": "nodemon index.js"
10+
},
11+
"repository": {
12+
"type": "git",
13+
"url": "git+https://github.com/mskian/create-markdown-post.git"
14+
},
15+
"keywords": [
16+
"markdown",
17+
"json",
18+
"api",
19+
"mustahe"
20+
],
21+
"author": "Santhosh veer",
22+
"license": "MIT",
23+
"bugs": {
24+
"url": "https://github.com/mskian/create-markdown-post/issues"
25+
},
26+
"homepage": "https://github.com/mskian/create-markdown-post#readme",
27+
"dependencies": {
28+
"express": "^4.17.1",
29+
"hbs": "^4.1.2",
30+
"mustache": "^4.2.0",
31+
"slugify": "^1.6.2"
32+
},
33+
"devDependencies": {
34+
"nodemon": "^2.0.15"
35+
}
36+
}

posts/example-blog-4286.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
title: "Example Post title"
3+
description: "This is an Example Post description"
4+
date: 2021-11-10
5+
tags:
6+
- "Hello World"
7+
---
8+
9+
## Hello World Post
10+
11+
This is Example Post Content

0 commit comments

Comments
 (0)