Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When used with a foreach. Only the first result has a rel= #13

Closed
idmx1 opened this issue Oct 4, 2019 · 4 comments
Closed

When used with a foreach. Only the first result has a rel= #13

idmx1 opened this issue Oct 4, 2019 · 4 comments
Labels

Comments

@idmx1
Copy link

idmx1 commented Oct 4, 2019

I am using Parsedown to parse a text with links from MySQL.

As recommended in another answer on StackOverflow: https://stackoverflow.com/a/47148927/10415318 I am using the Parsedown Extra Plugin https://github.com/taufik-nurrohman/parsedown-extra-plugin

When doing the query and the foreach all I get is the first result to have a rel=ugc attribute, the other ones don't have it. The links render ok, just not the rel attribute.

Here is the PHP code I have:

 public function getSingleThread($url) {
            $Parsedown = new ParsedownExtraPlugin();

            $Parsedown->linkAttributes = function($Text, $Attributes, &$Element, $Internal) {
              if (!$Internal) {
                  return [
                      'rel' => 'ugc',
                      'target' => '_blank'
                  ];
              }
              return [];
            };

            $thread = lib::$db->GetRow("SELECT t.*,
                                         t.description AS text,
                                         p.photo_small,
                                         p.username,
                                         p.name
                                      FROM forum_threads AS t
                                      INNER JOIN user_profiles AS p
                                        ON t.id_author = p.id_user
                                      AND t.url = ".lib::$db->qstr($url));

            $userId = $_SESSION["user"]["id_user"];

            $thread['editDesc'] = $thread['description'];

            $thread['description'] = $Parsedown->text($thread['description']);

            $idThread = $thread['id_thread'];
            $posts = lib::$db->GetAll("SELECT
                                        r.*,
                                        p.photo_small,
                                        p.username,
                                        p.name AS author_name,
                                        p.occupation AS author_job
                                      FROM forum_replies AS r
                                      INNER JOIN user_profiles as p
                                          ON r.id_author = p.id_user AND r.id_parent = 0 AND id_thread =".lib::$db->qstr($idThread)."
                                      ORDER BY date_added");

            foreach ($posts as &$postItem) {
              $postItem["replies"] = lib::$db->GetAll("SELECT r.*, p.photo_small,
                                                            p.username,
                                                            p.name AS author_name,
                                                            p.occupation AS author_job
                                                          FROM forum_replies AS r
                                                          INNER JOIN user_profiles as p
                                                              ON r.id_author = p.id_user AND r.id_parent = ".lib::$db->qstr($postItem["id_reply"])."
                                                          ORDER BY date_added");

                $old = $postItem['description'];
                $postItem['description'] = $Parsedown->text($old);
                $postItem['editDesc'] = $old;

                foreach ($postItem["replies"] as &$replies) {
                    $oldDesc = $replies['description'];
                    $replies['description'] = $Parsedown->text($replies['description']);
                    $replies['editDesc'] = $oldDesc;

                }
            }

            lib::$view->assign("thread",$thread);
            lib::$view->assign("posts", $posts);
            lib::$view->assign("userId",$userId);
            lib::$view->assign("pageTemplate", "front/forum/forum_thread.tpl");
        }
@taufik-nurrohman
Copy link
Owner

Making class instance within the for-each loop will increase the memory used. But what happens when you do this?

foreach ($posts as &$postItem) {
    $Parsedown = new ParsedownExtraPlugin;
    // ... your code goes here
}

@idmx1
Copy link
Author

idmx1 commented Oct 4, 2019

Hey, thanks for answering.

Just tried it but its the same result.

Updated my code to give you a full picture of what I am doing.

@idmx1
Copy link
Author

idmx1 commented Oct 4, 2019

Actually based on your answer, this works:

foreach ($posts as &$postItem) {
                $Parsedown->linkAttributes = function($Text, $Attributes, &$Element, $Internal) {
              if (!$Internal) {
                  return [
                      'rel' => 'ugc',
                      'target' => '_blank'
                  ];
              }
              return [];
            };

// Code goes in here
}

@taufik-nurrohman
Copy link
Owner

taufik-nurrohman commented Oct 4, 2019

This seems to be caused by inlineImage() method that inherits to inlineLink() method. In my extension, I need to alternately attach and unmount the linkAttributes property on the image and link. I will mark this issue as a bug.

For quick fix, try to put this line next out of the if ( ... ) {} statement.

@taufik-nurrohman taufik-nurrohman changed the title When used with a foreach. Only the first result has a rel=nofollw When used with a foreach. Only the first result has a rel= Nov 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants