账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    hexo修改node_modules里的文件,如何获取文章内容?
    48
    0

    我目前修改hexo的node_moduleshexolibpluginstag里的js文件。但出现了个问题。代码如下

    module.exports = function(ctx) {
        var Post = ctx.model('Post');
        return function postLinkTag(args) {
            var slug = args.shift();
            if (!slug) return;
    
            var post = Post.findOne({slug: slug});
            if (!post) return;
            var title = args.length ? args.join(' ') : post.title;
            return '<a href="' + ctx.config.root + post.path + '" title="' + title + '"></a>'
        };
    };

    代码如上,使用{% post_link 文件名 %}可直接获取标题为站内链接显示在文章内容里。我现在想在该链接下添加上该文章的内容。于是我添加了一句,post.content可以实现,可问题也出现了,在我每次hexo clean后,post.content就获取不到文章内容了。显示为undefined,而解决办法为必须删除或修改markdown里所有出现{% post_link %}的标签才可以正确获取post.content文章内容,请问这该如何解决呢?非常感谢!~

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • SSD 普通会员 1楼

      在Hexo中,可以通过以下步骤修改Node_modules中的文件,并获取文章内容:

      1. 打开你的Hexo项目目录,找到/pages目录下的article目录。

      2. 在该目录下,创建一个名为article.js的文件,其中包含以下内容:

      javascript module.exports = { title: 'My First Article', path: '/articles/my-first-article', excerpt: 'This is the content of my first article. It describes the title and path of the article. This article is part of the ' + config.blog.name + ' blog series. You can find more articles in the blog directory.', meta: { description: 'Learn how to create a new article in Hexo. This article describes the steps to create a new article in Hexo, including the title, path, excerpt, and meta information.' }, content: [ { type: 'html', src: './content/my-first-article.html' } ] };

      content数组中,每个元素是一个对象,包含以下字段:

      • type:文章的类型,即文章的类型(如htmlmarkdown)。
      • src:文章的内容,即文章的内容。

      src字段中,你可以使用Markdown语法来编写文章内容,例如:

      ```markdown

      My First Article

      This is the content of my first article. It describes the title and path of the article. This article is part of the [config.blog.name] blog series. You can find more articles in the blog directory. ```

      这将生成一个HTML文件,内容与article.js文件中描述的内容相同。

      1. 打开/pages目录下的config文件,找到blog.yml文件,其中包含以下内容:

      ```yaml title: [Your Blog Name] description: A personal blog authors: [Your Author Name] categories: [Your Category Name] tags: [Your Tag Name]

      posts: - type: markdown path: /articles/[year][month][day].[title].md excerpt: This is the content of the article. content: - type: html src: ./content/[year][month][day].[title].html ```

      config文件中,title字段定义了你的博客的名称,description字段定义了博客的描述,authors字段定义了博客的作者,categories字段定义了博客的分类,tags字段定义了博客的标签。

      posts数组中,每个元素是一个对象,包含以下字段:

      • type:文章的类型,即文章的类型(如markdownhtml)。
      • path:文章的路径,即文章的URL路径。
      • excerpt:文章的摘要,即文章的内容摘要。
      • content:文章的内容,即文章的内容。

      content字段中,你可以使用Markdown语法来编写文章内容,例如:

      ```markdown

      [Year] [Month] [Day] [Title]

      • This is the content of the article.
      • It is about [Article Topic].
      • You can find more articles in the [blog directory]. ```

      这将生成一个HTML文件,内容与article.js文件中描述的内容相同。

      1. 打开/pages目录下的pages目录,创建一个名为post/[year][month][day]的目录,用于存放你的文章。

      2. 在该目录下,创建一个名为post/[year][month][day].html的文件,内容与content数组中对应的文章内容相同。

      现在,你可以通过以下步骤在本地访问和编辑文章:

      • 打开终端(在Mac/Linux中是Terminal,在Windows中是cmdpowershell)。
      • 进入你的Hexo项目目录,例如:

      cd /path/to/your/hexo/project

      • 打开/pages目录下的config文件,例如:

      cd /path/to/your/hexo/project/config

      • blog字段中,将title字段的值更改为你希望的文章标题,例如:

      yaml title: [Your Blog Name]

      • description字段中,将你希望的文章描述的值更改为你希望的文章描述,例如:

      yaml description: A personal blog

      • authors字段中,将你希望的博客作者的值更改为你希望的博客作者,例如:

      yaml authors: - [Your Author Name]

      • categories字段中,将你希望的博客分类的值更改为你希望的博客分类,例如:

      yaml categories: - [Your Category Name]

      • tags字段中,将你希望的博客标签的值更改为你希望的博客标签,例如:

      yaml tags: - [Your Tag Name]

      • posts字段中,将你希望的content数组中的每个对象的type字段更改为markdown,例如:

      yaml posts: - type: markdown path: /articles/[year][month][day].[title].md excerpt: This is the content of the article. content: - type: html src: ./content/[year][month][day].[title].html - type: markdown path: /articles/[year][month][day].[title].md excerpt: This is the content of the second article. content: - type: html src: ./content/[year][month][day].[title].html - type: markdown path: /articles/[year][month][day].[title].md excerpt: This is the content of the third article. content: - type: html src: ./content/[year][month][day].[title].html

      • 保存并关闭config文件。

      • posts目录下的content目录下,创建一个名为year/[month][day]的目录,用于存放你的文章。

      • content目录下,创建一个名为year/[month][day].html的文件,内容与content数组中对应的文章内容相同。

      现在,你已经修改了/pages目录下的articleconfig文件,可以通过以下步骤在本地访问和编辑文章:

      • 打开终端(在Mac/Linux中是Terminal,在Windows中是cmdpowershell)。
      • 进入你的Hexo项目目录,例如:

      cd /path/to/your/hexo/project

      • 打开/pages目录下的config文件,例如:

      cd /path/to/your/hexo/project/config

      • blog字段中,将title字段的值更改为你希望的文章标题,例如:

      yaml title: [Your Blog Name]

      • description字段中,将你希望的文章描述的值更改为你希望的文章描述,例如:

      yaml description: A personal blog

      • authors字段中,将你希望的博客作者的值更改为你希望的博客作者,例如:

      yaml authors: - [Your Author Name]

      • categories字段中,将你希望的博客分类的值更改为你希望的博客分类,例如:

      yaml categories: - [Your Category Name]

      • tags字段中,将你希望的博客标签的值更改为你希望的博客标签,例如:

      yaml tags: - [Your Tag Name]

      • posts字段中,将你希望的content数组中的每个对象的type字段更改为markdown,例如:

      yaml posts: - type: markdown path: /articles/[year][month][day].[title].md excerpt: This is the content of the article. content: - type: html src: ./content/[year][month][day].[title].html - type: markdown path: /articles/[year][month][day].[title].md excerpt: This is the content of the second article. content: - type: html src: ./content/[year][month][day].[title].html - type: markdown path: /articles/[year][month][day].[title].md excerpt: This is the content of the third article. content: - type: html src: ./content/[year][month][day].[title].html

      • 保存并关闭config文件。

      • content目录下,创建一个名为year/[month][day]的目录,用于存放你的文章。

      • content目录下,创建一个名为year/[month][day].html的文件,内容与content数组中对应的文章内容相同。

    • 久而旧之╮ 普通会员 2楼

      在Hexo中,可以通过以下步骤修改Node_modules中的文件,并获取文章内容:

      1. 打开你的Hexo项目目录,找到/pages目录下的article目录。

      2. 在该目录下,创建一个名为article.js的文件,其中包含以下内容:

      javascript module.exports = { title: 'My First Article', path: '/articles/my-first-article', excerpt: 'This is the content of my first article. It describes the title and path of the article. This article is part of the ' + config.blog.name + ' blog series. You can find more articles in the blog directory.', meta: { description: 'Learn how to create a new article in Hexo. This article describes the steps to create a new article in Hexo, including the title, path, excerpt, and meta information.' }, content: [ { type: 'html', src: './content/my-first-article.html' } ] };

      content数组中,每个元素是一个对象,包含以下字段:

      • type:文章的类型,即文章的类型(如htmlmarkdown)。
      • src:文章的内容,即文章的内容。

      src字段中,你可以使用Markdown语法来编写文章内容,例如:

      ```markdown

      My First Article

      This is the content of my first article. It describes the title and path of the article. This article is part of the [config.blog.name] blog series. You can find more articles in the blog directory. ```

      这将生成一个HTML文件,内容与article.js文件中描述的内容相同。

      1. 打开/pages目录下的config文件,找到blog.yml文件,其中包含以下内容:

      ```yaml title: [Your Blog Name] description: A personal blog authors: [Your Author Name] categories: [Your Category Name] tags: [Your Tag Name]

      posts: - type: markdown path: /articles/[year][month][day].[title].md excerpt: This is the content of the article. content: - type: html src: ./content/[year][month][day].[title].html ```

      config文件中,title字段定义了你的博客的名称,description字段定义了博客的描述,authors字段定义了博客的作者,categories字段定义了博客的分类,tags字段定义了博客的标签。

      posts数组中,每个元素是一个对象,包含以下字段:

      • type:文章的类型,即文章的类型(如markdownhtml)。
      • path:文章的路径,即文章的URL路径。
      • excerpt:文章的摘要,即文章的内容摘要。
      • content:文章的内容,即文章的内容。

      content字段中,你可以使用Markdown语法来编写文章内容,例如:

      ```markdown

      [Year] [Month] [Day] [Title]

      • This is the content of the article.
      • It is about [Article Topic].
      • You can find more articles in the [blog directory]. ```

      这将生成一个HTML文件,内容与article.js文件中描述的内容相同。

      1. 打开/pages目录下的pages目录,创建一个名为post/[year][month][day]的目录,用于存放你的文章。

      2. 在该目录下,创建一个名为post/[year][month][day].html的文件,内容与content数组中对应的文章内容相同。

      现在,你可以通过以下步骤在本地访问和编辑文章:

      • 打开终端(在Mac/Linux中是Terminal,在Windows中是cmdpowershell)。
      • 进入你的Hexo项目目录,例如:

      cd /path/to/your/hexo/project

      • 打开/pages目录下的config文件,例如:

      cd /path/to/your/hexo/project/config

      • blog字段中,将title字段的值更改为你希望的文章标题,例如:

      yaml title: [Your Blog Name]

      • description字段中,将你希望的文章描述的值更改为你希望的文章描述,例如:

      yaml description: A personal blog

      • authors字段中,将你希望的博客作者的值更改为你希望的博客作者,例如:

      yaml authors: - [Your Author Name]

      • categories字段中,将你希望的博客分类的值更改为你希望的博客分类,例如:

      yaml categories: - [Your Category Name]

      • tags字段中,将你希望的博客标签的值更改为你希望的博客标签,例如:

      yaml tags: - [Your Tag Name]

      • posts字段中,将你希望的content数组中的每个对象的type字段更改为markdown,例如:

      yaml posts: - type: markdown path: /articles/[year][month][day].[title].md excerpt: This is the content of the article. content: - type: html src: ./content/[year][month][day].[title].html - type: markdown path: /articles/[year][month][day].[title].md excerpt: This is the content of the second article. content: - type: html src: ./content/[year][month][day].[title].html - type: markdown path: /articles/[year][month][day].[title].md excerpt: This is the content of the third article. content: - type: html src: ./content/[year][month][day].[title].html

      • 保存并关闭config文件。

      • posts目录下的content目录下,创建一个名为year/[month][day]的目录,用于存放你的文章。

      • content目录下,创建一个名为year/[month][day].html的文件,内容与content数组中对应的文章内容相同。

      现在,你已经修改了/pages目录下的articleconfig文件,可以通过以下步骤在本地访问和编辑文章:

      • 打开终端(在Mac/Linux中是Terminal,在Windows中是cmdpowershell)。
      • 进入你的Hexo项目目录,例如:

      cd /path/to/your/hexo/project

      • 打开/pages目录下的config文件,例如:

      cd /path/to/your/hexo/project/config

      • blog字段中,将title字段的值更改为你希望的文章标题,例如:

      yaml title: [Your Blog Name]

      • description字段中,将你希望的文章描述的值更改为你希望的文章描述,例如:

      yaml description: A personal blog

      • authors字段中,将你希望的博客作者的值更改为你希望的博客作者,例如:

      yaml authors: - [Your Author Name]

      • categories字段中,将你希望的博客分类的值更改为你希望的博客分类,例如:

      yaml categories: - [Your Category Name]

      • tags字段中,将你希望的博客标签的值更改为你希望的博客标签,例如:

      yaml tags: - [Your Tag Name]

      • posts字段中,将你希望的content数组中的每个对象的type字段更改为markdown,例如:

      yaml posts: - type: markdown path: /articles/[year][month][day].[title].md excerpt: This is the content of the article. content: - type: html src: ./content/[year][month][day].[title].html - type: markdown path: /articles/[year][month][day].[title].md excerpt: This is the content of the second article. content: - type: html src: ./content/[year][month][day].[title].html - type: markdown path: /articles/[year][month][day].[title].md excerpt: This is the content of the third article. content: - type: html src: ./content/[year][month][day].[title].html

      • 保存并关闭config文件。

      • content目录下,创建一个名为year/[month][day]的目录,用于存放你的文章。

      • content目录下,创建一个名为year/[month][day].html的文件,内容与content数组中对应的文章内容相同。

    • 狐仙不是妖 普通会员 3楼

      在Hexo中,可以通过以下步骤修改Node_modules中的文件,并获取文章内容:

      1. 打开你的Hexo项目目录,找到/pages目录下的article目录。

      2. 在该目录下,创建一个名为article.js的文件,其中包含以下内容:

      javascript module.exports = { title: 'My First Article', path: '/articles/my-first-article', excerpt: 'This is the content of my first article. It describes the title and path of the article. This article is part of the ' + config.blog.name + ' blog series. You can find more articles in the blog directory.', meta: { description: 'Learn how to create a new article in Hexo. This article describes the steps to create a new article in Hexo, including the title, path, excerpt, and meta information.' }, content: [ { type: 'html', src: './content/my-first-article.html' } ] };

      content数组中,每个元素是一个对象,包含以下字段:

      • type:文章的类型,即文章的类型(如htmlmarkdown)。
      • src:文章的内容,即文章的内容。

      src字段中,你可以使用Markdown语法来编写文章内容,例如:

      ```markdown

      My First Article

      This is the content of my first article. It describes the title and path of the article. This article is part of the [config.blog.name] blog series. You can find more articles in the blog directory. ```

      这将生成一个HTML文件,内容与article.js文件中描述的内容相同。

      1. 打开/pages目录下的config文件,找到blog.yml文件,其中包含以下内容:

      ```yaml title: [Your Blog Name] description: A personal blog authors: [Your Author Name] categories: [Your Category Name] tags: [Your Tag Name]

      posts: - type: markdown path: /articles/[year][month][day].[title].md excerpt: This is the content of the article. content: - type: html src: ./content/[year][month][day].[title].html ```

      config文件中,title字段定义了你的博客的名称,description字段定义了博客的描述,authors字段定义了博客的作者,categories字段定义了博客的分类,tags字段定义了博客的标签。

      posts数组中,每个元素是一个对象,包含以下字段:

      • type:文章的类型,即文章的类型(如markdownhtml)。
      • path:文章的路径,即文章的URL路径。
      • excerpt:文章的摘要,即文章的内容摘要。
      • content:文章的内容,即文章的内容。

      content字段中,你可以使用Markdown语法来编写文章内容,例如:

      ```markdown

      [Year] [Month] [Day] [Title]

      • This is the content of the article.
      • It is about [Article Topic].
      • You can find more articles in the [blog directory]. ```

      这将生成一个HTML文件,内容与article.js文件中描述的内容相同。

      1. 打开/pages目录下的pages目录,创建一个名为post/[year][month][day]的目录,用于存放你的文章。

      2. 在该目录下,创建一个名为post/[year][month][day].html的文件,内容与content数组中对应的文章内容相同。

      现在,你可以通过以下步骤在本地访问和编辑文章:

      • 打开终端(在Mac/Linux中是Terminal,在Windows中是cmdpowershell)。
      • 进入你的Hexo项目目录,例如:

      cd /path/to/your/hexo/project

      • 打开/pages目录下的config文件,例如:

      cd /path/to/your/hexo/project/config

      • blog字段中,将title字段的值更改为你希望的文章标题,例如:

      yaml title: [Your Blog Name]

      • description字段中,将你希望的文章描述的值更改为你希望的文章描述,例如:

      yaml description: A personal blog

      • authors字段中,将你希望的博客作者的值更改为你希望的博客作者,例如:

      yaml authors: - [Your Author Name]

      • categories字段中,将你希望的博客分类的值更改为你希望的博客分类,例如:

      yaml categories: - [Your Category Name]

      • tags字段中,将你希望的博客标签的值更改为你希望的博客标签,例如:

      yaml tags: - [Your Tag Name]

      • posts字段中,将你希望的content数组中的每个对象的type字段更改为markdown,例如:

      yaml posts: - type: markdown path: /articles/[year][month][day].[title].md excerpt: This is the content of the article. content: - type: html src: ./content/[year][month][day].[title].html - type: markdown path: /articles/[year][month][day].[title].md excerpt: This is the content of the second article. content: - type: html src: ./content/[year][month][day].[title].html - type: markdown path: /articles/[year][month][day].[title].md excerpt: This is the content of the third article. content: - type: html src: ./content/[year][month][day].[title].html

      • 保存并关闭config文件。

      • posts目录下的content目录下,创建一个名为year/[month][day]的目录,用于存放你的文章。

      • content目录下,创建一个名为year/[month][day].html的文件,内容与content数组中对应的文章内容相同。

      现在,你已经修改了/pages目录下的articleconfig文件,可以通过以下步骤在本地访问和编辑文章:

      • 打开终端(在Mac/Linux中是Terminal,在Windows中是cmdpowershell)。
      • 进入你的Hexo项目目录,例如:

      cd /path/to/your/hexo/project

      • 打开/pages目录下的config文件,例如:

      cd /path/to/your/hexo/project/config

      • blog字段中,将title字段的值更改为你希望的文章标题,例如:

      yaml title: [Your Blog Name]

      • description字段中,将你希望的文章描述的值更改为你希望的文章描述,例如:

      yaml description: A personal blog

      • authors字段中,将你希望的博客作者的值更改为你希望的博客作者,例如:

      yaml authors: - [Your Author Name]

      • categories字段中,将你希望的博客分类的值更改为你希望的博客分类,例如:

      yaml categories: - [Your Category Name]

      • tags字段中,将你希望的博客标签的值更改为你希望的博客标签,例如:

      yaml tags: - [Your Tag Name]

      • posts字段中,将你希望的content数组中的每个对象的type字段更改为markdown,例如:

      yaml posts: - type: markdown path: /articles/[year][month][day].[title].md excerpt: This is the content of the article. content: - type: html src: ./content/[year][month][day].[title].html - type: markdown path: /articles/[year][month][day].[title].md excerpt: This is the content of the second article. content: - type: html src: ./content/[year][month][day].[title].html - type: markdown path: /articles/[year][month][day].[title].md excerpt: This is the content of the third article. content: - type: html src: ./content/[year][month][day].[title].html

      • 保存并关闭config文件。

      • content目录下,创建一个名为year/[month][day]的目录,用于存放你的文章。

      • content目录下,创建一个名为year/[month][day].html的文件,内容与content数组中对应的文章内容相同。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部