【发布时间】:2018-02-22 08:49:11
【问题描述】:
我正在尝试使用Hexo 构建一个博客,使用Pug 作为模板引擎。我do not understand 如何访问全局变量(在 Hexo 中定义)并最终添加了一些 JavaScript 来调试输出。
在 Hexo 中,site 变量保存有关正在使用的站点的信息。这是一个基于 JavaScript 的静态博客生成器,可以在处理过程中注入 JS。在处理模板时,我希望在控制台上输出 site 变量(_ 是 Lodash 的全局变量):
_.forEach(site, function (po) {
console.log('post info', po)
})
我得到的输出是(我分解了post info 块):
post info Query {
data:
[ Document {
title: 'first article',
layout: 'mypost',
_content: '\nThis is the first article',
source: '_posts/first.md',
raw: '---\ntitle: first article\ntags: tech\nlayout: mypost\n---\n\nThis is the first article',
slug: 'first',
published: true,
date: moment("2018-02-20T20:57:27.120"),
updated: moment("2018-02-22T09:19:24.597"),
comments: true,
photos: [],
link: '',
_id: 'cjdy8m62w00005ondnc5md94c',
content: '<p>This is the first article</p>\n',
site: [Object],
excerpt: '',
more: '<p>This is the first article</p>\n',
path: [Getter],
permalink: [Getter],
full_source: [Getter],
asset_dir: [Getter],
tags: [Getter],
categories: [Getter],
prev: [Object],
__post: true,
lang: null,
canonical_path: '2018/02/20/first/index.html' },
Document {
title: 'Hello World 2',
_content: '\nSecond article comes here',
source: '_posts/second.md',
raw: '---\ntitle: Hello World 2\ntags: tech\n---\n\nSecond article comes here',
slug: 'second',
published: true,
date: moment("2018-02-20T23:05:52.156"),
updated: moment("2018-02-22T09:19:48.205"),
comments: true,
layout: 'post',
photos: [],
link: '',
_id: 'cjdy8m63300015ond87428rhr',
content: '<p>Second article comes here</p>\n',
site: [Object],
excerpt: '',
more: '<p>Second article comes here</p>\n',
path: [Getter],
permalink: [Getter],
full_source: [Getter],
asset_dir: [Getter],
tags: [Getter],
categories: [Getter],
next: [Object],
__post: true,
lang: null,
canonical_path: '2018/02/20/second/index.html' } ],
length: 2 }
post info Query { data: [], length: 0 }
post info Model {
domain: null,
_events: {},
_eventsCount: 0,
_maxListeners: undefined,
name: 'Category',
data: {},
_mutex: Mutex { _locked: false, _queue: [] },
schema:
Schema {
paths:
{ name: [Object],
parent: [Object],
slug: [Object],
path: [Object],
permalink: [Object],
posts: [Object],
length: [Object],
_id: [Object] },
statics: {},
methods: {},
hooks: { pre: [Object], post: [Object] },
stacks:
{ getter: [Array],
setter: [Array],
import: [Array],
export: [Array] } },
length: 0,
Document: { [Function] super_: [Function: Document] },
Query: { [Function] super_: [Function: Query] } }
post info Model {
domain: null,
_events: {},
_eventsCount: 0,
_maxListeners: undefined,
name: 'Tag',
data:
{ cjdy8m63600025ondq69zill9: { name: 'tech', _id: 'cjdy8m63600025ondq69zill9' } },
_mutex: Mutex { _locked: false, _queue: [] },
schema:
Schema {
paths:
{ name: [Object],
slug: [Object],
path: [Object],
permalink: [Object],
posts: [Object],
length: [Object],
_id: [Object] },
statics: {},
methods: {},
hooks: { pre: [Object], post: [Object] },
stacks:
{ getter: [Array],
setter: [Array],
import: [Array],
export: [Array] } },
length: 1,
Document: { [Function] super_: [Function: Document] },
Query: { [Function] super_: [Function: Query] } }
post info {}
我不知道Query、Model、Schema 和Document 是什么,以及如何访问其中的数据。
它们后面的似乎是一个对象(键和值用冒号分隔),但它也包含诸如
之类的结构schema:
Schema {
paths:
{ name: [Object],
(...)
我不明白(特别是Schema { 部分)。
这是一种什么样的数据结构以及如何访问其内容?
【问题讨论】:
-
他们是classes。
-
@Nit:谢谢。我删除了其他 cmets,因为我自己发现了如何处理我认为是类实例的成员(在我的情况下)。您介意引导一个答案,我将对其进行编辑以添加一些细节吗?
-
你可以用所有细节回答你自己的问题,这在 Stack Overflow 上完全没问题。
-
@Nit:是的,我知道,当我自己找到自己问题的答案时,我总是这样做。我也喜欢对那些帮助我的人给予可量化的信任,通常要求他们只是加强一个答案(通常是他们的评论),然后我会用细节更新它。这样,它对下一个更有用,并且代表信用到了应有的地方。但我很乐意接受您的评论并自己做 - 感谢您的解决方案顺便说一句!
标签: javascript pug hexo