Initial commit: Markdown editor with file management and regex tools
项目特性: - 完整的Markdown编辑器,支持实时预览 - 文件管理功能,支持保存/加载/删除文件 - 正则表达式工具,支持批量文本替换 - 前后端分离架构 - 响应式设计 技术栈: - 前端:React + TypeScript + Vite - 后端:Python Flask - Markdown解析:Python-Markdown 包含组件: - WorkingMarkdownEditor: 基础功能版本 - FullMarkdownEditor: 完整功能版本 - SimpleMarkdownEditor: 简化版本
This commit is contained in:
32
backend/app/__init__.py
Normal file
32
backend/app/__init__.py
Normal file
@@ -0,0 +1,32 @@
|
||||
"""
|
||||
Flask应用初始化模块
|
||||
负责创建和配置Flask应用实例
|
||||
"""
|
||||
|
||||
from flask import Flask
|
||||
from flask_cors import CORS
|
||||
|
||||
|
||||
def create_app():
|
||||
"""
|
||||
创建并配置Flask应用
|
||||
|
||||
Returns:
|
||||
Flask: 配置好的Flask应用实例
|
||||
"""
|
||||
app = Flask(__name__)
|
||||
|
||||
# 配置跨域支持
|
||||
CORS(app, resources={
|
||||
r"/api/*": {
|
||||
"origins": ["http://localhost:3000", "http://127.0.0.1:3000"],
|
||||
"methods": ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
|
||||
"allow_headers": ["Content-Type", "Authorization"]
|
||||
}
|
||||
})
|
||||
|
||||
# 注册蓝图
|
||||
from app.routes import api_bp
|
||||
app.register_blueprint(api_bp, url_prefix='/api')
|
||||
|
||||
return app
|
262
backend/app/routes.py
Normal file
262
backend/app/routes.py
Normal file
@@ -0,0 +1,262 @@
|
||||
"""
|
||||
API路由模块
|
||||
处理所有Markdown编辑器的API端点
|
||||
"""
|
||||
|
||||
import os
|
||||
import re
|
||||
from flask import Blueprint, request, jsonify, send_file
|
||||
from utils.markdown_parser import MarkdownParser
|
||||
from utils.file_manager import FileManager
|
||||
from utils.regex_processor import RegexProcessor
|
||||
|
||||
# 创建API蓝图
|
||||
api_bp = Blueprint('api', __name__)
|
||||
|
||||
# 初始化工具类
|
||||
markdown_parser = MarkdownParser()
|
||||
file_manager = FileManager()
|
||||
regex_processor = RegexProcessor()
|
||||
|
||||
|
||||
@api_bp.route('/health', methods=['GET'])
|
||||
def health_check():
|
||||
"""
|
||||
健康检查端点
|
||||
|
||||
Returns:
|
||||
JSON: 服务状态信息
|
||||
"""
|
||||
return jsonify({
|
||||
'status': 'healthy',
|
||||
'service': 'markdown-editor-api'
|
||||
})
|
||||
|
||||
|
||||
@api_bp.route('/parse', methods=['POST'])
|
||||
def parse_markdown():
|
||||
"""
|
||||
解析Markdown文本为HTML
|
||||
|
||||
Request Body:
|
||||
{
|
||||
"content": "# Hello World"
|
||||
}
|
||||
|
||||
Returns:
|
||||
JSON: {
|
||||
"html": "<h1>Hello World</h1>",
|
||||
"metadata": {...}
|
||||
}
|
||||
"""
|
||||
try:
|
||||
data = request.get_json()
|
||||
if not data or 'content' not in data:
|
||||
return jsonify({'error': 'Missing content field'}), 400
|
||||
|
||||
content = data['content']
|
||||
result = markdown_parser.parse(content)
|
||||
|
||||
return jsonify(result)
|
||||
|
||||
except Exception as e:
|
||||
return jsonify({'error': str(e)}), 500
|
||||
|
||||
|
||||
@api_bp.route('/files', methods=['GET'])
|
||||
def list_files():
|
||||
"""
|
||||
获取指定目录的文件列表
|
||||
|
||||
Query Parameters:
|
||||
path: 目录路径(相对于工作目录)
|
||||
|
||||
Returns:
|
||||
JSON: 文件列表信息
|
||||
"""
|
||||
try:
|
||||
path = request.args.get('path', '')
|
||||
files = file_manager.list_files(path)
|
||||
return jsonify({'files': files})
|
||||
|
||||
except Exception as e:
|
||||
return jsonify({'error': str(e)}), 500
|
||||
|
||||
|
||||
@api_bp.route('/files/read', methods=['GET'])
|
||||
def read_file():
|
||||
"""
|
||||
读取文件内容
|
||||
|
||||
Query Parameters:
|
||||
path: 文件路径
|
||||
|
||||
Returns:
|
||||
JSON: 文件内容和元数据
|
||||
"""
|
||||
try:
|
||||
path = request.args.get('path')
|
||||
if not path:
|
||||
return jsonify({'error': 'Missing path parameter'}), 400
|
||||
|
||||
content = file_manager.read_file(path)
|
||||
return jsonify({'content': content})
|
||||
|
||||
except FileNotFoundError:
|
||||
return jsonify({'error': 'File not found'}), 404
|
||||
except Exception as e:
|
||||
return jsonify({'error': str(e)}), 500
|
||||
|
||||
|
||||
@api_bp.route('/files/write', methods=['POST'])
|
||||
def write_file():
|
||||
"""
|
||||
写入文件内容
|
||||
|
||||
Request Body:
|
||||
{
|
||||
"path": "文件路径",
|
||||
"content": "文件内容"
|
||||
}
|
||||
|
||||
Returns:
|
||||
JSON: 操作结果
|
||||
"""
|
||||
try:
|
||||
data = request.get_json()
|
||||
if not data or 'path' not in data or 'content' not in data:
|
||||
return jsonify({'error': 'Missing required fields'}), 400
|
||||
|
||||
path = data['path']
|
||||
content = data['content']
|
||||
|
||||
file_manager.write_file(path, content)
|
||||
return jsonify({'message': 'File saved successfully'})
|
||||
|
||||
except Exception as e:
|
||||
return jsonify({'error': str(e)}), 500
|
||||
|
||||
|
||||
@api_bp.route('/files/delete', methods=['DELETE'])
|
||||
def delete_file():
|
||||
"""
|
||||
删除文件
|
||||
|
||||
Request Body:
|
||||
{
|
||||
"path": "文件路径"
|
||||
}
|
||||
|
||||
Returns:
|
||||
JSON: 操作结果
|
||||
"""
|
||||
try:
|
||||
data = request.get_json()
|
||||
if not data or 'path' not in data:
|
||||
return jsonify({'error': 'Missing path field'}), 400
|
||||
|
||||
file_manager.delete_file(data['path'])
|
||||
return jsonify({'message': 'File deleted successfully'})
|
||||
|
||||
except FileNotFoundError:
|
||||
return jsonify({'error': 'File not found'}), 404
|
||||
except Exception as e:
|
||||
return jsonify({'error': str(e)}), 500
|
||||
|
||||
|
||||
@api_bp.route('/regex/replace', methods=['POST'])
|
||||
def regex_replace():
|
||||
"""
|
||||
使用正则表达式批量替换文本
|
||||
|
||||
Request Body:
|
||||
{
|
||||
"content": "原始文本",
|
||||
"pattern": "正则表达式",
|
||||
"replacement": "替换内容",
|
||||
"flags": "gi"
|
||||
}
|
||||
|
||||
Returns:
|
||||
JSON: {
|
||||
"result": "替换后的文本",
|
||||
"matches": 3,
|
||||
"groups": [...]
|
||||
}
|
||||
"""
|
||||
try:
|
||||
data = request.get_json()
|
||||
required_fields = ['content', 'pattern', 'replacement']
|
||||
|
||||
if not data or not all(field in data for field in required_fields):
|
||||
return jsonify({'error': 'Missing required fields'}), 400
|
||||
|
||||
content = data['content']
|
||||
pattern = data['pattern']
|
||||
replacement = data['replacement']
|
||||
flags = data.get('flags', '')
|
||||
|
||||
result = regex_processor.replace(content, pattern, replacement, flags)
|
||||
return jsonify(result)
|
||||
|
||||
except re.error as e:
|
||||
return jsonify({'error': f'Invalid regex pattern: {str(e)}'}), 400
|
||||
except Exception as e:
|
||||
return jsonify({'error': str(e)}), 500
|
||||
|
||||
|
||||
@api_bp.route('/regex/extract', methods=['POST'])
|
||||
def regex_extract():
|
||||
"""
|
||||
使用正则表达式提取文本中的匹配项
|
||||
|
||||
Request Body:
|
||||
{
|
||||
"content": "文本内容",
|
||||
"pattern": "正则表达式",
|
||||
"flags": "gi"
|
||||
}
|
||||
|
||||
Returns:
|
||||
JSON: 匹配项列表
|
||||
"""
|
||||
try:
|
||||
data = request.get_json()
|
||||
if not data or 'content' not in data or 'pattern' not in data:
|
||||
return jsonify({'error': 'Missing required fields'}), 400
|
||||
|
||||
content = data['content']
|
||||
pattern = data['pattern']
|
||||
flags = data.get('flags', '')
|
||||
|
||||
matches = regex_processor.extract(content, pattern, flags)
|
||||
return jsonify({'matches': matches})
|
||||
|
||||
except re.error as e:
|
||||
return jsonify({'error': f'Invalid regex pattern: {str(e)}'}), 400
|
||||
except Exception as e:
|
||||
return jsonify({'error': str(e)}), 500
|
||||
|
||||
|
||||
@api_bp.route('/files/download')
|
||||
def download_file():
|
||||
"""
|
||||
下载文件
|
||||
|
||||
Query Parameters:
|
||||
path: 文件路径
|
||||
|
||||
Returns:
|
||||
File: 文件内容
|
||||
"""
|
||||
try:
|
||||
path = request.args.get('path')
|
||||
if not path:
|
||||
return jsonify({'error': 'Missing path parameter'}), 400
|
||||
|
||||
return send_file(path, as_attachment=True)
|
||||
|
||||
except FileNotFoundError:
|
||||
return jsonify({'error': 'File not found'}), 404
|
||||
except Exception as e:
|
||||
return jsonify({'error': str(e)}), 500
|
Reference in New Issue
Block a user