Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rss feed): added xslt for rss feed #9252

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

exports[`atom filters to the first two entries 1`] = `
[
"<?xml version="1.0" encoding="utf-8"?>
"<?xml version="1.0" encoding="utf-8"?><?xml-stylesheet type="text/xsl" href="/rss.xslt"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<id>https://docusaurus.io/myBaseUrl/blog</id>
<title>Hello Blog</title>
Expand Down Expand Up @@ -48,7 +48,7 @@ exports[`atom filters to the first two entries 1`] = `

exports[`atom filters to the first two entries using limit 1`] = `
[
"<?xml version="1.0" encoding="utf-8"?>
"<?xml version="1.0" encoding="utf-8"?><?xml-stylesheet type="text/xsl" href="/rss.xslt"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<id>https://docusaurus.io/myBaseUrl/blog</id>
<title>Hello Blog</title>
Expand Down Expand Up @@ -94,7 +94,7 @@ exports[`atom filters to the first two entries using limit 1`] = `

exports[`atom has feed item for each post 1`] = `
[
"<?xml version="1.0" encoding="utf-8"?>
"<?xml version="1.0" encoding="utf-8"?><?xml-stylesheet type="text/xsl" href="/rss.xslt"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<id>https://docusaurus.io/myBaseUrl/blog</id>
<title>Hello Blog</title>
Expand Down Expand Up @@ -380,7 +380,7 @@ exports[`json has feed item for each post 1`] = `

exports[`rss filters to the first two entries 1`] = `
[
"<?xml version="1.0" encoding="utf-8"?>
"<?xml version="1.0" encoding="utf-8"?><?xml-stylesheet type="text/xsl" href="/rss.xslt"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
<title>Hello Blog</title>
Expand Down Expand Up @@ -428,7 +428,7 @@ exports[`rss filters to the first two entries 1`] = `

exports[`rss filters to the first two entries using limit 1`] = `
[
"<?xml version="1.0" encoding="utf-8"?>
"<?xml version="1.0" encoding="utf-8"?><?xml-stylesheet type="text/xsl" href="/rss.xslt"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
<title>Hello Blog</title>
Expand Down Expand Up @@ -476,7 +476,7 @@ exports[`rss filters to the first two entries using limit 1`] = `

exports[`rss has feed item for each post 1`] = `
[
"<?xml version="1.0" encoding="utf-8"?>
"<?xml version="1.0" encoding="utf-8"?><?xml-stylesheet type="text/xsl" href="/rss.xslt"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
<title>Hello Blog</title>
Expand Down
17 changes: 15 additions & 2 deletions packages/docusaurus-plugin-content-blog/src/feed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ async function createBlogFeedFile({
feedType: FeedType;
generatePath: string;
}) {
const [feedContent, feedPath] = (() => {
const feedDetails = (() => {
switch (feedType) {
case 'rss':
return [feed.rss2(), 'rss.xml'];
Expand All @@ -187,7 +187,20 @@ async function createBlogFeedFile({
}
})();
try {
await fs.outputFile(path.join(generatePath, feedPath), feedContent);
const xsltLink =
'<?xml version="1.0" encoding="utf-8"?><?xml-stylesheet type="text/xsl" href="/rss.xslt"?>';

if (feedDetails[1] !== 'feed.json' && feedDetails[0]) {
feedDetails[0] = feedDetails[0]?.replace(
'<?xml version="1.0" encoding="utf-8"?>',
xsltLink,
);
}
Xebec19 marked this conversation as resolved.
Show resolved Hide resolved

await fs.outputFile(
path.join(generatePath, `${feedDetails[1]}`),
feedDetails[0],
);
} catch (err) {
logger.error(`Generating ${feedType} feed failed.`);
throw err;
Expand Down
33 changes: 33 additions & 0 deletions website/static/rss.xslt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<html>
<head>
<title>Blog Information</title>
<link rel="stylesheet" type="text/css" href="/styles.css" />
</head>
<body>
<main class="markdown">
<div class="description">
<h1><xsl:value-of select="rss/channel/title"/></h1>
<p>Description: <xsl:value-of select="rss/channel/description"/></p>
<p>Last Build Date: <xsl:value-of select="substring(rss/channel/lastBuildDate,0,11)"/></p>
<p>Language: <xsl:value-of select="rss/channel/language"/></p>
</div>
<h2>Recent Posts</h2>
<ul class="postsList">
<xsl:for-each select="rss/channel/item">
<li>
<a href="{link}">
<xsl:value-of select="title"/>
</a>
</li>
</xsl:for-each>
</ul>
</main>
</body>
</html>
</xsl:template>

</xsl:stylesheet>
Xebec19 marked this conversation as resolved.
Show resolved Hide resolved
35 changes: 35 additions & 0 deletions website/static/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

.markdown {
padding: 4rem;
margin: auto;
text-align: center;
/* stylelint-disable-next-line font-family-name-quotes */
font-family: system-ui, -apple-system, Segoe UI, Roboto, Ubuntu;
line-height: 1.65;
}

.col,
.container {
padding: 0 var(--ifm-spacing-horizontal);
width: 100%;
}

.markdown li,
body {
word-wrap: break-word;
list-style: none;
}

body,
ol ol,
ol ul,
ul ol,
ul ul {
margin: 0 auto;
}
Xebec19 marked this conversation as resolved.
Show resolved Hide resolved