Mostra i numeri dei post

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 Mi Piace

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 Mi Piace

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);
}

Questo non sembra più funzionare. Esiste un modo attuale per mostrare i numeri dei post?

2 Mi Piace

Ora il tag post non ha un identificatore data-post-number, questo stile CSS non funziona, non so se c’è un altro modo per risolverlo?

Non senza javascript.

3 Mi Piace

Grazie per la risposta, si tratta di un plugin che deve essere scritto per chiamare l’API? O è semplicemente scritto nel componente del tema?

Grazie mille. Ho risolto il problema adesso!

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'
    });

Fantastico! Hai capito come fare. :+1:

Ecco alcuni miglioramenti che puoi apportare:

  • api.decoratedCooked sarà deprecato e viene chiamato su ogni post.
    Puoi usare decorateCookedElement invece, che funziona senza jQuery.

  • Un element che rappresenta il post corrente viene passato all’API; puoi usarlo come contesto del tuo post.

  • Puoi lavorare con la funzione schedule di ember per attendere il rendering dell’elemento.

import { schedule } from "@ember/runloop";
// const { schedule } = require("@ember/runloop");  // usa questo se stai eseguendo in Admin UI

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

image

3 Mi Piace

:+1: Grazie mille per la correzione.