Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
canifa_note
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Vũ Hoàng Anh
canifa_note
Commits
2cfa4c3b
Commit
2cfa4c3b
authored
Dec 23, 2023
by
Steven
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
chore: tweak frontend routes register
parent
aa136a27
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
67 additions
and
82 deletions
+67
-82
rss.go
api/v1/rss.go
+18
-24
frontend.go
server/frontend/frontend.go
+49
-58
No files found.
api/v1/rss.go
View file @
2cfa4c3b
...
@@ -13,14 +13,17 @@ import (
...
@@ -13,14 +13,17 @@ import (
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4"
"github.com/usememos/memos/internal/util"
"github.com/usememos/memos/internal/util"
"github.com/usememos/memos/plugin/gomark/ast"
"github.com/usememos/memos/plugin/gomark/parser"
"github.com/usememos/memos/plugin/gomark/parser"
"github.com/usememos/memos/plugin/gomark/parser/tokenizer"
"github.com/usememos/memos/plugin/gomark/parser/tokenizer"
"github.com/usememos/memos/plugin/gomark/renderer"
"github.com/usememos/memos/plugin/gomark/renderer"
"github.com/usememos/memos/store"
"github.com/usememos/memos/store"
)
)
const
maxRSSItemCount
=
100
const
(
const
maxRSSItemTitleLength
=
100
maxRSSItemCount
=
100
maxRSSItemTitleLength
=
128
)
func
(
s
*
APIV1Service
)
registerRSSRoutes
(
g
*
echo
.
Group
)
{
func
(
s
*
APIV1Service
)
registerRSSRoutes
(
g
*
echo
.
Group
)
{
g
.
GET
(
"/explore/rss.xml"
,
s
.
GetExploreRSS
)
g
.
GET
(
"/explore/rss.xml"
,
s
.
GetExploreRSS
)
...
@@ -171,29 +174,24 @@ func (s *APIV1Service) getSystemCustomizedProfile(ctx context.Context) (*Customi
...
@@ -171,29 +174,24 @@ func (s *APIV1Service) getSystemCustomizedProfile(ctx context.Context) (*Customi
}
}
func
getRSSItemTitle
(
content
string
)
string
{
func
getRSSItemTitle
(
content
string
)
string
{
var
title
string
tokens
:=
tokenizer
.
Tokenize
(
content
)
if
isTitleDefined
(
content
)
{
nodes
,
_
:=
parser
.
Parse
(
tokens
)
title
=
strings
.
Split
(
content
,
"
\n
"
)[
0
][
2
:
]
if
len
(
nodes
)
>
0
{
}
else
{
firstNode
:=
nodes
[
0
]
title
=
strings
.
Split
(
content
,
"
\n
"
)[
0
]
title
:=
renderer
.
NewStringRenderer
()
.
Render
([]
ast
.
Node
{
firstNode
})
var
titleLengthLimit
=
util
.
Min
(
len
(
title
),
maxRSSItemTitleLength
)
return
title
if
titleLengthLimit
<
len
(
title
)
{
}
title
=
title
[
:
titleLengthLimit
]
+
"..."
}
title
:=
strings
.
Split
(
content
,
"
\n
"
)[
0
]
var
titleLengthLimit
=
util
.
Min
(
len
(
title
),
maxRSSItemTitleLength
)
if
titleLengthLimit
<
len
(
title
)
{
title
=
title
[
:
titleLengthLimit
]
+
"..."
}
}
return
title
return
title
}
}
func
getRSSItemDescription
(
content
string
)
(
string
,
error
)
{
func
getRSSItemDescription
(
content
string
)
(
string
,
error
)
{
var
description
string
tokens
:=
tokenizer
.
Tokenize
(
content
)
if
isTitleDefined
(
content
)
{
var
firstLineEnd
=
strings
.
Index
(
content
,
"
\n
"
)
description
=
strings
.
Trim
(
content
[
firstLineEnd
+
1
:
],
" "
)
}
else
{
description
=
content
}
tokens
:=
tokenizer
.
Tokenize
(
description
)
nodes
,
err
:=
parser
.
Parse
(
tokens
)
nodes
,
err
:=
parser
.
Parse
(
tokens
)
if
err
!=
nil
{
if
err
!=
nil
{
return
""
,
err
return
""
,
err
...
@@ -201,7 +199,3 @@ func getRSSItemDescription(content string) (string, error) {
...
@@ -201,7 +199,3 @@ func getRSSItemDescription(content string) (string, error) {
result
:=
renderer
.
NewHTMLRenderer
()
.
Render
(
nodes
)
result
:=
renderer
.
NewHTMLRenderer
()
.
Render
(
nodes
)
return
result
,
nil
return
result
,
nil
}
}
func
isTitleDefined
(
content
string
)
bool
{
return
strings
.
HasPrefix
(
content
,
"# "
)
}
server/frontend/frontend.go
View file @
2cfa4c3b
package
frontend
package
frontend
import
(
import
(
"context"
"fmt"
"fmt"
"html/template"
"html/template"
"net/http"
"net/http"
...
@@ -10,7 +11,7 @@ import (
...
@@ -10,7 +11,7 @@ import (
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/labstack/echo/v4/middleware"
v1
"github.com/usememos/memos/api/v1"
api
v1
"github.com/usememos/memos/api/v1"
"github.com/usememos/memos/internal/util"
"github.com/usememos/memos/internal/util"
"github.com/usememos/memos/plugin/gomark/parser"
"github.com/usememos/memos/plugin/gomark/parser"
"github.com/usememos/memos/plugin/gomark/parser/tokenizer"
"github.com/usememos/memos/plugin/gomark/parser/tokenizer"
...
@@ -19,6 +20,11 @@ import (
...
@@ -19,6 +20,11 @@ import (
"github.com/usememos/memos/store"
"github.com/usememos/memos/store"
)
)
const
(
// maxMetadataDescriptionLength is the maximum length of metadata description.
maxMetadataDescriptionLength
=
256
)
type
FrontendService
struct
{
type
FrontendService
struct
{
Profile
*
profile
.
Profile
Profile
*
profile
.
Profile
Store
*
store
.
Store
Store
*
store
.
Store
...
@@ -31,39 +37,67 @@ func NewFrontendService(profile *profile.Profile, store *store.Store) *FrontendS
...
@@ -31,39 +37,67 @@ func NewFrontendService(profile *profile.Profile, store *store.Store) *FrontendS
}
}
}
}
func
(
s
*
FrontendService
)
Serve
(
e
*
echo
.
Echo
)
{
func
(
s
*
FrontendService
)
Serve
(
ctx
context
.
Context
,
e
*
echo
.
Echo
)
{
// Use echo static middleware to serve the built dist folder.
// Use echo static middleware to serve the built dist folder.
// refer: https://github.com/labstack/echo/blob/master/middleware/static.go
// refer: https://github.com/labstack/echo/blob/master/middleware/static.go
e
.
Use
(
middleware
.
StaticWithConfig
(
middleware
.
StaticConfig
{
e
.
Use
(
middleware
.
StaticWithConfig
(
middleware
.
StaticConfig
{
HTML5
:
true
,
Root
:
"dist"
,
Filesystem
:
http
.
Dir
(
"dist"
)
,
HTML5
:
true
,
Skipper
:
func
(
c
echo
.
Context
)
bool
{
Skipper
:
func
(
c
echo
.
Context
)
bool
{
return
util
.
HasPrefixes
(
c
.
Path
(),
"/api"
,
"/memos.api.v2"
,
"/robots.txt"
,
"/sitemap.xml"
,
"/m/:memoID"
)
return
util
.
HasPrefixes
(
c
.
Path
(),
"/api"
,
"/memos.api.v2"
,
"/robots.txt"
,
"/sitemap.xml"
,
"/m/:memoID"
)
},
},
}))
}))
s
.
registerRoutes
(
e
)
s
.
registerRoutes
(
e
)
s
.
registerFileRoutes
(
ctx
,
e
)
}
}
func
(
s
*
FrontendService
)
registerRoutes
(
e
*
echo
.
Echo
)
{
func
(
s
*
FrontendService
)
registerRoutes
(
e
*
echo
.
Echo
)
{
rawIndexHTML
:=
getRawIndexHTML
()
rawIndexHTML
:=
getRawIndexHTML
()
e
.
GET
(
"/
robots.txt
"
,
func
(
c
echo
.
Context
)
error
{
e
.
GET
(
"/
m/:memoID
"
,
func
(
c
echo
.
Context
)
error
{
ctx
:=
c
.
Request
()
.
Context
()
ctx
:=
c
.
Request
()
.
Context
()
instanceURLSetting
,
err
:=
s
.
Store
.
GetSystemSetting
(
ctx
,
&
store
.
FindSystemSetting
{
memoID
,
err
:=
util
.
ConvertStringToInt32
(
c
.
Param
(
"memoID"
))
Name
:
v1
.
SystemSettingInstanceURLName
.
String
(),
if
err
!=
nil
{
// Redirect to `index.html` if any error occurs.
return
c
.
HTML
(
http
.
StatusOK
,
rawIndexHTML
)
}
memo
,
err
:=
s
.
Store
.
GetMemo
(
ctx
,
&
store
.
FindMemo
{
ID
:
&
memoID
,
})
})
if
err
!=
nil
{
if
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to get instance URL system setting"
)
.
SetInternal
(
err
)
return
c
.
HTML
(
http
.
StatusOK
,
rawIndexHTML
)
}
}
if
instanceURLSetting
==
nil
{
if
memo
==
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Instance URL system setting is not set"
)
return
c
.
HTML
(
http
.
StatusOK
,
rawIndexHTML
)
}
}
instanceURL
:=
instanceURLSetting
.
Value
creator
,
err
:=
s
.
Store
.
GetUser
(
ctx
,
&
store
.
FindUser
{
if
instanceURL
==
""
{
ID
:
&
memo
.
CreatorID
,
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Instance URL system setting is not set"
)
})
if
err
!=
nil
{
return
c
.
HTML
(
http
.
StatusOK
,
rawIndexHTML
)
}
}
// Inject memo metadata into `index.html`.
indexHTML
:=
strings
.
ReplaceAll
(
rawIndexHTML
,
"<!-- memos.metadata -->"
,
generateMemoMetadata
(
memo
,
creator
))
return
c
.
HTML
(
http
.
StatusOK
,
indexHTML
)
})
}
func
(
s
*
FrontendService
)
registerFileRoutes
(
ctx
context
.
Context
,
e
*
echo
.
Echo
)
{
instanceURLSetting
,
err
:=
s
.
Store
.
GetSystemSetting
(
ctx
,
&
store
.
FindSystemSetting
{
Name
:
apiv1
.
SystemSettingInstanceURLName
.
String
(),
})
if
err
!=
nil
||
instanceURLSetting
==
nil
{
return
}
instanceURL
:=
instanceURLSetting
.
Value
if
instanceURL
==
""
{
return
}
e
.
GET
(
"/robots.txt"
,
func
(
c
echo
.
Context
)
error
{
robotsTxt
:=
fmt
.
Sprintf
(
`User-agent: *
robotsTxt
:=
fmt
.
Sprintf
(
`User-agent: *
Allow: /
Allow: /
Host: %s
Host: %s
...
@@ -73,20 +107,6 @@ Sitemap: %s/sitemap.xml`, instanceURL, instanceURL)
...
@@ -73,20 +107,6 @@ Sitemap: %s/sitemap.xml`, instanceURL, instanceURL)
e
.
GET
(
"/sitemap.xml"
,
func
(
c
echo
.
Context
)
error
{
e
.
GET
(
"/sitemap.xml"
,
func
(
c
echo
.
Context
)
error
{
ctx
:=
c
.
Request
()
.
Context
()
ctx
:=
c
.
Request
()
.
Context
()
instanceURLSetting
,
err
:=
s
.
Store
.
GetSystemSetting
(
ctx
,
&
store
.
FindSystemSetting
{
Name
:
v1
.
SystemSettingInstanceURLName
.
String
(),
})
if
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to get instance URL system setting"
)
.
SetInternal
(
err
)
}
if
instanceURLSetting
==
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Instance URL system setting is not set"
)
}
instanceURL
:=
instanceURLSetting
.
Value
if
instanceURL
==
""
{
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Instance URL system setting is not set"
)
}
urlsets
:=
[]
string
{}
urlsets
:=
[]
string
{}
// Append memo list.
// Append memo list.
memoList
,
err
:=
s
.
Store
.
ListMemos
(
ctx
,
&
store
.
FindMemo
{
memoList
,
err
:=
s
.
Store
.
ListMemos
(
ctx
,
&
store
.
FindMemo
{
...
@@ -110,35 +130,6 @@ Sitemap: %s/sitemap.xml`, instanceURL, instanceURL)
...
@@ -110,35 +130,6 @@ Sitemap: %s/sitemap.xml`, instanceURL, instanceURL)
sitemap
:=
fmt
.
Sprintf
(
`<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:news="http://www.google.com/schemas/sitemap-news/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:mobile="http://www.google.com/schemas/sitemap-mobile/1.0" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">%s</urlset>`
,
strings
.
Join
(
urlsets
,
"
\n
"
))
sitemap
:=
fmt
.
Sprintf
(
`<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:news="http://www.google.com/schemas/sitemap-news/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:mobile="http://www.google.com/schemas/sitemap-mobile/1.0" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">%s</urlset>`
,
strings
.
Join
(
urlsets
,
"
\n
"
))
return
c
.
XMLBlob
(
http
.
StatusOK
,
[]
byte
(
sitemap
))
return
c
.
XMLBlob
(
http
.
StatusOK
,
[]
byte
(
sitemap
))
})
})
e
.
GET
(
"/m/:memoID"
,
func
(
c
echo
.
Context
)
error
{
ctx
:=
c
.
Request
()
.
Context
()
memoID
,
err
:=
util
.
ConvertStringToInt32
(
c
.
Param
(
"memoID"
))
if
err
!=
nil
{
// Redirect to `index.html` if any error occurs.
return
c
.
HTML
(
http
.
StatusOK
,
rawIndexHTML
)
}
memo
,
err
:=
s
.
Store
.
GetMemo
(
ctx
,
&
store
.
FindMemo
{
ID
:
&
memoID
,
})
if
err
!=
nil
{
return
c
.
HTML
(
http
.
StatusOK
,
rawIndexHTML
)
}
if
memo
==
nil
{
return
c
.
HTML
(
http
.
StatusOK
,
rawIndexHTML
)
}
creator
,
err
:=
s
.
Store
.
GetUser
(
ctx
,
&
store
.
FindUser
{
ID
:
&
memo
.
CreatorID
,
})
if
err
!=
nil
{
return
c
.
HTML
(
http
.
StatusOK
,
rawIndexHTML
)
}
// Inject memo metadata into `index.html`.
indexHTML
:=
strings
.
ReplaceAll
(
rawIndexHTML
,
"<!-- memos.metadata -->"
,
generateMemoMetadata
(
memo
,
creator
))
return
c
.
HTML
(
http
.
StatusOK
,
indexHTML
)
})
}
}
func
generateMemoMetadata
(
memo
*
store
.
Memo
,
creator
*
store
.
User
)
string
{
func
generateMemoMetadata
(
memo
*
store
.
Memo
,
creator
*
store
.
User
)
string
{
...
@@ -154,8 +145,8 @@ func generateMemoMetadata(memo *store.Memo, creator *store.User) string {
...
@@ -154,8 +145,8 @@ func generateMemoMetadata(memo *store.Memo, creator *store.User) string {
if
len
(
description
)
==
0
{
if
len
(
description
)
==
0
{
description
=
memo
.
Content
description
=
memo
.
Content
}
}
if
len
(
description
)
>
200
{
if
len
(
description
)
>
maxMetadataDescriptionLength
{
description
=
description
[
:
200
]
+
"..."
description
=
description
[
:
maxMetadataDescriptionLength
]
+
"..."
}
}
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment