Skip to content

Update documentation links #1441

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

Merged
merged 1 commit into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 27 additions & 36 deletions assets/js/doclinks.js
Original file line number Diff line number Diff line change
@@ -1,63 +1,54 @@
'use strict';

// Wraps some elements in anchor tags referencing to the Symfony documentation
$(function() {
var $modal = $('#sourceCodeModal');
var $controllerCode = $modal.find('code.php');
var $templateCode = $modal.find('code.twig');
document.addEventListener('DOMContentLoaded', function() {
const modalElt = document.querySelector('#sourceCodeModal');
if (!modalElt) {
return;
}
const controllerCode = modalElt.querySelector('code.php');
const templateCode = modalElt.querySelector('code.twig');

function anchor(url, content) {
return '<a class="doclink" target="_blank" href="' + url + '">' + content + '</a>';
};
}

function wrap(content, links) {
return content.replace(
new RegExp(Object.keys(links).join('|'), 'g'),
token => anchor(links[token], token)
);
};
}

// Wraps links to the Symfony documentation
$modal.find('.hljs-comment').each(function() {
$(this).html($(this).html().replace(/https:\/\/symfony.com\/doc\/[\w/.#-]+/g, function(url) {
return anchor(url, url);
}));
// Wrap Symfony Doc urls in comments
[...modalElt.querySelectorAll('.hljs-comment')].forEach((commentElt) => {
commentElt.innerHTML = commentElt.innerHTML.replace(/https:\/\/symfony.com\/[\w/.#-]+/g, (url) => anchor(url, url));
});

// Wraps Symfony's attributes
var attributes = {
'Cache': 'https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/cache.html',
'IsGranted': 'https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/security.html#isgranted',
'ParamConverter': 'https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html',
'Route': 'https://symfony.com/doc/current/routing.html#creating-routes-as-attributes-or-annotations',
'Security': 'https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/security.html#security'
// Wraps Symfony PHP attributes in code
const attributes = {
'Cache': 'https://symfony.com/doc/current/http_cache.html#http-cache-expiration-intro',
'Route': 'https://symfony.com/doc/current/routing.html#creating-routes-as-attributes',
'IsGranted': 'https://symfony.com/doc/current/security.html#security-securing-controller-annotations'
};

$controllerCode.find('.hljs-meta').each(function() {
var src = $(this).text();

$(this).html(wrap(src, attributes));
[...controllerCode.querySelectorAll('.hljs-meta')].forEach((elt) => {
elt.innerHTML = wrap(elt.textContent, attributes);
});

// Wraps Twig's tags
$templateCode.find('.hljs-template-tag + .hljs-name').each(function() {
var tag = $(this).text();

[...templateCode.querySelectorAll('.hljs-template-tag + .hljs-name')].forEach((elt) => {
const tag = elt.textContent;
if ('else' === tag || tag.match(/^end/)) {
return;
}

var url = 'https://twig.symfony.com/doc/3.x/tags/' + tag + '.html#' + tag;

$(this).html(anchor(url, tag));
const url = 'https://twig.symfony.com/doc/3.x/tags/' + tag + '.html#' + tag;
elt.innerHTML = anchor(url, tag);
});

// Wraps Twig's functions
$templateCode.find('.hljs-template-variable > .hljs-name').each(function() {
var func = $(this).text();

var url = 'https://twig.symfony.com/doc/3.x/functions/' + func + '.html#' + func;

$(this).html(anchor(url, func));
[...templateCode.querySelectorAll('.hljs-template-variable > .hljs-name')].forEach((elt) => {
const func = elt.textContent;
const url = 'https://twig.symfony.com/doc/3.x/functions/' + func + '.html#' + func;
elt.innerHTML = anchor(url, func);
});
});
10 changes: 6 additions & 4 deletions src/Controller/BlogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function index(Request $request, int $page, string $_format, PostReposito
* after performing a database query looking for a Post with the 'slug'
* value given in the route.
*
* See https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html
* See https://symfony.com/doc/current/doctrine.html#automatically-fetching-objects-entityvalueresolver
*/
#[Route('/posts/{slug}', name: 'blog_post', methods: ['GET'])]
public function postShow(Post $post): Response
Expand All @@ -93,10 +93,10 @@ public function postShow(Post $post): Response
}

/**
* NOTE: The ParamConverter mapping is required because the route parameter
* NOTE: The #[MapEntity] mapping is required because the route parameter
* (postSlug) doesn't match any of the Doctrine entity properties (slug).
*
* See https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html#doctrine-converter
* See https://symfony.com/doc/current/doctrine.html#doctrine-entity-value-resolver
*/
#[Route('/comment/{postSlug}/new', name: 'comment_new', methods: ['POST'])]
#[IsGranted('IS_AUTHENTICATED')]
Expand Down Expand Up @@ -140,7 +140,9 @@ public function commentNew(
* a route name for it.
*
* The "id" of the Post is passed in and then turned into a Post object
* automatically by the ParamConverter.
* automatically by the ValueResolver.
*
* See https://symfony.com/doc/current/doctrine.html#automatically-fetching-objects-entityvalueresolver
*/
public function commentForm(Post $post): Response
{
Expand Down