提交 8981d88a 编写于 作者: 李少辉-开发者's avatar 李少辉-开发者 🎧

add sitemap path to robots txt

Signed-off-by: 李少辉-开发者's avatarlish <lish@csdn.net>
上级 33830078
User-agent: *
Allow:/
Sitemap: https://docs.gitcode.net/sitemap.xml
\ No newline at end of file
Sitemap: https://docs.gitcode.net/sitemap.xml
Sitemap: https://docs.gitcode.net/spring/guide/sitemap.xml
\ No newline at end of file
......@@ -8,4 +8,8 @@
<loc>https://docs.gitcode.net/spring/guide/sitemap.xml</loc>
<lastmod>"2022-04-01T10:27:44.390Z"</lastmod>
</sitemap>
<sitemap>
<loc>https://docs.gitcode.net/spring/guide/sitemap.xml</loc>
<lastmod>"2022-04-01T10:45:44.701Z"</lastmod>
</sitemap>
</sitemapindex>
\ No newline at end of file
......@@ -5,6 +5,7 @@ const builder = new xml2js.Builder();
const sitemap_path = "./public/sitemap.xml"
const robot_path = "./public/robots.txt"
const allowed_sitemaps = [
'https://docs.gitcode.net/spring/guide/sitemap.xml'
......@@ -12,7 +13,7 @@ const allowed_sitemaps = [
// 读取xml 文件内容
function get_sitemap_content(sitemap_path){
function get_file_content(sitemap_path){
return fs.readFileSync(sitemap_path)
}
// 解析xml
......@@ -37,7 +38,18 @@ function write_to_file(path, content){
fs.writeFileSync(path, content)
}
var sitemap_string = get_sitemap_content(sitemap_path)
function check_robot(){
var robot_string = get_file_content(robot_path).toString()
console.log(robot_string)
allowed_sitemaps.forEach(function(item){
if(robot_string.indexOf(item)<0){
robot_string = robot_string + "\n" + "Sitemap: " + item
}
})
write_to_file(robot_path, robot_string)
}
var sitemap_string = get_file_content(sitemap_path)
parse_sitemap_content(sitemap_string).then(function(sitemap_json){
allowed_sitemaps.forEach(function(item){
......@@ -49,6 +61,7 @@ parse_sitemap_content(sitemap_string).then(function(sitemap_json){
return build_sitemap_from_json_content(sitemap_json)
}).then(function(xml){
write_to_file(sitemap_path, xml)
check_robot()
console.log('Done')
}).catch(function(err){
console.log(err)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册