feat(content-type): add content_type field, markdown auto-conversion, and API doc updates

- Add content_type column to ul_post via migration
- Install league/commonmark for markdown->HTML conversion
- Add Post model accessors/setters for content_type and content
- Update API Articles controller save/update with content_type support
- Update API docs with content_type parameter and markdown example

Closes content-type-support plan
This commit is contained in:
augushong
2026-04-29 20:46:44 +08:00
parent 0e8944bc7f
commit 6f332467df
5 changed files with 104 additions and 2 deletions

View File

@@ -8,6 +8,7 @@ use app\BaseController;
use app\model\Post;
use app\model\PostCategory;
use app\model\PostTag;
use League\CommonMark\CommonMarkConverter;
use think\Request;
class Articles extends BaseController
@@ -105,6 +106,21 @@ class Articles extends BaseController
if (!isset($post_data['status'])) {
$post_data['status'] = 0;
}
if (!isset($post_data['content_type'])) {
$post_data['content_type'] = 'html';
}
// content_type 白名单校验
$allowed_content_types = ['html', 'markdown'];
if (!in_array($post_data['content_type'], $allowed_content_types)) {
return json_message('无效的content_type', 500);
}
// Markdown→HTML 自动转换
if ($post_data['content_type'] === 'markdown' && !empty($post_data['content'])) {
$converter = new CommonMarkConverter(['html_input' => 'strip', 'allow_unsafe_links' => false]);
$post_data['content_html'] = $converter->convert($post_data['content']);
}
$model_post = Post::create($post_data);
@@ -201,6 +217,12 @@ class Articles extends BaseController
}
}
// Markdown→HTML 自动转换
if (isset($post_data['content_type']) && $post_data['content_type'] === 'markdown' && !empty($post_data['content'])) {
$converter = new CommonMarkConverter(['html_input' => 'strip', 'allow_unsafe_links' => false]);
$post_data['content_html'] = $converter->convert($post_data['content']);
}
$model_post->save($post_data);
return json_message([], 0, '更新成功');