Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:03,000 --> 00:00:06,574
We configured the Tailwind Typography plugin
2
00:00:06,574 --> 00:00:09,714
to properly style our review article.
3
00:00:09,714 --> 00:00:12,303
Now, since we're loading the content
4
00:00:12,303 --> 00:00:14,460
from a separate Markdown file,
5
00:00:14,532 --> 00:00:17,347
maybe we could keep all the data there,
6
00:00:17,347 --> 00:00:20,071
not just the article text, but also
7
00:00:20,071 --> 00:00:22,328
the title and the image path.
8
00:00:22,406 --> 00:00:24,893
This way we could make this component
9
00:00:24,893 --> 00:00:26,574
the same for all reviews,
10
00:00:26,641 --> 00:00:29,169
and just display different data from
11
00:00:29,169 --> 00:00:31,487
a different file for each review.
12
00:00:31,557 --> 00:00:34,424
Now, there is a way to add properties
13
00:00:34,424 --> 00:00:35,896
to a Markdown file.
14
00:00:35,974 --> 00:00:39,478
At the top we can create a "front matter" section,
15
00:00:39,478 --> 00:00:43,697
delimited by 3 "-" characters before and after.
16
00:00:43,697 --> 00:00:46,208
And here we can set arbitrary
17
00:00:46,208 --> 00:00:48,459
properties in YAML format.
18
00:00:48,546 --> 00:00:51,440
Usually you just write a property name,
19
00:00:51,440 --> 00:00:52,405
like "title",
20
00:00:52,479 --> 00:00:55,258
followed by a ":", and the value,
21
00:00:55,258 --> 00:00:57,030
that's typically a string.
22
00:00:57,030 --> 00:00:59,365
Let me put the Markdown file and
23
00:00:59,365 --> 00:01:01,701
the page component side by side,
24
00:01:01,774 --> 00:01:04,141
so we can simply copy the values
25
00:01:04,141 --> 00:01:05,546
from the page code.
26
00:01:05,620 --> 00:01:07,351
This will be the "title".
27
00:01:07,351 --> 00:01:09,484
Then we can set the "image",
28
00:01:09,531 --> 00:01:13,347
that's really the path, to be used as the img src.
29
00:01:13,411 --> 00:01:15,611
Maybe we could also add a "date",
30
00:01:15,611 --> 00:01:18,341
to show when the review was published.
31
00:01:18,341 --> 00:01:22,747
As the value it's best to use the ISO date format,
32
00:01:22,747 --> 00:01:26,292
so this means the May 4th 2023.
33
00:01:26,292 --> 00:01:29,587
To extract this data from the front matter
34
00:01:29,587 --> 00:01:31,809
we'll need to use another package
35
00:01:31,809 --> 00:01:33,224
called "gray-matter".
36
00:01:33,291 --> 00:01:35,160
There are other libraries that
37
00:01:35,160 --> 00:01:36,780
can do the same of course,
38
00:01:36,843 --> 00:01:38,665
but this one is pretty popular.
39
00:01:38,665 --> 00:01:41,879
We can use it to parse our Markdown file,
40
00:01:41,879 --> 00:01:44,135
and it will return an object that
41
00:01:44,135 --> 00:01:46,050
contains both the "content",
42
00:01:46,118 --> 00:01:49,249
that in our case will be the Markdown text,
43
00:01:49,249 --> 00:01:53,047
and the "data" extracted from the front matter.
44
00:01:53,047 --> 00:01:56,684
The "data" object will have all the properties,
45
00:01:56,684 --> 00:01:57,999
like "title" etc.
46
00:01:58,077 --> 00:02:00,306
So let's go back to our project,
47
00:02:00,577 --> 00:02:03,349
I'll rearrange these panels first.
48
00:02:03,417 --> 00:02:05,790
Now, let's install that new package.
49
00:02:05,790 --> 00:02:09,170
We need to run "npm install gray-matter",
50
00:02:09,170 --> 00:02:11,243
and just wait a few seconds.
51
00:02:11,691 --> 00:02:13,591
Now I'll restart the server,
52
00:02:14,231 --> 00:02:16,235
and let's see how to get the
53
00:02:16,235 --> 00:02:17,881
front matter properties
54
00:02:17,952 --> 00:02:20,298
in the component, where we already
55
00:02:20,298 --> 00:02:21,884
read the Markdown file.
56
00:02:21,953 --> 00:02:25,074
I'll also show the browser on the right side.
57
00:02:25,074 --> 00:02:27,900
And we're finally ready to use the new library.
58
00:02:27,900 --> 00:02:31,036
We want to import the "matter" function
59
00:02:31,036 --> 00:02:33,784
from the "gray-matter" module.
60
00:02:33,784 --> 00:02:35,751
Now we can use this function,
61
00:02:36,004 --> 00:02:39,722
before converting the Markdown to HTML.
62
00:02:39,722 --> 00:02:42,819
Because we want "matter" to parse the full
63
00:02:42,819 --> 00:02:45,325
text that we loaded from the file.
64
00:02:45,399 --> 00:02:47,195
This will return an object
65
00:02:47,195 --> 00:02:48,992
with a "content" property.
66
00:02:49,061 --> 00:02:52,373
The "content" in our case is the Markdown,
67
00:02:52,373 --> 00:02:54,218
so that's what we want to pass
68
00:02:54,218 --> 00:02:55,755
to the "marked" function.
69
00:02:55,817 --> 00:02:58,834
But then we'll also get a "data" property,
70
00:02:58,834 --> 00:03:00,812
that will have all the values
71
00:03:00,812 --> 00:03:02,313
from the front matter.
72
00:03:02,381 --> 00:03:04,821
Let's start by logging this "data",
73
00:03:04,821 --> 00:03:07,531
so we can check exactly what it contains.
74
00:03:07,741 --> 00:03:10,211
Let's keep an eye on the server logs,
75
00:03:10,211 --> 00:03:11,546
and reload the page.
76
00:03:11,613 --> 00:03:13,059
It's compiling.
77
00:03:13,233 --> 00:03:15,972
And you can now see the data object here:
78
00:03:15,972 --> 00:03:18,678
it has "title", "date", and "image",
79
00:03:18,678 --> 00:03:21,522
just like we wrote in the front matter.
80
00:03:21,522 --> 00:03:24,088
And by the way, the Markdown is
81
00:03:24,088 --> 00:03:26,489
still being rendered as HTML,
82
00:03:26,572 --> 00:03:29,011
which proves we're using the "content"
83
00:03:29,011 --> 00:03:30,231
property correctly.
84
00:03:30,295 --> 00:03:32,986
But now we can extract the properties
85
00:03:32,986 --> 00:03:34,659
from the "data" object,
86
00:03:34,732 --> 00:03:37,148
since we know it contains "title",
87
00:03:37,148 --> 00:03:38,570
"date", and "image".
88
00:03:38,641 --> 00:03:41,397
At this point we can remove this log.
89
00:03:41,397 --> 00:03:43,868
Then display the "title" variable
90
00:03:43,868 --> 00:03:45,291
inside the Heading.
91
00:03:45,366 --> 00:03:48,376
And we can also use the "image" property
92
00:03:48,586 --> 00:03:51,056
as the "src" for the img element.
93
00:03:51,056 --> 00:03:54,468
Let's double check that the page is still working.
94
00:03:54,468 --> 00:03:56,802
It still looks exactly the same.
95
00:03:56,802 --> 00:03:59,181
Now, let's also display the date,
96
00:03:59,181 --> 00:04:00,910
maybe after the Heading.
97
00:04:00,982 --> 00:04:03,486
I'll just put it in a paragraph tag.
98
00:04:03,942 --> 00:04:05,717
And if we save, you can see
99
00:04:05,717 --> 00:04:07,426
the date before the image.
100
00:04:07,491 --> 00:04:09,881
Let's add a bit of styling as well.
101
00:04:09,881 --> 00:04:11,743
We could make it italic,
102
00:04:11,743 --> 00:04:14,346
and add some padding at the bottom.
103
00:04:14,346 --> 00:04:16,161
Ok, that looks good.
104
00:04:16,161 --> 00:04:17,773
So, you can see that
105
00:04:17,773 --> 00:04:21,115
by keeping all the data in the Markdown file,
106
00:04:21,115 --> 00:04:23,963
the JSX elements in this component
107
00:04:23,963 --> 00:04:27,100
no longer have any hard-coded values.
108
00:04:27,100 --> 00:04:29,064
In the next couple of videos
109
00:04:29,064 --> 00:04:32,528
we'll make this entire component fully reusable
110
00:04:32,528 --> 00:04:36,712
and capable of displaying the data for any review.
7915
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.