File size: 3,574 Bytes
5878f15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// pages/api/jekyll/create.js
import { exec } from 'child_process';
import { promisify } from 'util';
import path from 'path';
import fs from 'fs-extra';

const execAsync = promisify(exec);

export default async function handler(req, res) {
  if (req.method !== 'POST') {
    return res.status(405).json({ error: 'Method not allowed' });
  }

  try {
    const { siteName, template = 'blog', config = {} } = req.body;

    if (!siteName || !/^[a-zA-Z0-9-_]+$/.test(siteName)) {
      return res.status(400).json({ 
        error: 'Invalid site name. Use only alphanumeric, dash, and underscore.' 
      });
    }

    const sitePath = path.join('/app/projects', siteName);

    // Check if site already exists
    if (await fs.pathExists(sitePath)) {
      return res.status(409).json({ 
        error: 'Site already exists' 
      });
    }

    // Create Jekyll site menggunakan entrypoint script
    console.log(`Creating Jekyll site: ${siteName}`);
    
    const { stdout, stderr } = await execAsync(
      `/usr/local/bin/entrypoint.sh create-site ${siteName}`,
      { 
        timeout: 30000,
        cwd: '/app'
      }
    );

    // Customize site based on template
    await customizeJekyllSite(sitePath, template, config);

    console.log('Jekyll site created:', stdout);
    if (stderr) console.warn('Jekyll warnings:', stderr);

    res.status(201).json({
      success: true,
      message: 'Jekyll site created successfully',
      siteName,
      path: sitePath,
      previewUrl: `/preview/${siteName}`,
      output: stdout
    });

  } catch (error) {
    console.error('Error creating Jekyll site:', error);
    
    res.status(500).json({
      error: 'Failed to create Jekyll site',
      details: error.message,
      stderr: error.stderr || null
    });
  }
}

async function customizeJekyllSite(sitePath, template, config) {
  try {
    // Update _config.yml dengan konfigurasi custom
    const configPath = path.join(sitePath, '_config.yml');
    const defaultConfig = await fs.readFile(configPath, 'utf8');
    
    const customConfig = `${defaultConfig}

# Jekyll Studio Configuration
title: ${config.title || 'My Jekyll Site'}
description: ${config.description || 'Created with Jekyll Studio'}
url: ""
baseurl: ""

# Build settings
markdown: kramdown
highlighter: rouge
theme: minima

plugins:
  - jekyll-feed

# Exclude from processing
exclude:
  - Gemfile
  - Gemfile.lock
  - node_modules
  - vendor/bundle/
  - vendor/cache/
  - vendor/gems/
  - vendor/ruby/
`;

    await fs.writeFile(configPath, customConfig);

    // Create sample post berdasarkan template
    const postsDir = path.join(sitePath, '_posts');
    await fs.ensureDir(postsDir);

    const samplePost = `---
layout: post
title: "Welcome to Jekyll Studio!"
date: ${new Date().toISOString().split('T')[0]} 12:00:00 +0000
categories: jekyll update
---

# Welcome to Your New Jekyll Site!

This site was created using **Jekyll Studio** on Hugging Face Spaces.

## Features

- πŸš€ Fast static site generation
- πŸ“ Markdown support  
- 🎨 Customizable themes
- πŸ“± Mobile responsive
- πŸ” SEO optimized

## Getting Started

Edit this post in \`_posts/\` directory or create new posts using the Jekyll Studio interface.

Happy blogging! ✨
`;

    const postPath = path.join(postsDir, `${new Date().toISOString().split('T')[0]}-welcome-to-jekyll-studio.md`);
    await fs.writeFile(postPath, samplePost);

    console.log(`Jekyll site customized with ${template} template`);
  } catch (error) {
    console.warn('Failed to customize Jekyll site:', error.message);
  }
}