Skip to content

Commit 2cfc698

Browse files
committed
PostMeta에 slug 필드 추가, url 복사 개선
1 parent ee3f34c commit 2cfc698

File tree

5 files changed

+31
-11
lines changed

5 files changed

+31
-11
lines changed

lib/model/post_meta.dart

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,13 @@ class PostMeta {
77
final String title;
88
final String category;
99
final DateTime? date;
10+
final String slug;
1011

11-
PostMeta({required this.title, required this.category, this.date});
12+
PostMeta(
13+
{required this.title,
14+
required this.category,
15+
this.date,
16+
required this.slug});
1217

1318
factory PostMeta.fromJson(Map<String, dynamic> json) {
1419
final rawTitle = (json['title'] ?? '').toString().trim();
@@ -21,13 +26,21 @@ class PostMeta {
2126
dt = DateTime.parse(rawDate);
2227
} catch (_) {}
2328
}
24-
return PostMeta(title: rawTitle, category: rawCategory, date: dt);
29+
30+
final rawSlug = Uri.encodeComponent((json['slug'] ?? '')
31+
.toString()
32+
.trim()
33+
.replaceAll(RegExp(r'^-+|-+$'), ''));
34+
35+
return PostMeta(
36+
title: rawTitle, category: rawCategory, date: dt, slug: rawSlug);
2537
}
2638

2739
PostMeta withFallbacks({required String fallbackTitle}) => PostMeta(
2840
title: title.isNotEmpty ? title : fallbackTitle,
2941
category: category.isNotEmpty ? category : 'Uncategorized',
3042
date: date,
43+
slug: slug,
3144
);
3245
}
3346

lib/pages/post_list_page.dart

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import 'package:flutter/material.dart';
22
import 'package:flutter/services.dart';
33
import 'package:intl/intl.dart';
4-
import 'dart:html' as html;
4+
import 'package:web/web.dart' as web;
5+
import 'dart:js_interop';
6+
57
import 'package:markdown_widget/markdown_widget.dart';
68
import 'package:sando_diary/model/post_meta.dart';
79

@@ -25,15 +27,17 @@ class _PostListPageState extends State<PostListPage> {
2527
}
2628

2729
void _launchURLInNewTab(String url) {
28-
html.window.open(url, '_blank');
30+
web.window.open(url, '_blank');
2931
}
3032

3133
void _copyCurrentPostUrlToClipboard() {
32-
final currentUrl = html.window.location.href; // 현재 페이지의 URL 가져오기
33-
Clipboard.setData(ClipboardData(text: currentUrl)); // URL을 클립보드에 복사
34+
final postUrl =
35+
Uri.base.resolve('/posts/${currentPost.meta.slug}').toString();
36+
print('Copy URL: $postUrl');
37+
Clipboard.setData(ClipboardData(text: postUrl));
3438

3539
ScaffoldMessenger.of(context).showSnackBar(
36-
SnackBar(content: Text('URL copied to clipboard!')),
40+
const SnackBar(content: Text('URL copied to clipboard!')),
3741
);
3842
}
3943

@@ -52,7 +56,7 @@ class _PostListPageState extends State<PostListPage> {
5256
data: currentDoc.body,
5357
config: MarkdownConfig(configs: [
5458
LinkConfig(
55-
style: TextStyle(
59+
style: const TextStyle(
5660
color: Colors.cyan,
5761
decoration: TextDecoration.underline,
5862
),
@@ -72,7 +76,7 @@ class _PostListPageState extends State<PostListPage> {
7276
return Scaffold(
7377
appBar: !isShowPostDetail
7478
? AppBar(
75-
title: Text('Blog Posts'),
79+
title: const Text('Blog Posts'),
7680
)
7781
: AppBar(
7882
elevation: 0,
@@ -84,7 +88,7 @@ class _PostListPageState extends State<PostListPage> {
8488
leading: BackButton(
8589
onPressed: () => setState(() {
8690
isShowPostDetail = false;
87-
html.window.history.pushState(null, 'Posts', '/');
91+
web.window.history.pushState(null, 'Posts', '/');
8892
}),
8993
),
9094
actions: [

post/markdown_example.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
title: 예제 마크다운 Test...!
33
date: 2024-08-14
44
category: Test
5+
slug: example_markdown
56
---
67

78
# h1 Heading 8-)

pubspec.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ packages:
470470
source: hosted
471471
version: "15.0.0"
472472
web:
473-
dependency: transitive
473+
dependency: "direct main"
474474
description:
475475
name: web
476476
sha256: "868d88a33d8a87b18ffc05f9f030ba328ffefba92d6c127917a2ba740f9cfe4a"

pubspec.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ dependencies:
1919
markdown_widget: ^2.3.2+6
2020
cosmic_frontmatter: ^1.0.3
2121
intl: ^0.20.2
22+
2223
yaml: ^3.1.2
2324
url_launcher: ^6.0.20
25+
web: ^1.1.1
2426

2527
dev_dependencies:
2628
flutter_test:

0 commit comments

Comments
 (0)