显示帖子编号

Is it possible to show post numbers for each post (Or easy to do via CSS or similar). My use case is running forum mafia, where allowing users to easily refer to a post without the bulk of quoting it would be amazing.

1 个赞

Just press the link button under each post, and the post number is visible there.

It is hard to say if this is what you are looking for, but it should get you close.

https://github.com/sitepoint/discourse-theme/blob/b4b4a7218ffbbfd4ee3cf8aa7421d29c96fdc4ed/assets/stylesheets/desktop/topic-post.scss#L45-L51

We use that CSS to put #1, #2, et al on a topic after the date/timestamp.

10 个赞

Yes, it is, this is amazing :slight_smile:

Thanks so much!

a.post-date span {
  margin-right: 1em;
}

a.post-date:after {
  content: "#"attr(data-post-number);
}

这似乎不再起作用了。有没有当前显示帖子编号的方法?

2 个赞

现在帖子标签没有 data-post-number 标识符,这个 CSS 样式不起作用,不知道有没有其他解决方法?

没有 JavaScript 就不行。

3 个赞

感谢您的回复,这是需要编写插件来调用API吗?还是直接在主题组件中编写?

非常感谢。我现在已经解决了这个问题!

api.decorateCooked(() => {
          var add_post_number_timer = setTimeout(() => {
            var articles = document.getElementsByTagName("article");
            for (var i = 0; i < articles.length; i++) {
              var post_date = articles[i].getElementsByClassName("post-date");
              if (post_date.length != 0) {
                var post_number = articles[i].getAttribute("id").replace("post_", "");
                post_date[1].setAttribute("data-post-number", post_number);
              }
            }
            clearTimeout(add_post_number_timer);
          }, 500);
        }, {
        id: 'add_post_number'
    });

太棒了!你找到了实现方法。 :+1:

你可以做一些改进:

  • api.decoratedCooked 将被弃用,并且会在每次发布时调用。
    你可以改用 decorateCookedElement,它可以在没有 jQuery 的情况下工作。

  • 一个代表当前帖子的 element 会被传递给 API;你可以将其用作帖子的上下文。

  • 你可以使用 ember 的 schedule 函数来等待元素渲染。

import { schedule } from "@ember/runloop";
// const { schedule } = require("@ember/runloop");  // 如果在 Admin UI 中运行,请使用此行

api.decorateCookedElement((element,  /* helper */) => {
  schedule("afterRender", () => {
    const article = element.closest("article");
    if (article) {
      article.dataset.postNumber = article.id.replace("post_", "");
    }
  });
});

image

3 个赞

:+1:非常感谢您的更正。